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??
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!
>is their any way to skip this
Yes, hit a key.
-Prelude
My best code is written with the delete key.
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.
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 mentOriginally 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.
Parinoia Means Having All The Facts!
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.Originally posted by spike232
that is what i ment
Quzah.
Hope is the first step on the road to disappointment.
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
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); }
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;
}
getchar() is ANSI standard, the others are not (or so I believe).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?
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
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.