tags:

views:

215

answers:

3

I'm new to C. Here's my code:

/* Using scanf() */
#include <stdio.h>

int main(void) {
    int iDec1, iDec2, iDec3;
    printf("Enter three decimals:\n");
    scanf("%d,%d,%d", &iDec1, &iDec2, &iDec3);
    printf("Your decimals are %d, %d and %d.", iDec1, iDec2, iDec3);
    return 0;
}

It works in the Command Prompt, but when I run it through Eclipse it doesn't do anything. After hitting stop, this appears in the Console output:

Enter three decimals

Your decimals are 3, 2147344384 and 2147344384.

What the...? How come it works fine outside Eclipse but not inside Eclipse?

A: 

Fix, remove the commas from the string in scanf()

Try this:

#include <stdio.h>

int main(void) {
   int iDec1, iDec2, iDec3;
   printf("Enter three decimals:\n");
   scanf("%d %d %d", &iDec1, &iDec2, &iDec3);
   printf("Your decimals are %d, %d and %d.", iDec1, iDec2, iDec3);
   return 0;
 }

Output:

Enter three decimals:
4 5 6
Your decimals are 4, 5 and 6.

Or if you stick with your program(commas in scanf string) then you need to separate the integers with commas as shown below:

Output:

Enter three decimals:
4,5,6
Your decimals are 4, 5 and 6.
Prasoon Saurav
That does not fix the problem, I'm afraid.
Pieter
While giving inputs separate them with commas and that should probably work(yeah when you use commas in the string in scanf).
Prasoon Saurav
I have tested it with commas but it simply doesn't work. And even if it did, it should work fine with spaces in Eclipse because it works fine on the command line.
Pieter
A: 

So, this thread might help you out. Yes, it's for Java, not C, but the last post on this thread outlines how to get input to work in eclipse console. This may just come down to how you run your program.

If the info in the link doesn't help, please post the steps you take to execute your program (which menu options you use etc. Also post eclipse version). I'll try to replicate.

Mike Mytkowski
The thing is... according to my code, the printf() command should be executed before the scanf() command. I don't see that printf() command on the screen before the program freezes, which not how I wrote the code.
Pieter
A: 

fflush(stdout); did the trick.

Pieter