简体   繁体   English

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

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

I am beginning desktop programming with c++, and when I run the following code: 我开始使用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;
}

I don't see the "Type in a line" prompt when I run the program inside Eclipse. 在Eclipse中运行程序时,没有看到“键入一行”提示。 Instead if I just type in a response, I see what I typed followed by: 相反,如果我只键入响应,则会看到键入的内容, 然后输入:

Type in a line
the string is Hello World

Why doesn't it first show the prompt "type in a line" before I type in my input? 为什么在我输入内容之前,它没有首先显示提示“在一行中输入”?

You need to flush the output buffer. 您需要刷新输出缓冲区。 I just added std::cout << std::endl; 我刚刚添加了std::cout << std::endl; and include <iostream> . 并包含<iostream> I would suggest you start using std::cout instead of printf . 我建议您开始使用std::cout而不是printf It's type safe. 类型安全。 I tested this inside of Eclipse, without the flush, the line doesn't show up, with the flush it does. 我在Eclipse内部对此进行了测试,但没有刷新,但没有刷新,也没有显示该行。

If you don't want to move to <iostream> I just tested fflush(stdout) from <stdio.h> and it works too. 如果你想要移动到<iostream>我只是测试fflush(stdout)<stdio.h>和它的作品了。 However, I strongly encourage the move, stdio.h is the standard for C, the iostream is the standard for C++ and since you're learning C++... 但是,我强烈建议您这样做,stdio.h是C的标准,iostream是C ++的标准,因为您正在学习C ++ ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM