0

I'm trying to set up Eclipse with C/C++ but it's been causing me problems and despite trying to find a solution most of the afternoon I'm stumped.

I have the main program set up on my Windows PC (originally installed for Java, but now with the C/C++ plugin) and then followed a video on youtube to install/set up the rest like GCC. I can now compile and run programs successfully within the IDE, but for some reason if there is a scanf() function in the program it gets executed first, even if there is a printf() function before it.

I wrote this small program to test it:

int main(void) {
int amount;

printf("Enter amount: ");
scanf("%d", &amount);

printf("amount: %d", amount);

return (0);
}

When it runs, the program demands input first without executing the print statement. If you enter some input, it then prints Enter amount: amount: value.

I've been using Eclipse on my mac for a while and it's never caused me any problems...

Joe
  • 6,497
  • 1
  • 12
  • 27
Daifel
  • 45
  • 1
  • 4
  • This has nothing to do with Eclipse. It's all about buffering. See this: http://stackoverflow.com/q/1716296/10077 – Fred Larson Aug 07 '14 at 16:55
  • I don't really understand this / how it applies to my particular problem. The program would run on my macbook, what's causing it to act up in Windows? Is it not set up properly? I also get this error message during compilation (only sometimes: – Daifel Aug 07 '14 at 17:10
  • Sorry, this is the error message: c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.9.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot open output file C A Modern Approach.exe – Daifel Aug 07 '14 at 17:11
  • Buffering may behave differently on different operating systems. The error message is probably due to the program still running while you're trying to compile it. Windows doesn't like that. – Fred Larson Aug 07 '14 at 17:12
  • For example see http://stackoverflow.com/q/20439193/10077 – Fred Larson Aug 07 '14 at 17:21
  • Hmm, thanks for the quick reply, but I'm afraid I still don't know how to get around the problem. Does this mean I need to alter the code itself? – Daifel Aug 07 '14 at 17:24
  • Did you read the first question I linked, and particularly the [first answer](http://stackoverflow.com/a/1716621/10077)? – Fred Larson Aug 07 '14 at 17:26
  • Yes. I didn't understand all of it. I tried adding setbuf(stdout, NULL); – Daifel Aug 07 '14 at 17:29
  • ... at the beginning of the program. But it didn't make a difference. – Daifel Aug 07 '14 at 17:30
  • How about adding `fflush(stdout);` after the `printf`? – Fred Larson Aug 07 '14 at 17:37
  • Nope, that doesn't seem to be doing anything either. – Daifel Aug 07 '14 at 17:51
  • Update your question to add (not replace) your modified code. I'll take a look. – Fred Larson Aug 07 '14 at 18:03
  • Ok, I restarted Eclipse and then tried it again and it worked using setbuf(stdout, NULL); and fflush(stdout);. So that seems to have solved the main problem, thank you! Does this mean that I have to include the statement at the beginning of all function then? Also, is there a way to stop the error message during compilation from reappearing all the time? (When it appears, the program doesn't compile properly and I have to restart Eclipse). – Daifel Aug 07 '14 at 18:06
  • @Daifel if you execute your original program outside of Eclipse do you have the same problem? I ran it in GDB in Windows, but without Eclipse, and it works fine. – TRKemp Aug 07 '14 at 18:10
  • You should need only the `setbuf` call or the `fflush`, not both. As for the error message, you just need to make sure your program is terminated before you recompile. In Eclipse, the console should have a red square when your program is running. If you click that, it should kill your program. But I'm not sure why it wouldn't just exit normally. – Fred Larson Aug 07 '14 at 18:11
  • So making sure the program terminates is stopping the error. If I accidentally try to compile while it's still running I basically have to restart Eclipse though. Thanks so much for your help anyway, I can get back to actually learning some programming tomorrow :D – Daifel Aug 07 '14 at 18:34
  • @TRKemp sorry just noticed your post. I tried the original program in command prompt and it worked fine. – Daifel Aug 07 '14 at 18:49

0 Answers0