使用 getchar() 和 while 循环重新键入字符调试

问题描述

我在用 C 语言输入字符时遇到了一个小问题。程序在循环的第一次迭代后工作正常,菜单 a)、b)、c)、d)、e) 中的所有选项都有效。然而,问题出现在带有“a”和“c”选项的循环的后续迭代中。就好像它不会立即加载字符,仅在以下行中加载。我放了一个带有代码和控制台窗口的概览屏幕链接。 或者问题出在另一段代码中? 这是我的代码片段:


char menu(void) {
    int ch;

    printf("Enter the letter corresponding to the selected option:\n");     
    printf("  a) adding numbers to the BST tree from the keyboard \n"); 
    printf("  b) adding random numbers to the tree to determine the number of numbers \n"); 
    printf("  c) writing numbers in order from the smallest \n");
    printf("  d) removing a specific number \n");
    printf("  e) end \n");

    while ((ch = getchar()) != EOF) 
    {
        while (getchar() != '\n')
        {
            continue;
        }
        ch = tolower(ch);                           
        if (strchr("abcde",ch) == NULL)            
            puts("Enter a,b,c,d or e:");
        else
            break;
    }
    if (ch == EOF)
        ch = 'e';
    return ch;
}

int main(int argc,char* argv[])    //This is main function
{
    int number,size,size_moose,element_to_delete;
    int left_compartment,right_compartment;
    char choice;
    root = NULL;


    printf("Welcome to a number sorting program that uses the BST (Binary Search Tree) sorting algorithm\n");
    printf("####################################################################################################\n");
    printf("####################################################################################################\n");
    printf("\t\tMENU\n\n");


    while ((choice = menu()) != 'e') {
        switch (choice) {
        case 'a':
            printf("How many numbers do you want to enter?\n");
            scanf("%d",&size);
            printf("Enter the numbers one after the other:\n");
            for (int i = 0; i < size; i++)
            {
                scanf("%d",&number);
                dodawanie(number,root);  //the name of the adding function
            }
            printf("\n");
            break;
        case 'b':
            printf("How many numbers do you want to draw?\n");
            scanf("%d",&size_moose);
            printf("Enter the left boundary of the range (e.g. 1):\n");
            scanf("%d",&left_compartment);
            printf("Enter the right bound of the range (e.g. 100):\n");
            scanf("%d",&right_compartment);
            for (int i = 0; i < size_moose; i++)
            {
                 //using a random number function
                number = losowanie(left_compartment,right_compartment);  
                dodawanie(number,root);
            }
            printf("...I drew a %d number/numbers...\n",size_moose);
            printf("\n");
            break;
        case 'c':
            printf("Listing the numbers in order from the smallest:\n");
            in_order_tree_walk(root);  //a function that prints out ordered numbers
            printf("\n");
            break;
        case 'd':
            printf("Give a specific value to delete:\n");
            scanf("%d",&element_to_delete);
            kasowanie(szukaj(root,element_to_delete));  //using a function to remove a specific element
            printf("\n");
            break;
        default:
            printf("Error in switch statement\n");
        }
    }

    printf("####################################################################################################\n");
    printf("####################################################################################################\n");
    printf("Goodbye!\n");

    return 0;
}

我在屏幕上放了一个程序运行的例子 problemC

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...