Thread: getchar when buffer is empty??

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    17

    Question getchar when buffer is empty??

    when u use getchar if the buffer is empty then it just sits and waits, is their any way to skip this, or chek the buffer first??
    Parinoia Means Having All The Facts!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is their any way to skip this
    Yes, hit a key.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    when using getchar, after selecting your character, unless it is return or enter keys, you will have to hit return or enter to before continuing.

    getchar();
    /* hit key of your choice, then hit return */

    you could use <conio.h> if it is available to you and functions getch(); or getche();
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  4. #4
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    I think he might want to something where, if the buffer is empty do nothing. If the buffer has something in it, read it.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    17
    Originally posted by dharh
    I think he might want to something where, if the buffer is empty do nothing. If the buffer has something in it, read it.
    that is what i ment
    Parinoia Means Having All The Facts!

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by spike232


    that is what i ment
    You need the non-ANSI, DOS based function "kbdhit()". You check kbdhit() which returns true if a key has been struck. There is no ANSI way to do this.

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

  7. #7
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    could you print us some code so that we may inspect your program closely and determine what it is exactly that you want.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  8. #8
    Registered User dharh's Avatar
    Join Date
    Jan 2002
    Posts
    51
    Here is an example of what I use to read stuff from stdin.

    Code:
    /* fgets _stdin function, gets like fgets using _stdin 
    */
    int gets_s(char *str, int size) {
          /* variable to say if there has been an error, might change to a pointer
          ** which would return NULL upon error.
          */
      int error;
      int c;      /* a single "character" */
      char *ptr;  /* pointer to first \n in string */
    
      if (fgets(str, size, stdin) != NULL) {
        if ((ptr = strchr(str, '\n')) != NULL) {
          *ptr = '\0';
          error = 0;
        }
        else {
          while (((c = getchar()) != '\n') && (c != EOF)) {
          }
          error = 1;
        }
      }
      else {
        error = 1;
      }
    
      return (error);
    }

  9. #9
    Unregistered
    Guest
    Out of intrests what's the difference between the getchar() function and the fucntions getch(); or getche(); in the conio.h?

    btw, as said, kbhit() works perfect if in DOS conio.h if u just wanna check if key is waiting, here code showing getting and processing if key has been pressed,

    if(kbhit())
    {
    p = getchar();
    while(p == 'L')
    {
    i = (rand() % 50);
    MsgBox(DIE_STRINGS[i]);
    p = getch();
    }
    if(p == UP) overallDir = UP;
    if(p == DOWN) overallDir = DOWN;
    if(p == LEFT) overallDir = LEFT;
    if(p == RIGHT) overallDir = RIGHT;
    }

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Unregistered
    Out of intrests what's the difference between the getchar() function and the fucntions getch(); or getche(); in the conio.h?
    getchar() is ANSI standard, the others are not (or so I believe).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Unregistered
    Guest
    I'm not sure about ANSII standards, but getchar() takes a character after you hit enter getch() instantly takes the character (no enter means don't push the wrong key!) and getche() is getch() + screen echo.

    getch() is very good for menus in dos and I use it all the time, but, you can run into problems with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a buffer line by line, while using system read
    By Hammad Saleem in forum C Programming
    Replies: 9
    Last Post: 05-27-2008, 05:41 AM
  2. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  3. getchar buffer size
    By oncemyway in forum C Programming
    Replies: 3
    Last Post: 08-02-2005, 12:49 AM
  4. clearthe buffer
    By john_murphy69 in forum C Programming
    Replies: 2
    Last Post: 02-27-2003, 08:13 PM