Thread: getchar() help!

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    1

    getchar() help!

    I am having a problem with getchar().
    How do i avoid getchar() reading the \n character.
    I have this little code
    do
    {
    printf("Enter a number no larger than |32767|\n");
    num = convert();
    if(num < 32768 && num > -32768)
    printf("%d",num);
    else
    printf("-32768");
    printf("\nAgain? 1 = yes, 0 = no\n");
    }while((answer = getchar()) == '1');

    so when the user enters a 1, the program restarts, but the problem is that in the function convert(), getchar reads the newline character '\n'. And what i want it to do get new input from the user not the old input from the Again? question.
    Please help

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Unfortunetly you can't, there is always getch() but that is non-standard.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You can flush the input buffer using something like this:
    >while (getchar() != '\n');
    but you have to put that in the right place, and be sure that there is actually data in the buffer or the process will block.

    Or you could use fgets() to read the input into a char array and then check the first element for y or n.

    To be honest, I'd probably write a seperate function to get the users input, that way you can do better error handling.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    7

    i think i have an idea

    Instead of using the getchar() function u can use the getch() or getche() function.

    It's just an idea

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    2
    You may run an infinite loop and use scanf instead of getchar in which you can set the filter for /n. This should help. Do remember to use fflush(stdin) after scanf in the loop.
    Last edited by Saurav; 12-10-2002 at 11:40 PM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Saurav
    You may run an infinite loop and use scanf instead of getchar in which you can set the filter for /n. This should help. Do remember to use fflush(stdin) after scanf in the loop.
    Populus: *Shreeking in terror*
    Populus: "What have you done?! You have drawn the wrath of the angry forum gods for using undefined behaviour! We are doomed! DOOMED I TELL YOU!
    Populus: *Weeping*

    1) It's '\n' and not '/n'.
    2) Never ever ever use fflush(stdin). If the above narriation doesn't clue you in, it's undefined behavior. Search the board for many (many many many many...) posts on why this is wrong to do. Anyone who tells you to use it is wrong.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  2. getchar buffer size
    By oncemyway in forum C Programming
    Replies: 3
    Last Post: 08-02-2005, 12:49 AM
  3. getchar() problem from K&R book
    By anemicrose in forum C Programming
    Replies: 13
    Last Post: 04-04-2004, 11:06 PM
  4. help with getchar lol
    By Taco Grande in forum C Programming
    Replies: 5
    Last Post: 03-18-2003, 09:25 PM
  5. Can anybody take a look at this?
    By TerryBogard in forum C Programming
    Replies: 10
    Last Post: 11-21-2002, 01:11 PM