当我输入 0 时,程序结束。但是getchar()
if语句中有它不起作用,你能帮帮我吗?
在案例 0 中,我希望它从用户那里获取字符。如果输入'N'
或'n'
输入,程序将结束,但如果不输入,程序将重新开始。(从 sec1 开始)。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
sec1:
printf("Select a number between 4 and 0: ");
scanf("%d[\n]", &n);
switch(n)
{
case 0:
puts("Are you sure?");
puts("Yes(Y) or No(N)");
if(getchar() == 'N') goto sec1;
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
default:
puts("Only numbers between 4 and 1 are accepted!");
goto sec1;
break;
}
system("pause");
return 0;
}
工作一
int main()
{
int n, c;
sec1:
printf("Select a number between 4 and 0: ");
scanf("%d/n", &n);
switch(n)
{
case 0:
puts("Are you sure?");
puts("Yes(Y) or No(N)");
fflush(stdin);
if (getchar() == 'N') goto sec1;
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
default:
puts("Only numbers between 4 and 1 are accepted!");
goto sec1;
break;
}
system("pause");
return 0;
}