Thread: getchar() doesn't work

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    2

    getchar() doesn't work

    hello I've been messing around with C for a while now. I did some programming for our Highschool robotics team in first but nothing with complicated or anything that required user input from the terminal. I started following the tutorials here and I keep having problems with getchar() command. It works fine when I don't use scanf. However whenever I do use it it doesn't work. I think I should be clearing the input as said in this faq. http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    However, I don't quite understand how it works or what each line does. If someone could give me some clue as to what I'm doing wrong (or right) I'd appreciate it. Also if someone could help explain that faq to me I'd greatly appreciate it. Thanks

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Indeed you should,

    Say you ask the user for input, eg their age.
    They'd probably enter, 40 for example - and hit enter (ie return), meaning a newline on the end of there age is loaded into stdin.
    So stdin now holds, 40\n

    You call, scanf("%d", &age) for example, meaning to take the 40 from the stream and load it into 'age'. Now the stdin stream *contains* \n, you then ask a user to say input their sex using getchar() and the user enters M\n, the steam holds \nM\n so getchar() returns \n, and the stream now holds M\n.

    Of course it's not just newlines that can be left in the stream, It could be anything, eg the user enters something after their age - which is not relevant.
    In turn, you should clear the stream before you ask for input as does that link explain.
    PS: stdin = standard input, usually the keyboard.

    hth.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Just remember there might not always be a newline, which the tutorial explains.

    Edit: Where did MacGyver's post go?
    Last edited by zacs7; 07-12-2007 at 07:03 PM.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    2

    Thank You

    This has baffled me anytime I tried to do anything with user input in C. Now my programs work. Thank you for explaining it and doing it so quickly. If I have anymore questions I can't seem to figure out I'll be sure to ask.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    1
    hi all
    what i believe is using fflush(stdin) is a better idea. the link that you have mentioned is for C++ and we dont need to programme for so many streams in c. you can have fflush(stdin) do all your clearing.

  6. #6
    C maniac
    Join Date
    Aug 2004
    Location
    Cyberwarping to Middle Earth
    Posts
    154
    what i believe is using fflush(stdin) is a better idea.
    Read the FAQ.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by zacs7 View Post
    Edit: Where did MacGyver's post go?
    What post?

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by avani gadhai
    hi all
    what i believe is using fflush(stdin) is a better idea. the link that you have mentioned is for C++ and we dont need to programme for so many streams in c. you can have fflush(stdin) do all your clearing.
    To expand on what kawk said... You believe wrong.

    MacGyver, I don't understand - your post was more informative and more clear than my own.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    It basically repeated what you said, but I thought it was quite late after your post at the time. In addition, I'm pretty sure I've written a post similar to it before when this type of issue came up in the past. Overall, I didn't think it was worth keeping it.

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It was a long post, I'd expect it to be a long time after mine. You also used dot points which is

    • Easy to read
    • To the point



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-25-2009, 09:51 PM
  2. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  3. getchar() to work twice
    By bigboii in forum C Programming
    Replies: 2
    Last Post: 11-06-2005, 10:16 PM
  4. getchar buffer size
    By oncemyway in forum C Programming
    Replies: 3
    Last Post: 08-02-2005, 12:49 AM
  5. getchar() problem.
    By caroundw5h in forum C Programming
    Replies: 11
    Last Post: 06-17-2004, 07:10 PM