0

So I just installed eclipse on windows after having too many problems with my old installation on a vm running Ubuntu.

All problems gone except for what I wrote in the title. Scanf text never comes up first, no matter which program I'm running. It shows text when I have entered all that I can enter. Let me give you an example:

#include <stdio.h>
#include <stdlib.h>

int main() {

    int number1;
    int number2;

    printf ("Type in a number \n");              
    scanf ("%d", &number1);                                

    printf ("Type in a number \n");
    scanf ("%d", &number2);

    int number3 = number1 * number2;

    printf ("%d", number3);                                      


    return 0;
}

Console looks like this:

2
3
Type in a number 
Type in a number 
6

2 and 3 I had to type in first, then the last three lines showed up. That's not how it's supposed to be is it?

Thanks for any help!

  • 1
    The Eclipse console is funny this way. I usually put a `fflush(stdout);` after each `printf` to work around it. – Fred Larson Feb 22 '22 at 21:56
  • Instead of `setbuf(stdout, NULL)` a better compromise is `setlinebuf(stdout)` and ensure that all `printf` have `\n` on the end of the format string. – Craig Estey Feb 23 '22 at 00:56

1 Answers1

0

What seems to work is writing

setbuf(stdout, NULL);

into every main of every program. It does fix the problem, yet there's maybe another fix that's not so annoying as doing that everytime. It's not bad, but I don't get why it simply worked on another instance of eclipse but here not.

  • Disabling buffering on the output stream could have a significant performance impact, if you have many function calls to output functions. Therefore, I would rather recommend calling `fflush( stdout );` between output function calls and input function calls. – Andreas Wenzel Feb 22 '22 at 23:11