[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 426
  • Last Modified:

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  
0
4376
Asked:
4376
1 Solution
 
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
Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

 
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
 
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

Featured Post

Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now