我开始使用 C++ 进行桌面编程,当我运行以下代码时:
#include <stdio.h>
int main (int argc, char ** argv){
enum{ max_string = 127};
static char string[max_string + 1] = "";
printf("Type in a line\n");
fgets(string,max_string,stdin);
printf("the string is %s\n",string);
return 0;
}
当我在 Eclipse 中运行该程序时,没有看到“键入一行”提示。相反,如果我只是输入响应,我会看到我输入的内容随后:
Type in a line
the string is Hello World
为什么在我输入内容之前它不首先显示“输入一行”提示?
最佳答案
您需要刷新输出缓冲区。我刚刚添加了std::cout << std::endl;
并包括<iostream>
。我建议您开始使用 std::cout
而不是printf
。它是类型安全的。我在 Eclipse 内部对此进行了测试,没有刷新,该行不会显示,使用刷新则显示。
如果您不想想要移至<iostream>
我刚刚测试了fflush(stdout)
来自<stdio.h>
它也有效。不过,我强烈鼓励这样做,stdio.h 是 C 的标准,iostream 是 C++ 的标准,而且既然你正在学习 C++...
关于c++ - 在Eclipse中运行C++程序时不出现输入提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16246667/