scanf 读取了错误的字符
scanf reads the wrong char
我需要用 C 语言编写一个程序,让用户选择绘制矩形正方形或打印他制作的棋盘,但每次在第一次输入形状后,程序的行为就好像他输入了一个无效字符一样。
printf("please make you decision R-rectangle, S-square, E-end\n");
scanf("%c", &decision);
do
{
if (decision == 'R') {
printf("you chose rectangle! please enter the X start position, Y start position, hieght and width\n");
scanf("%d %d %d %d", &rectMat[0][rectCounter], &rectMat[1][rectCounter], &rectMat[2][rectCounter], &rectMat[3][rectCounter]);
if (rectCounter + 1 < MAX_RECT)
{
if (checkRECT(rectMat[0][rectCounter], rectMat[1][rectCounter], rectMat[2][rectCounter], rectMat[3][rectCounter], areaMat) == 1)
{
drawRECT(rectMat[0][rectCounter], rectMat[1][rectCounter], rectMat[2][rectCounter], rectMat[3][rectCounter], areaMat);
rectCounter++;
}
else
{
printf("we couldn't fit you rectangle\n");
}
}
else
{
printf("you have too many rectangles\n");
}
}
if (decision == 'S')
{
printf("you chose square! please enter the X start position, Y start position and size of the side\n");
scanf("%d %d %d", &sqrMat[0][rectCounter], &sqrMat[1][rectCounter], &sqrMat[2][rectCounter]);
if (sqrCounter<MAX_SQR)
{
if (checkSQR(sqrMat[0][rectCounter], sqrMat[1][rectCounter], sqrMat[2][rectCounter],areaMat)==1)
{
drawSQR(sqrMat[0][rectCounter], sqrMat[1][rectCounter], sqrMat[2][rectCounter], areaMat);
sqrCounter++;
}
else
{
printf("we couldn't fit your square\n");
}
}
else
{
printf("you have too many sqaures\n");
}
}
if (decision == 'P')
{
printArea(areaMat);
}
if (decision != 'E'&& decision != 'P'&& decision != 'S'&& decision != 'R')
{
printf("invalid entry\n");
}
printf("please make you decision R-rectangle, S-square, E-end\n");
scanf("%c", &decision);
} while (decision != 'E');
当您使用"%c"
格式读取字符时,它将读取输入缓冲区中的下一个字符,不会跳过任何白色-space。而当使用scanf
读取任何内容时,你输入的换行符也会被发送给程序,并在你输入结束后添加为一个白色-space字符。大多数格式确实会跳过前导白色-space(例如 "%d"
格式),但 "%c"
和 "%["
格式不会。
当读取字符时,几乎总是建议跳过前导白色-space,这是通过在格式前添加一个 space 来完成的,例如
scanf(" %c", &decision);
// ^
// |
// Note space here
另请注意,大写字母和小写字母之间存在差异。 'R' != 'r'
。要么检查大小写字母,要么使用 toupper
(or tolower
) 进行转换。
你最好使用 [=e10=] 而不是 scanf()
因为无论如何你都期待一个字符
我需要用 C 语言编写一个程序,让用户选择绘制矩形正方形或打印他制作的棋盘,但每次在第一次输入形状后,程序的行为就好像他输入了一个无效字符一样。
printf("please make you decision R-rectangle, S-square, E-end\n");
scanf("%c", &decision);
do
{
if (decision == 'R') {
printf("you chose rectangle! please enter the X start position, Y start position, hieght and width\n");
scanf("%d %d %d %d", &rectMat[0][rectCounter], &rectMat[1][rectCounter], &rectMat[2][rectCounter], &rectMat[3][rectCounter]);
if (rectCounter + 1 < MAX_RECT)
{
if (checkRECT(rectMat[0][rectCounter], rectMat[1][rectCounter], rectMat[2][rectCounter], rectMat[3][rectCounter], areaMat) == 1)
{
drawRECT(rectMat[0][rectCounter], rectMat[1][rectCounter], rectMat[2][rectCounter], rectMat[3][rectCounter], areaMat);
rectCounter++;
}
else
{
printf("we couldn't fit you rectangle\n");
}
}
else
{
printf("you have too many rectangles\n");
}
}
if (decision == 'S')
{
printf("you chose square! please enter the X start position, Y start position and size of the side\n");
scanf("%d %d %d", &sqrMat[0][rectCounter], &sqrMat[1][rectCounter], &sqrMat[2][rectCounter]);
if (sqrCounter<MAX_SQR)
{
if (checkSQR(sqrMat[0][rectCounter], sqrMat[1][rectCounter], sqrMat[2][rectCounter],areaMat)==1)
{
drawSQR(sqrMat[0][rectCounter], sqrMat[1][rectCounter], sqrMat[2][rectCounter], areaMat);
sqrCounter++;
}
else
{
printf("we couldn't fit your square\n");
}
}
else
{
printf("you have too many sqaures\n");
}
}
if (decision == 'P')
{
printArea(areaMat);
}
if (decision != 'E'&& decision != 'P'&& decision != 'S'&& decision != 'R')
{
printf("invalid entry\n");
}
printf("please make you decision R-rectangle, S-square, E-end\n");
scanf("%c", &decision);
} while (decision != 'E');
当您使用"%c"
格式读取字符时,它将读取输入缓冲区中的下一个字符,不会跳过任何白色-space。而当使用scanf
读取任何内容时,你输入的换行符也会被发送给程序,并在你输入结束后添加为一个白色-space字符。大多数格式确实会跳过前导白色-space(例如 "%d"
格式),但 "%c"
和 "%["
格式不会。
当读取字符时,几乎总是建议跳过前导白色-space,这是通过在格式前添加一个 space 来完成的,例如
scanf(" %c", &decision);
// ^
// |
// Note space here
另请注意,大写字母和小写字母之间存在差异。 'R' != 'r'
。要么检查大小写字母,要么使用 toupper
(or tolower
) 进行转换。
你最好使用 [=e10=] 而不是 scanf()
因为无论如何你都期待一个字符