Solved

Getch trouble.

Posted on 1999-01-09
18
417 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

895 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

15 Experts available now in Live!

Get 1:1 Help Now