【发布时间】:2016-01-16 23:10:20
【问题描述】:
我的任务是创建一个从小写到大写的简单程序。但是,在测试我的输入是否正确打印时,我注意到每次输入一个字符时,它总是会再次循环,即使它不应该这样做。
#include <stdio.h>
int main() {
char input;
//enter a character
//set the given input char to variable
printf("Enter a character in lower case: \n");
input = getchar();
//Sentinel value is Q
while (input != 'Q'){
printf("you entered %c.\n",input);
printf("\n");
printf("Enter a character in lower case: \n");
input = getchar(); //gets input from inside the loop
}
printf("Goodbye \n");
return 0;
}
测试输出(我输入字符'g'并按回车一次):
Enter a character in lower case:
g
you entered g.
Enter a character in lower case:
you entered
.
我没有发现问题。
【问题讨论】:
-
你试过输入
Q看看会发生什么吗? -
它正在打印
'\n'(换行符)字符。 -
您可能会发现很多重复的内容与此相关:1) stackoverflow.com/questions/2549701/… 2) stackoverflow.com/questions/19342434/…
标签: c