delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/10/12/19:04:12

From: "A. Sinan Unur" <asu1 AT cornell DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: user input problem
Date: 12 Oct 2001 22:49:04 GMT
Organization: Cornell University
Lines: 54
Sender: asu1 AT cornell DOT invalid (on slip-32-102-40-232.ny.us.prserv.net)
Message-ID: <Xns9138BF6C2445FASINANUNUR@132.236.56.8>
References: <3bc7498f DOT 24352957 AT news DOT westnet DOT com DOT au>
NNTP-Posting-Host: slip-32-102-40-232.ny.us.prserv.net
X-Trace: news01.cit.cornell.edu 1002926944 9587 32.102.40.232 (12 Oct 2001 22:49:04 GMT)
X-Complaints-To: usenet AT news01 DOT cit DOT cornell DOT edu
NNTP-Posting-Date: 12 Oct 2001 22:49:04 GMT
User-Agent: Xnews/4.06.22
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

gehok55 AT hotmail DOT com (Josh) wrote in 
news:3bc7498f DOT 24352957 AT news DOT westnet DOT com DOT au:

hi,

this kind of off-topic here because it is a general C question rather than 
being specific to DJGPP. i suggest you take a look at the 
comp.lang.c.moderated faq at http://www.faqs.org/faqs/C-faq/faq/ (esp 
section 12, question 12.19).

a better solution is to use fgets and parse the input using atoi or strtol, 
or maybe sscanf.

hth

#include <stdio.h>

static int read_int_from_stdin(void)
{
	int items;
	int value;
	char buffer[256];

	fgets(buffer, 256, stdin);
	items = sscanf(buffer, "%d", &value);

	if( items != 1 ) value = 0;

	return value;
}

int main(void)
{
	int numplayers;

	do 
	{
		printf("Please enter the number of players [2-4]:\n");
		numplayers = read_int_from_stdin();
		if (numplayers >= 2 && numplayers <= 4) break;
		printf("You entered: %d\n", numplayers);
		printf("Scrabble requires between 2 to 4 players\n"); 
	} while ( 1 == 1 );
	
	printf("You entered: %d\n", numplayers);

	return 0;
}


-- 
--------------------------------
A. Sinan Unur
http://www.unur.com/

- Raw text -


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