开关默认值在应该显示的时候没有显示
在我使用 switch 的 C 程序中,我的 int 变量有问题。 我的代码是:
while(1) {
printf("0. END\n1. TRANSLATE\n2. TEST FROM LESSON\n3. RANDOM"
" WORDS TEST\n4. SHOW DICTIONARY\n5. ADD WORD\n"
"6. DELETE WORD\nYOUR CHOICE: ");
scanf("%d",&option);
fflush(stdin);
printf("\n");
switch(option) {
case 0: {
exit(0);
break;
}
case 1: {
system("cls");
translate();
printf("\n");
break;
}
case 2: {
system("cls");
lessons();
printf("\n");
break;
}
case 3: {
randomFromLessons();
printf("\n");
break;
}
case 4: {
system("cls");
allWords();
printf("\n");
break;
}
case 5: {
system("cls");
addWord();
break;
}
case 6: {
system("cls");
deleteWord();
printf("\n");
break;
}
default: {
printf("---------------\n");
printf("WRONG VALUE.\n");
printf("---------------\n\n");
}
}
}
当我在选项 var 中输入“d”时。它显示默认值,这就是我想要的,但是当我按数字 1 启动名为“translate()”的方法,然后返回主菜单并再次按“d”时,它会让我回到“translate()”而不是显示默认值。
当我使用 char 而不是 int 时,没有问题。 那么,到底是什么问题呢?什么事不断发生?我做错了什么?那么在 switch 中使用 char 不是最好的选择吗?
In my C program which is using switch I have problem with my int variable.
My code is:
while(1) {
printf("0. END\n1. TRANSLATE\n2. TEST FROM LESSON\n3. RANDOM"
" WORDS TEST\n4. SHOW DICTIONARY\n5. ADD WORD\n"
"6. DELETE WORD\nYOUR CHOICE: ");
scanf("%d",&option);
fflush(stdin);
printf("\n");
switch(option) {
case 0: {
exit(0);
break;
}
case 1: {
system("cls");
translate();
printf("\n");
break;
}
case 2: {
system("cls");
lessons();
printf("\n");
break;
}
case 3: {
randomFromLessons();
printf("\n");
break;
}
case 4: {
system("cls");
allWords();
printf("\n");
break;
}
case 5: {
system("cls");
addWord();
break;
}
case 6: {
system("cls");
deleteWord();
printf("\n");
break;
}
default: {
printf("---------------\n");
printf("WRONG VALUE.\n");
printf("---------------\n\n");
}
}
}
When I type 'd' into option var. it shows default, which is what I want, BUT when I press number 1 which starts method named "translate()" and then get back into main menu and press 'd' again it gets me back into "translate()" instead of showing the default.
When I use char instead of int, there is no problem.
So, what exactly is the problem? What keeps happening? What am I doing wrong? Isn't using char in switch the best option overall then?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您希望允许文本输入,则应使用
fgets
将输入读取为字符串,然后根据需要转换为整数。如果您只想接受数字,则应该检查
scanf
的结果以查看它是否成功 - 在这种情况下,成功时它将返回1
,以防它成功读取1个参数。如果不成功,它不会覆盖option
但保留以前的值 - 这就是为什么你会得到你所描述的行为。此外,
fflush(stdin);
是未定义的行为,因为fflush
从来不打算在输入流上使用。要从标准输入中丢弃换行符,您可以添加单个getchar()
。所以你可以将代码修复为如下所示:
If you wish to allow text input, you should read the input as a string with
fgets
and then convert to integers as needed.If you only wish to accept numbers, you should check the result of
scanf
to see if it succeeded - in this case it will return1
when successful, in case it managed to read 1 parameter. If not successful, it won't overwriteoption
but keep the previous value - that's why you get the behavior you describe.Furthermore
fflush(stdin);
is undefined behavior sincefflush
was never meant to be used on input streams. To just discard the line feed character from stdin you can add singlegetchar()
.So you could fix the code into something like this: