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

Input prompt for C++ program doesn't appear when running in Eclipse

本文关键字:提示 输入 程序 Eclipse 运行时 C++      更新时间:2023-10-16

我开始用c++进行桌面编程,当我运行以下代码时:

   #include <stdio.h>
    int main (int argc, char ** argv){
    enum{ max_string = 127};
    static char string[max_string + 1] = "";
    printf("Type in a linen");
    fgets(string,max_string,stdin);
    printf("the string is %sn",string);
    return 0;
}

当我在Eclipse中运行程序时,我没有看到"键入一行"提示符。相反,如果我只输入一个响应,我将看到我在之后输入的内容:

Type in a line
the string is Hello World

为什么在我输入之前不先显示提示"键入一行" ?

需要刷新输出缓冲区。我只是加上了std::cout << std::endl;,并包括了<iostream>。我建议你开始使用std::cout而不是printf。它是类型安全的。我在Eclipse中对此进行了测试,没有刷新,这一行不会显示,刷新时它会显示。

如果你想要移动到<iostream>,我刚刚从<stdio.h>测试了fflush(stdout),它也起作用。然而,我强烈鼓励你这么做,stdio.h是C的标准,iostream是c++的标准,既然你正在学习c++…