【发布时间】:2014-05-26 08:21:19
【问题描述】:
当我运行程序时,无论我为shape 输入什么,都会执行最后的else 语句。
#include <stdio.h>
int main(void)
{
char shape = 'a';
printf("What shape do you want?\nEnter 's' for a Square, 'b' for a Box, 't' for a Triangle\n");
while (shape != 's' && shape != 'b' && shape != 't')
{
scanf_s(" %c", &shape);
if (shape == 's')
{
printf("You entered %c", shape);
}
else if (shape == 'b')
{
printf("You entered %c", shape);
}
else if (shape == 't')
{
printf("you entered %c", shape);
}
else
{
printf("Please enter 's' 'b' or 't'");
}
}
return 0;
}
【问题讨论】:
-
尝试删除格式字符串的前导空格。 scanf_s 不是读取单个字符的好选择。 getchar 可能是更好的选择。
-
我不知道
C,但scanf_s(" %c", &shape);没有捕获输入。顺便说一句,你的while永远是真的。 -
@user3525735 : Please avoid anything which is related to chating inside posts and comments(这包括感谢和请...)。
标签: c if-statement char scanf