怎么来fflush(stdin)function不起作用?
我的主要问题是为什么fflush(stdin); function不起作用? 每当我运行代码时,我都无法使用空格ex获得第二个输入。 你好世界,但我得到你好? 谢谢
#include 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); // >%s<<\n"); getchar(); }
因为fflush(stdin)
是未定义的行为。 fflush()
仅由输出流的C标准定义,并且最后一个操作是输出的更新流。
如果您得到任何输出,那将是因为您在问题描述区域中显示的代码不是您实际使用的代码。
关于你的陈述:
我无法用空格ex获得第二个输入。 你好世界,但我得到你好? 。
如果没有printf()
语句中的附加参数,则不会输出任何输出和运行时错误。
行(两个地方) printf("The word you entered was >>%s<<\n");
需要另一个参数,add ,string
,如下所示:
printf("The text you entered was >>%s<<\n", string);
这将解决您的问题。
这是在printf()
添加参数string
后的输出 (而不是删除fflush()
)
显然, fflush(stdin);
这不是真正的问题,至少对于陈述的问题?