Simple C Program

放肆的年华 提交于 2019-12-25 04:35:18

问题


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.


回答1:


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.




回答2:


I think you need "\n" in side the "scanf". Like this.

scanf( "%d\n", &i2 );

Try it.




回答3:


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();

}



来源:https://stackoverflow.com/questions/1490262/simple-c-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!