#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/