我想使用 while 循环在我的程序中插入一个条件。最后它会询问您是否要从头开始重复该程序。如果你输入'n'然后程序停止,如果你输入任何其他你继续。问题是即使你在 'n' 中写入它仍然会继续。我正在提供代码,以便您自己查看:
我没有给出程序的其余部分,因为它运行良好,只是循环本身才是问题所在。当我创建一个涉及整数的条件时它工作正常,只是当我想要一个 char 字符串时出现问题。
#include <stdio.h>
#include <stdbool.h>
int main()
{
char cnd[1];
while(cnd != 'n') {
printf("Would you like to continue? If not, then type in 'n', if you do then type in anything else: ");
scanf("%1s", &cnd);
}
return 0;
}
最佳答案
为什么要使用字符数组??您可以只使用常规字符
#include <stdio.h>
int main(){
char cnd;
while(cnd != 'n') {
printf("Would you like to continue? If not, then type in 'n', if you do then type in anything else: ");
scanf("%c", &cnd);
}
return 0;
}
关于c - 尽管满足条件,但循环不会停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54065154/