在使用GDB的Eclipse CDT中,Scanf似乎无法在调试模式下工作

在调试模式下运行此代码时:

#include  #include  int main() { int a, b, c; scanf("%d%d%d", &a, &b, &c); printf("Values entered: %d %d %d\n", a, b, c); return EXIT_SUCCESS; } 

该程序不会请求任何用户输入,只会输出:

输入值:18 78 2130026496

我有同样的问题。 想象一下,如果使用换行符或使用输入函数,则必须清除输出缓冲区。 所以,这样做..

 #include  #include  int main() { int a, b, c; fflush(stdout);//Clears the stdout buffer scanf("%d%d%d", &a, &b, &c); printf("Values entered: %d %d %d\n", a, b, c); return EXIT_SUCCESS; } 

似乎问题是由于在运行scanf之前GDB写入stdin以下行引起的:

18列表线程组 – 可用

scanf("%d%d%d", &a, &b, &c); 将该行解释为int而不是等待用户输入。

我使用的当前解决方案是使用以下方法清除程序开头的stdin

 int ch; while ((ch = getchar()) != '\n' && ch != EOF); 

我知道这是一种黑客,但我搜索了一个多小时的解决方案,我找不到任何。 我希望这可以帮助别人。