Stop/pause program after asking for input

This is a discussion on Stop/pause program after asking for input within the C Programming forums, part of the General Programming Boards category; Hello, I just started learning C using the guide on this page and I found a little problem I haven't ...

  1. #1
    Registered User mrppinzon's Avatar
    Join Date
    Oct 2012
    Posts
    1

    Stop/pause program after asking for input

    Hello, I just started learning C using the guide on this page and I found a little problem I haven't been able to fix by myself.

    On some examples used on the guide, the program ask for an input and should give you a printf in return. However, after you enter the input, it will finish the program without giving you time to see the answer. What do I use to make it stop/pause in order to be able to read the answer?

    An example of this is:

    Code:
    #include <stdio.h>
    
    int main()
    {
        int this_is_a_number;
    
    
        printf( "Please enter a number: " );
        scanf( "%d", &this_is_a_number );
        printf( "You entered %d", this_is_a_number );
        getchar();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    If you enter, say, 4,then press 'enter', the input buffer contains:
    4\n

    The newline remains in the buffer, and is devoured by your "getchar()", leading it to seem like it was skipped.
    To verify this, put a second "getchar()" after the first one and see if the window remains open.

    FAQ > Stop my Windows Console from disappearing everytime I run my program? - Cprogramming.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I get input to stop at 'Esc' instead of \n ?
    By xkronix in forum C Programming
    Replies: 11
    Last Post: 09-17-2012, 12:07 PM
  2. Curses keyboard input without pause
    By guesst in forum Linux Programming
    Replies: 3
    Last Post: 04-14-2008, 11:22 AM
  3. stop input being echoed
    By Kinasz in forum C Programming
    Replies: 11
    Last Post: 07-25-2004, 12:02 PM
  4. input without pause
    By asafze in forum C Programming
    Replies: 3
    Last Post: 06-02-2004, 07:36 AM
  5. Pause in OpenGL, but don't stop drawing... ?
    By Arker in forum Game Programming
    Replies: 4
    Last Post: 10-16-2003, 11:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21