• C

Getch trouble.

When i use getch in my C program and then use scanf, the key typed at getch is echoed at the scanf prompt.
Using fflush or flushall doesn't help.
I use Borland C++ 5.0  
4376Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

scrapdogCommented:
That is because getch doesn't pull any chars out of the buffer, and the keypress goes to the buffer where it will be waiting when scanf picks it up.  Try using getc instead.
0
rsongylCommented:
Did you specify the header files?
ctype.h?
0
4376Author Commented:
Thanks for your comments scrapdog/rsongyl but using getc means that i have to press enter after typing a key. I want to be able to input any key without pressing enter after it.  

Here is a example of the c code.

#include <stdio.h>
#include <conio.h>

main()
{
 int a;
 char c;
 clrscr();
 printf("press any key...");
 c = getch();
 printf("\n\nInput a integer: ");
 scanf("%d",&a);
 printf("\nThe integer is: %d",a);
 getch();
 return 0;
}

When I run this, the key typed at getch appears at the scanf prompt.
0
Managing Security & Risk at the Speed of Business

Gartner Research VP, Neil McDonald & AlgoSec CTO, Prof. Avishai Wool, discuss the business-driven approach to automated security policy management, its benefits and how to align security policy management with business processes to address today's security challenges.

harrysCommented:
I tried your code with BC 3.11 and BC 4.5 and it worked ?

0
kxCommented:
Not Familiar with Borland, but ...
sounds like a compiler or library malfunction.
To debug you might try using setbuf or setvbuf
to get control over the buffer and then dump the
buffer between your input statements to see what
the problem is.  Use a hex format so you can see
the non-printing characters.  Also try fflush to
see why there is no apparent effect of that function.
getc is usually a macro in stdio.h.  Studying the
macro may give you some idea of the name of the variable
that keeps track of the position in the buffer.
You can then print that out for debug as well.
While not efficient, you might consider using
scanf to read in the single character - it might
bypass the problem.
Note that scanf is really no different than fscanf
since they are both buffered i/o.  To avoid the buffering,
use read() and write().
0
pagladasuCommented:
Remove the second getch() and immediately after the scanf statement insert
fflush(stdin);
0
SinclairCommented:
My guess is that you are compiling it for Windows, but run the program in a DOS box, or a console window, or something. My help files say "Do not use this function for Win32s or Win32 GUI applications" for getch, getc, etc. I don't really know how to fix it without writing a full-blown Windows application...
0
4376Author Commented:
Thanks for your answer Sinclair. You were right. BC++ 5.0 comes with bcc.exe and bcc32.exe. When I compiled with bcc32.exe the problem still exsisted. But when i compiled with bcc.exe the problem was solved.
It seems that the IDE must use bcc32 to compile C code.
Now I want to know how I can force the IDE to compile dos code with bcc.exe.
0
SinclairCommented:
Have you tried creating a new project and selecting "DOS" as the OS ? Also, try to change the processor to something like a 386 in compiler options...
0
4376Author Commented:
I've never tried creating a new project before.
Thanks for helping me Sinclair
 
0
4376Author Commented:
I've never tried creating a new project before.
Thanks for helping me Sinclair
 
0
SinclairCommented:
No prob... So, the program works now ? Or do you need more help ?
0
4376Author Commented:
Yes, The program works fine now. I do have an other problem. I'm need to now how i can safely program assembley language in my C programs. I've tried it but the program always hangs.  
0
SinclairCommented:
I doubt you can do it for Windows in a simple way, since Windows takes over everything and any DOS Assembly will just cause it to freak.
0
asarCommented:
this works fine in Borland C++ 4.5.2
and i think it should work for BC 5.0 also
fflush flushes stdout and stderr. It does not flush stdin.
Sometimes it comes due to stack problem.
Reboot your system and try again.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
4376Author Commented:
Thanks for your answer asar but I've already figured out the problem.
This problem exist when you compile with the 32-bit compiler of Borland C++ 5.0 and 5.02. I don't know about other 32-bit compilers or previous versions of Bolands 32-bit compilers.
It works fine when using the 16-bit compiler.

I don't know exactly why this happens, but I think this has to do with a difference in handling of keyboard input with 32-bit programs.  
0
4376Author Commented:
Thanks for your answer asar but I've already figured out the problem.
This problem exist when you compile with the 32-bit compiler of Borland C++ 5.0 and 5.02. I don't know about other 32-bit compilers or previous versions of Bolands 32-bit compilers.
It works fine when using the 16-bit compiler.

I don't know exactly why this happens, but I think this has to do with a difference in handling of keyboard input with 32-bit programs.  
0
4376Author Commented:
Someting is gone wrong. I meant to reject asar answer.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.