Solved

Getch trouble.

Posted on 1999-01-09
18
414 Views
Last Modified: 2010-04-15
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
Comment
Question by:4376
18 Comments
 
LVL 5

Expert Comment

by:scrapdog
ID: 1255668
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
 

Expert Comment

by:rsongyl
ID: 1255669
Did you specify the header files?
ctype.h?
0
 

Author Comment

by:4376
ID: 1255670
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
 
LVL 2

Expert Comment

by:harrys
ID: 1255671
I tried your code with BC 3.11 and BC 4.5 and it worked ?

0
 

Expert Comment

by:kx
ID: 1255672
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
 
LVL 4

Expert Comment

by:pagladasu
ID: 1255673
Remove the second getch() and immediately after the scanf statement insert
fflush(stdin);
0
 
LVL 2

Expert Comment

by:Sinclair
ID: 1255674
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
 

Author Comment

by:4376
ID: 1255675
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
 
LVL 2

Expert Comment

by:Sinclair
ID: 1255676
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:4376
ID: 1255677
I've never tried creating a new project before.
Thanks for helping me Sinclair
 
0
 

Author Comment

by:4376
ID: 1255678
I've never tried creating a new project before.
Thanks for helping me Sinclair
 
0
 
LVL 2

Expert Comment

by:Sinclair
ID: 1255679
No prob... So, the program works now ? Or do you need more help ?
0
 

Author Comment

by:4376
ID: 1255680
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
 
LVL 2

Expert Comment

by:Sinclair
ID: 1255681
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
 
LVL 1

Accepted Solution

by:
asar earned 20 total points
ID: 1255682
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
 

Author Comment

by:4376
ID: 1255683
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
 

Author Comment

by:4376
ID: 1255684
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
 

Author Comment

by:4376
ID: 1255685
Someting is gone wrong. I meant to reject asar answer.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now