473,385 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

scanf reads getch

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

Nov 15 '05 #1
4 2752
In article <11**********************@o13g2000cwo.googlegroups .com>,
VivekR <Ke*****@gmail.com> wrote:
Consider the program snippet below main
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.
int num;
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.
--
Feep if you love VT-52's.
Nov 15 '05 #2

[snip]
int choice = getche();


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.

Nov 15 '05 #3
In article <11**********************@g49g2000cwa.googlegroups .com>,
Steven <so******************@yahoo.com> wrote:
>int choice = getche();
You haven't given a definition for getche().
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
that one must include <windows.h>.

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.
--
"[...] it's all part of one's right to be publicly stupid." -- Dave Smey
Nov 15 '05 #4


Steven wrote:
[snip]
int choice = getche();
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

--
Er*********@sun.com

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Kris | last post by:
Hi! I have the following instruction: char wyp; scanf("%c",&wyp); this scanf is in a while loop, and I want it to get EXACTLY one character each time. Does anyone knows how to do it? BTW....
12
by: B Thomas | last post by:
Hi, I was reading O'Reilly's "Practical C programming" book and it warns against the use of scanf, suggesting to avoid using it completely . Instead it recomends to use using fgets and sscanf....
7
by: hugo27 | last post by:
obrhy8 June 18, 2004 Most compilers define EOF as -1. I'm just putting my toes in the water with a student's model named Miracle C. The ..h documentation of this compiler does state that when...
3
by: Vinicius | last post by:
Hello, the following code does not work: " .... void main(void) { char option; printf("Choose an option: ");
185
by: Martin Jørgensen | last post by:
Hi, Consider: ------------ char stringinput ..bla. bla. bla. do {
18
by: Martin Jørgensen | last post by:
Hi, Today I got a really strange problem... I've made myself a data-file and I read in data from that file.... When I read something like this line: 03 04 05, 00 04 01, 05 03 07, 08 03...
8
by: john | last post by:
I'm trying to use scanf() to get a string that is terminated by a $sign: Reading a file line that has: account number, name (terminated by $sign) and six numbers: 000001 John Doe$ 4 5 6 7 8 9 ...
26
by: tesh.uk | last post by:
Hi Gurus, I have written the following code with the help of Ivor Horton's Beginning C : // Structures, Arrays of Structures. #include "stdafx.h" #include "stdio.h" #define MY_ARRAY 15
5
by: Jack | last post by:
Hi, can someone tell me what is wrong with this code. cout << "Enter the size of the array:"; cin >size; myVar = new char; myVar = 'a'; printf ("%c\n",myVar); puts ("Enter value for the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.