scanf reads getch

V

VivekR

Hi,

Consider the program snippet below

main
{
....
printf("Enter Choice: ");
int choice = getche();

switch(choice)
{
case '1':
printf("Enter Num:");
int num;
scanf("%d", &num);
.....
break;

// some other cases like '2', '3' etc

case '5':
int x = getche();
...
}
}

My problem is when i type 1 or 2 for the "int choice = getche", I land
on the right case but the scanf reads and displays the previuously
entered character. But that does not happen for case '5'. I tried using
fflush(stdin) still it does not work.

Let me know what is wrong with my usage.

Thanks
Vivek Ragunathan
 
W

Walter Roberson

Consider the program snippet below

You are missing the prototype for main.
{
...
printf("Enter Choice: ");

You are not flushing the output buffer and you are not putting out \n
that would in -some- contexts allow the buffer to be flushed automatically.
Your prompt is not certain to appear before the read takes place.
[On most systems that I've encountered, the prompt will -not- appear
before the read takes place.]
int choice = getche();

You haven't given a definition for getche().
switch(choice)
{
case '1':
printf("Enter Num:");

See above about buffer flushing.

Compatability note: C89 does not permit variables to be declared after
active code; in C89, variables may only be declared at the beginning
of a block.
scanf("%d", &num);
....
break;
// some other cases like '2', '3' etc
case '5':
int x = getche();
..
}
}

My problem is when i type 1 or 2 for the "int choice = getche", I land
on the right case but the scanf reads and displays the previuously
entered character. But that does not happen for case '5'.

We would need to see getche() to make a proper assessment.

What we can say is that after you type the 1 or 2, then unless
getche() does some funky things, you would need to press newline
for the choice to be passed from the system buffers into your program.
Telling C to read a single character does NOT tell the system to
pass on the characters as soon as they are typed (and, before you
ask: No, there is NO portable way to tell the system to pass on
characters as soon as they are typed, and NO, setting stdin to
be unbuffered does NOT accomplish that.)

If you were doing a getchar() then that newline would be left
in the input buffer, and then the newline (and all leading whitespace)
would be skipped by scanf() with %d format, so scanf would read the
number as you expected.

As that is not happening, we can make guesses about what getche() is
doing, but we don't know. We could guess, for example, that
in your implementation, the "e" part of the getche() name stands
for "examine", and thus that after the character has been read,
it is ungetc()'d back into the input stream, there to be read again.
But that's speculation, and you would have to provide the source
for getche() for us to be sure.
I tried using
fflush(stdin) still it does not work.

fflush() is not defined for input files.
 
S

Steven

[snip]
You haven't given a definition for getche().
[snip]

If walter did more research he would find that getche() is the echo
version of getch() in the <conio.h> header file.

Anyway, it is not really standard C library, so maybe you should
refrain from using it, and use getc() instead. One extra Enter won't
kill the program.
 
W

Walter Roberson

If walter did more research he would find that getche() is the echo
version of getch() in the <conio.h> header file.

If Steven did more research, he would find that the first 6 google
hits on conio.h do not define getche() [though one defines _getche()].
Not until hit 7, a French index of Borland C functions, do we find
any kind of definition. conio.h has about 69000 google hits, but
only 4200 hits for conio.h getche . A number of those hits indicate
Anyway, it is not really standard C library, so maybe you should
refrain from using it, and use getc() instead. One extra Enter won't
kill the program.

"not really standard C library" ?? It isn't even provided by gcc.

You know the rule around here: if a referenced routine
isn't defined in the Standard then unless the code for it is given,
any and all behaviour may be imputed to the routine.

Some of the hits I find for getche() indicate that it flushes console
output before doing the read; most don't make any mention of that
behaviour. Clearly it is implementation specific, and absent
a definition of the routine, the interaction with buffered I/O is
undefined.
 
E

Eric Sosman

Steven said:
[snip]
You haven't given a definition for getche().

[snip]

If walter did more research he would find that getche() is the echo
version of getch() in the <conio.h> header file.

Nonsense. getch() returns a pointer to the piece
delivering check to the King, or NULL if the King is not
in check. If the King is in check, getche() returns a
pointer to the "extra" checking piece if a double check
exists, or returns NULL for a single check.

Piece *p;
p = getch(); /* first attacker */
if (p == NULL) {
printf ("The King is not checked.\n");
}
else {
printf ("The King is checked by the %s at %s",
p->name, p->location);
p = getche(); /* extra attacker */
if (p != NULL)
printf (" and by the %s at %s",
p->name, p->location);
printf (".\n");
}
Anyway, it is not really standard C library,

.... which is why Walter Roberson (you lost the attribution)
said no definition had been supplied.
so maybe you should
refrain from using it, and use getc() instead. One extra Enter won't
kill the program.

Agreed, heartily. But see Question 12.18 in the FAQ

http://www.eskimo.com/~scs/C-faq/top.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top