tags:

views:

285

answers:

3

Ok so I am trying to learn C and I want my user to input a value so I am using scanf. I started off not having the flushes, because nothing was comming up until I typed in two values. Now that I have them though I get the same problem there still is no output until after I type in two numbers. Here is my code:

#include <stdio.h>
using namespace std;

int main()
{

    int i1, i2, sums;

    printf( "Enter first integer\n" );
    fflush(stdout);
    scanf( "%d", &i1 );

    printf( "Enter second integer\n" );
    fflush(stdout);
    scanf( "%d", &i2 );
    sums = i1 + i2;

    printf( "Sum is %d\n", sums );
    fflush(stdout);
    return 0;
}

Any help would be greatly appreciated.

A: 
NawaMan
Vitali
+1  A: 

The following works fine for me:

#include <stdio.h>
int main() {
    int i1, i2, sums;

    printf( "Enter first integer\n" );
    scanf( "%d", &i1 );

    printf( "Enter second integer\n" );
    scanf( "%d", &i2 );

    sums = i1 + i2;
    printf( "Sum is %d\n", sums );

    return 0;
}

and gives:

Enter first integer
1
Enter second integer
6
Sum is 7

This is using Cygwin under XP. What platform and compiler are you using?

Update: One possibility is that, because you're running from within the Eclipse environment, it may be doing some weird stuff that interferes with the normal I/O rules.

I'm pretty certain that stdout, even if it's not line buffered like stderr, will autoflush if you attempt to read from stdin (at least in most environments I've used, which is a few).

Eclipse may be fiddling around with the way it attaches the console to the program's actual I/O. I would try to compile the code to a standalone executable and then run it outside the Eclipse environment. If it runs fine there, then it's probably the interaction between Eclipse and the program.

As I stated, your program works fine under XP with Cygwin, even without the flushes.

Further explanation is warranted. As Jerry Coffin rightly points out in a comment, the C standard (c1x, 2009/03/01 draft) states:

5.1.2.1 para 6: The input and output dynamics of interactive devices shall take place as specified in 7.19.3. The intent of these requirements is that unbuffered or line-buffered output appear as soon as possible, to ensure that prompting messages actually appear prior to a program waiting for input.

5.1.2.1 para 7: What constitutes an interactive device is implementation-defined.

7.9.13 para 3: When a stream is unbuffered, characters are intended to appear from the source or at the destination as soon as possible. Otherwise characters may be accumulated and transmitted to or from the host environment as a block. When a stream is fully buffered, characters are intended to be transmitted to or from the host environment as a block when a buffer is filled. When a stream is line buffered, characters are intended to be transmitted to or from the host environment as a block when a new-line character is encountered.

7.9.13 para 7: At program startup, three text streams are predefined and need not be opened explicitly - standard input (for reading conventional input), standard output (for writing conventional output), and standard error (for writing diagnostic output). As initially opened, the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device.

What may be happening is that the way Eclipse interacts with the programs input and output may be causing the program to not recognize stdout as an interactive device. It would then be fully buffered, meaning that you wouldn't see the output until the buffer is full, or the program terminates.

paxdiablo
I am using eclipse in vista, and I am still getting an error with that output.
Which underlying compiler in Vista? Eclipse has the CDT but that still needs you to have a compiler under it. Is it MinGW?. And, is the error just the output not showing up? Or is it an actual error during compile?
paxdiablo
Okay well I am kinda confused all i really wanted to do was create my own program to do this, but I went through the hellow world to get the Bineries? and I think I don't have access to output through those and now I am just really confused
A: 

i think your are flushing out the wrong stuff , try flushing scanf using stdin not using stdout,just like this

#include

main() { int i, j,sums; printf("enter the first integer\n");

  scanf("%d",&i);
  fflush(stdin);
  printf("enter the second integer\n");

  scanf("%d",&j);
  fflush(stdin);
  sums = i + j;
  printf("sum is %d\n",sums);
  //fflush(stdin);
  getchar();

}

Anand