【发布时间】:2014-03-15 08:07:06
【问题描述】:
我正在编写一个 C 程序,它需要接受最多 100 个字符的用户输入,但允许用户输入少于该限制。我试图用一个while循环来实现这个想法,该循环继续接受字符输入,直到用户按下回车键(ascii值为13),此时循环应该中断。这是我写的:
char userText[100]; //pointer to the first char of the 100
int count = 0; //used to make sure the user doens't input more than 100 characters
while(count<100 && userText[count]!=13){ //13 is the ascii value of the return key
scanf("%c", &userText[count]);
count++;
}
从命令行启动,如果我输入几个字符然后按回车,提示符只是换行并继续接受输入。我认为问题在于我不了解 scanf 如何接收输入,但我不确定如何更改它。当用户按下回车时,我该怎么做才能使循环中断?
【问题讨论】: