gpt4 book ai didi

C、if 语句和 strcmp

转载 作者:太空宇宙 更新时间:2023-11-04 00:47:44 26 4
gpt4 key购买 nike

我不明白为什么它不接受输入。它总是显示“无效输入”...帮助!

while(1)
{
fgets(input, MAX, stdin);
printf("%s", input);

if(strcmp(input, "1") == 0)
{
add();
}
else if(strcmp(input, "2") == 0)
{
delete();
}
else if(strcmp(input, "3") == 0)
{
view();
}
else if(strcmp(input, "4") == 0)
{
break;
}
else
{
printf("Invalid Input!\n");
}
}

最佳答案

因为 fgets() 存储的值包含尾随 '\n'

试试这个

int stop = 0;
while (stop == 0)
{
fgets(input, MAX, stdin);
printf("%s", input);

if (strcmp(input, "1\n") == 0)
add();
else if (strcmp(input, "2\n") == 0)
delete();
else if (strcmp(input, "3\n") == 0)
view();
else if (strcmp(input, "4\n") == 0)
stop = 1;
else
printf("Invalid Input!\n");
}

成功了吗?

因此您需要将其从input 中移除或添加到比较字符串中。

关于C、if 语句和 strcmp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31124858/

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