Link to home
Start Free TrialLog in
Avatar of 4376
4376

asked on

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  
Avatar of scrapdog
scrapdog
Flag of United States of America image

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.
Avatar of rsongyl
rsongyl

Did you specify the header files?
ctype.h?
Avatar of 4376

ASKER

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.
I tried your code with BC 3.11 and BC 4.5 and it worked ?

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().
Remove the second getch() and immediately after the scanf statement insert
fflush(stdin);
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...
Avatar of 4376

ASKER

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.
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...
Avatar of 4376

ASKER

I've never tried creating a new project before.
Thanks for helping me Sinclair
 
Avatar of 4376

ASKER

I've never tried creating a new project before.
Thanks for helping me Sinclair
 
No prob... So, the program works now ? Or do you need more help ?
Avatar of 4376

ASKER

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.  
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.
ASKER CERTIFIED SOLUTION
Avatar of asar
asar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 4376

ASKER

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.  
Avatar of 4376

ASKER

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.  
Avatar of 4376

ASKER

Someting is gone wrong. I meant to reject asar answer.