关于gets,scanf,fflush的问题
比如:
gets();
....
gets();
...
这样为什么能正常输入,第一个gets输入完以后的回车键如果不用fflush不会带到下一个gets里面么?
这样写也没问题:
gets();
....
gets();
...
gets();
...
但是这样就出问题了:
scanf();
gets();
gets();
为什么
[解决办法]
gets会从缓冲区读出回车到字符串中,然后将回车换成'\0'
scanf读取字符串,遇到回车结束。然后在字符串后面添加一个'\0'。 也就是说\n是滞留在缓冲区的。然后你用gets去读,就只读了一个\n,然后替换成了'\0',所以gets什么也没有读到...
[解决办法]
char *gets(char *s)
gets reads the next input line into the array s; it replaces the terminating newline with '\0'. It returns s, or NULL if end of file or error occurs.
[解决办法]
楼主了解 下 输入输出缓冲区的内容,这些问题就 迎刃而解 了。。。
http://www.cnblogs.com/charm/archive/2010/08/17/1801419.html
[解决办法]
gets本身就是用来读取字符串的,回车一并收走。
scanf %c 读取一个字符后回车放回输入缓冲区,被下一次任何企图从输入缓冲区的函数读走。