delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/27/04:45:30

From: George Foot <mert0407 AT sable DOT ox DOT ac DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Quick question...make apps wait?
Date: 27 Jan 1998 08:36:53 GMT
Organization: Oxford University, England
Lines: 67
Message-ID: <6ak6b6$1rc$3@news.ox.ac.uk>
References: <34CD6D8F DOT 1077 AT usa DOT net>
NNTP-Posting-Host: sable.ox.ac.uk
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On Mon, 26 Jan 1998 23:15:59 -0600 in comp.os.msdos.djgpp Kharis
Knightwind <kknightwind AT usa DOT net> wrote:

: How do you make a simple application wait for the user to press a key to
: move on?

This isn't really a djgpp question, so it might have been better to
ask a generic C newsgroup like comp.lang.c.

Portably, use gets [1] like so:

	printf ("Press ENTER to continue...\n");
	gets (some_string);

It won't look pretty, though -- the user will be able to type a line
of text and hit Enter.  You can use getchar, of course, but this will
still let them type a line of text, then hit Enter, and future reads
from the input stream will read off subsequent characters of that line
-- probably not what you want.

If you're not interested in portability, you can use (with care) the
conio function getch().  Mixing conio functions with stdio functions
is evil; however, if you make sure that you either print a newline
character ('\n') or call `fflush (stdout)' before doing the getch you
probably won't get bitten.

	printf ("Press almost any key to continue...\n");
	getch();

or

	printf ("Press almost any key to continue...");
	fflush (stdout);
	getch();

If you forget the newline or fflush call then strange things will
happen (e.g. the prompt may be displayed *after* the user presses a
key, rather than before).

If you're really not interested in portability you can use conio
functions for everything, which is probably a more preferred
solution.  The caveat here is that in conio, '\n' is a line feed -- it
moves the cursor down a line, but does not return it to the start of
that line.  Use '\r' for that.  So:

	cprintf ("Line 1\r\n");
	cprintf ("Line 2\r\n");
	cprintf ("Now press a key: ");  /* note: no \n needed! */
	ch = getch();
	cprintf ("%c\r\n", ch);

and so on.


[1] gets is a somewhat dangerous function -- it relies on the
character array whose address you pass being large enough to hold
whatever the user types.  However, the user can type as much as it
wants, so however big you make the buffer it *might* be overrun, which
would be a Bad Thing.

Instead, we can use getchar.  Keep reading characters with getchar
until a newline is encountered.  Since you don't care what the user
actually types, that should be OK -- if you did care you could use
fgets (q.v.) instead of gets.

-- 
george DOT foot AT merton DOT oxford DOT ac DOT uk

- Raw text -


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