【发布时间】:2018-04-21 13:51:27
【问题描述】:
我的代码中有一个非常奇怪的问题。 每次我的程序使用 scanf 获取 char 变量的值后,它都会弄乱接下来发生的所有事情,并在循环开始时跳过 get(menu)。 不使用 scanf 时不会发生这种情况。有什么想法吗?
void main()
{
char menu[20] = { 0 }, ch, ch2 = 0, str[SIZE] = { 0 }, str2[SIZE] = { 0 }, mat[SIZE][SIZE] = { 0 };
int boo = 1, loop = 0, num;
while (loop == 0)
{
printMenu();
gets(menu);
num = menuNum(menu);
switch (num)
{
case 0:
loop = 100;
break;
case 1:
boo = 1;
printf("Please enter a long string\n");
gets(str);
printf("Please choose separation character\n");
scanf("%c", &ch);
createMat(str, ch, mat);
break;
case 2:
if (mat[0][0] == '\0')
printf("Matrix does not exist!\n");
else
{
printMatrix(mat);
}
break;
case 3:
boo = longShort(mat);
break;
case 4:
printf("Please enter a long string\n");
gets(str2);
printf("Please choose separation character\n");
scanf("%c", &ch);
compareMat(str2, ch2, mat);
break;
default:
printf("Not a valid option!\n");
}
}
}
【问题讨论】:
-
我忘了说这是一个任务,因此我不能使用任何复杂的东西。只是我已经使用的基础知识。
-
void main是错误的。main应该返回int。 -
不要使用
scanf进行用户输入,也不要使用gets(它已从C11 中删除)。