c++ - 在Eclipse中运行C++程序时不出现输入提示

标签 c++ c eclipse

我开始使用 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/

相关文章:

java - Ant:多次替换源文件中的标记

c++ - 使用 Opencv 调整/裁剪图像

C++ 将数据读入 char*

c - 如何将 unsigned char * 转换为 QString

c - 如果您知道字符编码,您将如何从 C 语言的文本文件中读取,然后将其显示在控制台上?

java - 如何将 2 个 json 合并为一个来解析 JSON

c++ - 错误 : no instance of constructor "Camera::Camera" matches the argument list

c++ - 基于 log4j 的记录器 : log4cpp vs log4cplus vs log4cxx

将二维数组 C 代码转换为 malloc

python - Eclipse + Pydev + twitter 库问题