gpt4 book ai didi

C char 在 if..else 中不起作用

转载 作者:行者123 更新时间:2023-11-30 17:35:20 26 4
gpt4 key购买 nike

当我运行程序时,无论我输入什么 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 需要您要读取的字符数(因此是 _s)。尝试 scanf("%c", &shape, 1)

关于C char 在 if..else 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23025112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com