c - 为什么 getchar() 在 switch case 之后和 while 循环内不暂停?

标签 c

#include <stdio.h>
#include <stdlib.h>

main()
{
    int Q = 1;
    while(Q==1)
    { 
        system("clear");
        printf("MAIN MENU\n");
        printf("--------------------------------------\n");
        printf("1 - See all files\n");
        printf("2 - See all files with permission\n");
        printf("3 - VIM Editor\n");
        printf("4 - EXIT\n");

        fputs("Enter Choice : ",stdout);
        char ch = getchar();

        switch(ch)
        {
            case '1' : system("ls"); break;
            case '2' : system("ls -l"); break;
            case '3' : system("vi"); break;
            case '4' : Q=0; break;
            default  : puts("Wrong Choice.."); break;
        }

        fflush(stdin);

        fputs("PRESS ENTER TO CONTINUE...",stdout);
        getchar();
    }
}

getchar() 不会暂停,而只是清除屏幕并再次启动菜单。

出现这些问题的原因是什么? 我正在使用 tutorialspoint codingground 在线编译器。

最佳答案

如果显示意外字符是什么,您可以自己确定问题:

default:   printf ("Unrecognized choice:  '%c' (%d)", ch, ch);  break;

在任何类似情况下使用该技术都不错。如果代码以某种方式获得意外输入,请说明并显示已知信息。

关于c - 为什么 getchar() 在 switch case 之后和 while 循环内不暂停?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43793259/

相关文章:

c - 为什么我们使用零长度数组而不是指针?

c - 使用 srand() 避免重复生成种子

c - OpenCL 内核定义错误 clBuildProgram(CL_BUILD_PROGRAM_FAILURE)?

c - 基数范围内整数的最小完美散列

c - 在 C 中访问动态数组的部分?

php - http/javascript/php 在 C 中执行后

c - libuv:如何在出现错误时优雅地退出应用程序?

c - 在另一个函数中修改数组 - 崩溃

c - 自旋锁实现 (OSSpinLock)

c - 我需要帮助如何从较大的字符串中获取较小的字符串?在C中