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??
Printable View
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??
>is their any way to skip this
Yes, hit a key.
-Prelude
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();
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 mentQuote:
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.
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.Quote:
Originally posted by spike232
that is what i ment
Quzah.
could you print us some code so that we may inspect your program closely and determine what it is exactly that you want.
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).Quote:
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?
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.