getchar() not happening?

This is a discussion on getchar() not happening? within the C Programming forums, part of the General Programming Boards category; Hi there. I'm pretty new to this - finding the tutorials easy to follow though! So after tutorials 3 and ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    getchar() not happening?

    Hi there. I'm pretty new to this - finding the tutorials easy to follow though!
    So after tutorials 3 and 4 I had a crack at doing the stuff below. I managed to get it to work pretty much as intended. The only thing is, when I run it, the end message
    [Process returned 0 (0x0) execution time ### Press any key to continue]
    comes up before I've pressed enter, although I have the "getchar()" at the end.
    If I put a second one in, it does it, or anywhere else in the code, but it doesn't seem to happen there....
    Is it something to do with the switch?


    Code:
    #include <stdio.h>
    
    int mult (int x, int y)
    {return x*y;}
    int add (int x, int y)
    {return x+y;}
    int take (int x, int y)
    {return x-y;}
    
    int main()
    {
        int x;
        int y;
        int input;
        printf("Please input two numbers. \nPut a space between them. ");
        scanf("%d", &x);
        scanf("%d", &y);
        printf("What would you like to do with these numbers? \n");
        printf("1. Multiply \n");
        printf("2. Add \n");
        printf("3. Take \n");
        scanf( "%d", &input);
        switch (input) {
            case 1:
            printf("= %d\n", mult(x,y));
            break;
            case 2:
            printf("= %d\n", add(x,y));
            break;
            case 3:
            printf("= %d\n", take(x,y));
            break;
            default:
            printf("That wasn't an option!\n ");
        }
        getchar();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    5
    Hi,

    if you use "getch()" instead of "getchar()", it works
    But in this case you have to include <conio.h>

    Alex

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Using getch() instead of getchar() is a really bad idea, because getch() is a non-standard function.

    The issue here is that you're mixing formatted-input functions (scanf()) with character-based input functions (getchar()). Let's say you enter a number like 123 and hit enter. The input buffer will contain "123\n". scanf(), upon seeing that you requested a number with %d, will read the 123, be satisfied, and return. Leaving the "\n" in the input buffer.

    Your program continues executing, and hits the getchar() call. getchar() doesn't care what character it reads, as long as it's some character. So it checks the input buffer, and guess what? There's still a "\n" in there, so getchar() reads that and returns.

    The easiest way to get around this is to discard all data up to and including a newline after your scanf() call. Something like this should do the trick:
    Code:
    while(getchar() != '\n') {}
    This will clear the input buffer, without leaving a "\n" or anything in it, so that when you get to the getchar() at the end of main(), it will have to buffer and wait for the user to enter something.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    while(getchar() != '\n');
    ^ Will work too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    2
    ah yes, I think I see what you're saying now.
    As I said, I'm pretty new to this (2 days ), so I'm not sure of all the things "getchar()" does and how it works.
    I'll get it eventually, but thats seems to have cleared that problem, and that would explain why putting 2 getchar()s works, because the previous input has been 'used up' I guess (forgive me for my crude and noobish terms!).
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I don't understand "while (getchar() != '\n');"
    By valedor in forum C++ Programming
    Replies: 13
    Last Post: 09-08-2009, 05:12 PM
  2. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 01:49 PM
  3. getchar buffer size
    By oncemyway in forum C Programming
    Replies: 3
    Last Post: 08-02-2005, 12:49 AM
  4. help with getchar lol
    By Taco Grande in forum C Programming
    Replies: 5
    Last Post: 03-18-2003, 08:25 PM
  5. Can anybody take a look at this?
    By TerryBogard in forum C Programming
    Replies: 10
    Last Post: 11-21-2002, 12:11 PM

Tags for this Thread


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