【发布时间】:2015-03-09 19:18:29
【问题描述】:
谁能解释一下为什么当我输入字符'Q'时我的while循环不会结束?即使我在用户输入“Q”时将布尔值设置为 false,它仍会继续循环,它应该在 char input 的 scanf 之后结束。
我的代码:
#include <stdio.h>
typedef int bool;
#define true 1
#define false 0
int main(void) {
char input;
char output;
bool tf = true;
printf("Welcome to the Coder!\n");
while (tf) {
printf("Choose Input (H,A,B,Q) : ");
scanf_s(" %c\n", &input);
if (input == 'Q') {
tf = false;
}
else {
printf("Choose Output (H,A,B) : ");
scanf_s(" %c\n", &output);
}
}
return 0;
}
【问题讨论】:
-
你在输入 Q 后是否按下回车键?
-
还有另一个 scanf 问题?
-
是的,我确实按了输入
标签: c while-loop