www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/02/18/00:52:00

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: GETCHE() Command Error using DJGPP?
Date: Mon, 17 Feb 1997 23:25:25 -0800
Organization: Two pounds of chaos and a pinch of salt
Lines: 76
Message-ID: <33095965.3965@cs.com>
References: <3308e01a DOT 2752561 AT news DOT akula DOT com>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp226.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Enemy Mindz wrote:
> 
> Can someone please help me with the following program?  The program is
> supposed to ask you for two character inputs using the getche()
> command.  It is supposed to echo the character back as soon as you
> type it then determine which of the two characters is greater of the
> two.

The problems you describe are because you are mixing getche(), which is
a conio function, with printf(), which is a stdio function.  Under
DJGPP, stdout is line-buffered, which means that text is printed only
when one of the following events occurs:

 - A newline is printed
 - stdout is flushed (i.e, fflush(stdout);)
 - A stdio input function (getchar(), scanf(), etc.) is called
 - The internal buffer overflows

However, the conio functions all read directly from the BIOS keyboard
handler, and write directly to the video memory, completely bypassing
stdio, which is handled by DOS.  The result is that everything you print
will be mixed up, unless you use one of the following techniques:

 - flush stdout after every printf() that doesn't end with a newline
 - use setbuf( stdout, NULL ) to turn off buffering
 - use stdio functions exclusively
 - use conio functions exclusively (for example, cprintf() instead of
printf())

BTW, some comments:

> void main()

This is an illegal declaration for main().  Under ANSI C, main() must
always return an int.  Don't tell me it's a shortcut, or it doesn't
matter, because it does.  You also as a consequence need to put a
"return 0;" at the end of your program.

>         char a, b;

getche() actually returns an int, not a char, because it is possible for
the value returned to be < 0 in the case of an error.  This is pretty
much a technicality based on the behavior of getchar().

>         printf("Which character is greater?");

If I were you, I'd put '\n's at the end of each line, not at the
beginning of the next, unless you are doing something special.

> However when I run this program using DJGPP Compiler, I get the "Which
> character is greater?" statement then it seems like it skips the "Type
> a single character" statement and pauses.  Then when I press a key (in
> this case the 'A' key) the following occurs:
> 
> AType a single character
> Next time dont type the same character.

This doesn't make sense.  Did you press Enter after typing the first
character?  Your program's output doesn't indicate the second character
you typed.

> However it does not indicate the greater of the two, and the getche()
> command is executing before the printf statement.

Try fixing the above bugs and then see if you have problems.  If so,
post the revised source code and somebody will debug it for you.

Hope this helps!

-- 
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I  |        fighteer AT cs DOT com         |
|    *  Proud user of DJGPP!  *    |   http://www.cs.com/fighteer   |
|    ObJoke:  If Bill Gates were a robber, not only would he        |
|     shoot you, but he'd send you a bill for the bullets.          |
---------------------------------------------------------------------

- Raw text -


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