gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:52:22 35 4
gpt4 key购买 nike

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

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com