www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/10/09/20:59:39

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Trouble with getche()
Date: Wed, 09 Oct 1996 20:05:21 -0700
Organization: Three pounds of chaos and a pinch of salt
Lines: 69
Message-ID: <325C67F1.32B4@cs.com>
References: <325B1E56 DOT 5BDA AT erols DOT com>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp118.cs.com
Mime-Version: 1.0
To: tonys111 AT erols DOT com
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Tony Sideris wrote:
> 
> I'm trying to do this simple little C thing, but it won't work right...I
> copied it right out of a C book.
> Here's what I type:
> After I compile it, I run it and the cursor just goes to next line
> (which is blank) and does nothing. Then, for example, I type p it says:
> 
> pType a character:
> You typed: p

As others have mentioned, you're trying to mix buffered (printf) and
unbuffered (getche) output functions, which can lead to trouble no
matter what you do.  What they didn't mention is that even if you do use
fflush() to clear the stdout buffer after a printf(), you may still have
problems.  The <conio.h> functions all write directly to text screen
memory, not to DOS, so if somebody tries to redirect the output from
your program, the 'echoed' keystroke will still appear on the screen. 
Moreover, if somebody tries to redirect the input, it won't work,
because getche() reads the keyboard buffer directly.

There are two solutions to this dilemma:

1) Use standard getc() - or even better, fgets( ..., stdin ) - to read
input normally.  This forces the user to press Enter to get their
keystroke(s) accepted, but it allows both input and output redirection.

2) Use cprintf() along with getche().  cprintf() is the conio equivalent
of printf which writes directly to the screen memory.  You won't be able
to redirect i/o with cprintf()/getche(), but you will always see
everything on the screen as you print it because cprintf() isn't
buffered.  However, you have to watch out for the user pressing special
keys like the arrow, Alt-, and function keys.  These will make getch()
and getche() behave strangely.

BTW, you REALLY should get in the habit of using header files.  The
above program should have at least <stdio.h> and <conio.h> for
correctness.  Try compiling it with '-Wall' and see what happens, i.e.:

    gcc -Wall -g -O -o myprog.exe myprog.c

You should get lots of warnings about implicit function declarations.

Read up on the gcc docs to see what these valuable switches do.

This is what your program _should_ look like:

#include <stdio.h>
#include <conio.h>

int main( void )
{
    char ch;
    cprintf( "Type a character: " );
    ch = getche( );
    cprintf( "\nYou typed: %c", ch );
    return 0;
}

HTH,

John

-- 
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I |        fighteer AT cs DOT com          |
| Proud owner of what might one   |   http://www.cs.com/fighteer    |
| day be a spectacular MUD...     | Plan: To make Bill Gates suffer |
---------------------------------------------------------------------

- Raw text -


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