Thread: getchar() best practices

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    9

    getchar() best practices

    I have a program that outputs a menu, the input should be a char from stdin. Naturally, I would think to use getchar(). Are you supposed to do the following every time after you are finished getting the character?

    Code:
    while (getchar() != '\n')
        continue;
    I was googling around and I found fflush(stdin). Unfortunately, the behavior of flushing stdin is undefined, so there goes that idea.

    What's the best alternative to using getchar to read character input. Or, do you just have to be prudent to clear stdin of newline characters.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well if you have say

    Code:
    char buff[BUFSIZ];
    if ( fgets( buff, sizeof buff, stdin ) != NULL ) {
      // do stuff with buff, say sscanf
    }
    Then all the various hacks to clean up the input stream cease to be necessary.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by albundy View Post
    I have a program that outputs a menu, the input should be a char from stdin. Naturally, I would think to use getchar(). Are you supposed to do the following every time after you are finished getting the character?

    Code:
    while (getchar() != '\n')
        continue;
    I was googling around and I found fflush(stdin). Unfortunately, the behavior of flushing stdin is undefined, so there goes that idea.

    What's the best alternative to using getchar to read character input. Or, do you just have to be prudent to clear stdin of newline characters.
    It's going to depend what you are reading. Salem has the best suggestion for string inputs, but if you are using the buffer as a string now you need to clean the newline out of the buffer (unless you want your strings to have newlines at the end). For human readable numerics fgets() then sscanf() to pluck your numbers out of the buffer... For single characters getchar() then the little loop you had to clear the \n out of the buffer.

    The old standard of fflush(stdin) only works in compilers who's libraries are wired up to clear the input buffer for you (Turbo C, VC++, and a couple of others) but this is non-standard behavior and can't be trusted on other compilers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Good Programming Practices
    By mark909 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2011, 03:57 PM
  2. Best Practices for Operations on Structures
    By drunken_scot in forum C Programming
    Replies: 20
    Last Post: 09-15-2009, 03:15 PM
  3. Best Practices For Includes
    By valaris in forum C++ Programming
    Replies: 13
    Last Post: 03-09-2009, 03:12 AM
  4. coding practices
    By pobri19 in forum C++ Programming
    Replies: 2
    Last Post: 07-17-2008, 04:56 PM
  5. deque best practices
    By George2 in forum C++ Programming
    Replies: 10
    Last Post: 03-02-2008, 08:11 PM