why do I need 2 getchar()'s?

This is a discussion on why do I need 2 getchar()'s? within the C Programming forums, part of the General Programming Boards category; I'm a total n00b to programming in general, so forgive me if this is a bad question, but I've been ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    9

    why do I need 2 getchar()'s?

    I'm a total n00b to programming in general, so forgive me if this is a bad question, but I've been searching all over the place and can't quite find an answer for my particular problem.

    I want to develop good coding habits as I'm learning, so I'm concerned about this very small problem I've run into. I have a simple program that reads 2 numbers and prints them on the screen. I'm using the getchar() function to stop the output from closing, instead of the system(pause), which I understand is the right way. However, it seems that it doesn't work unless I put 2 getchar()'s at the end of the program, and I can't quite figure out why. Here's what I've got:

    Code:
    #include <stdio.h>
    
    int main()
    {
        int a, b;
        
        printf("Please enter a number: ");
        scanf("%d", &a);
        printf("\n\nPlease enter another number: ");
        scanf("%d", &b);
        printf("\n\nHello, World!  You entered %d and %d!", a, b);
        getchar();
        getchar();
        return 0;
    }
    If I only use 1 getchar() then the program closes after entering the numbers. What am I missing here?

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    The scanf() leaves the newline in the buffer. The first getchar() consumes that newline, the second causes the program to wait.

    You could avoid both getchar() calls if you ran your command line program from the command line.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    9
    After reading up on the scanf() function, I had considered that, but if that's the case, then I would have assumed that adding the 2nd number into the program would have then required 3 getchar()'s at the end, but that doesn't seem to be the case? How come the 2nd getchar() doesn't consume the 2nd newline, requiring me to have a 3rd one?

    I know I could avoid these by running from the command line, but it's very convenient to run them from my compiler, and I also want to make sure that I fully understand exactly what's going on here.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because scanf() eats the previous left-over newline in it's search for the number you're currently trying to input.

    Let's say we have three scanf's asking for numbers, and we enter 1, 2 and 3 for those, the input buffer [assuming we entered all of that "at once"] will contain:
    "1\n2\n3\n".
    The first scanf() eats '1' leaving "\n2\n3\n".
    The second scanf() eats "\n2" part, leaving "\n3\n"
    The thid scanf() eats the "\n3" part, leaving "\n" - so you need a getchar() to eat the newline before the application stops to wait for input.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    9
    Ah, I see, so scanf() is smart enough to tell between a \n and a number, and throws out what it finds while it's searching for valid data? That makes perfect sense, that little detail wasn't in the beginner's tutorial....I think I understand now.

    Thanks to everyone who replied, I appreciate it

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by parx86 View Post
    Ah, I see, so scanf() is smart enough to tell between a \n and a number, and throws out what it finds while it's searching for valid data? That makes perfect sense, that little detail wasn't in the beginner's tutorial....I think I understand now.

    Thanks to everyone who replied, I appreciate it
    Sort of: it will ignore whitespace (spaces, tabs, newlines, etc.) at the beginning -- it won't throw away a 'q', it will just stop.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    9
    I see what you mean, I tried running the program again inserting random amounts of spaces in front of my entries, and it always trims it to just the numbers. However, if I put tabs and spaces AFTER the numbers, the getchar() function doesn't stop the program like before, because it's absorbing what came AFTER my input, correct?

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    so use fgets and sscanf pairs - and nothing will be left out in the incoming buffer
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    9
    OK, I think I have a good understanding of what's going on here, I'm sure I'll have another question very soon

    Thanks everyone for your help!

Popular pages Recent additions subscribe to a feed

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