我正在创建一个程序,它应该验证我输入的正确值,但不幸的是我做错了。如果答案是 1 或 0,该程序需要检查输入值 10 次。否则它必须再次要求答案。输入完成后,它应该通过 printf 显示正确答案,但事实并非如此。
我的猜测是“开关盒”部分出了点问题。帮助将不胜感激!
我的代码:
int main()
{
char a0, a1, a2, a3, a4, a5, a6, a7, a8, a9;
char c = '0';
int a = 0, OK = 0, check = 0, valid_input = 0, a_ok;
printf("Fill in a value 1 or 0.\n\n");
while (a < 10)
{
while (valid_input == 0)
{
printf("Fill in a%d: ", a);
a_ok = scanf("%d", &OK);
if (a_ok != 1)
{
scanf("%s", &c);
}
else if (OK <0 | OK >1)
{
do
{
while (check == 0)
{
printf("Fill in a%d: ", a);
check = check +1;
}
c = getchar();
}
while (!isdigit(c));
ungetc(c, stdin);
}
else
valid_input = 1;
}
switch (a)
{
case 0:
OK = a0;
case 1:
OK = a1;
case 2:
OK = a2;
case 3:
OK = a3;
case 4:
OK = a4;
case 5:
OK = a5;
case 6:
OK = a6;
case 7:
OK = a7;
case 8:
OK = a8;
case 9:
OK = a9;
}
a = a +1;
}
/*
printf("Fill in a1: "); **<--- WANT TO REMOVE THIS PART BY LOOP**
scanf("%d", &a1);
printf("\nFill in a2: ");
scanf("%d", &a2);
printf("\nFill in a3: ");
scanf("%d", &a3);
printf("Fill in a4: ");
scanf("%d", &a4);
printf("\nFill in a5: ");
scanf("%d", &a5);
printf("Fill in a6: ");
scanf("%d", &a6);
printf("\nFill in a7: ");
scanf("%d", &a7);
printf("\nFill in a8: ");
scanf("%d", &a8);
printf("Fill in a9: ");
scanf("%d", &a9);
*/
printf("\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d", a0,a1,a2,a3,a4,a5,a6,a7,a8,a9);
getch();
return(0);
}