delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/01/19/18:17:51

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: getch() and getche()
Date: Sun, 19 Jan 1997 15:26:58 -0800
Organization: Two pounds of chaos and a pinch of salt
Lines: 41
Message-ID: <32E2ADC2.1579@cs.com>
References: <32E251B8 DOT 1C3E AT dmv DOT com>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp104.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Pyro Technic wrote:
> 
> i don't no if NE1 else has noticed this but there is a very annoying
> featire with getch() and getche(). as a newbie i'd much appreciate NE1
> who could point out how to fix it.

Under gcc, stdout is line buffered.  It only gets flushed when you print
a newline or call a stdio input function.  However, getch() and getche()
are conio functions which interface directly with the BIOS.  The two are
not directly compatible and will cause problems if you use them
together.  Please note that this is NOT a bug in djgpp, but a feature of
ANSI C/C++.  You should never mix stdio and conio functions in portable
code.

There are several ways to solve your problem without using a newline:

1)  Manually flush stdout after the cout call.

 cout << "Y for yes or N for No: ";
 fflush( stdout );
 answer = getche();

2)  Use cprinf (a conio function) instead of cout.

 cprintf( "Y for yes or N for No: " );
 answer = getche();

3)  Use stdio input functions.

 cout << "Y for yes or N for No: ";
 cin >> answer;

Of course, here you'll have to press Enter to get your key accepted.
  
-- 
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I  |        fighteer AT cs DOT com         |
| Call me for your free AOL disk!  |   http://www.cs.com/fighteer   |
|  Chain letters, work-at-home schemes, free long distance, etc.,   |
|    are ILLEGAL!  Keep the Internet litter-free... don't SPAM.     |
---------------------------------------------------------------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019