【发布时间】:2013-10-26 18:27:47
【问题描述】:
我的主要问题是为什么 fflush(stdin);功能不工作? 每当我运行代码时,我都无法使用空格 ex 获得第二个输入。你好世界,但我得到你好??谢谢
#include <stdio.h>
main(){
int x;
double y;
char string[100];
/*
* string input
*/
printf("Enter one word: ");
scanf("%s", string); // note there is no & before string */
printf("The word you entered was >>%s<<\n");
printf("Enter many words: ");
fflush(stdin); // <---- for some reason this function is not working
scanf("%[^\n]", string); // read up to a newline (multiple words)
printf("The text you entered was >>%s<<\n");
getchar();
}
【问题讨论】:
-
你的问题与
fflush(stdin)无关。它与缺少的参数有关。见下文。 (把第二个参数放进去,然后就可以做一些格式化工作了)