Mail Archives: djgpp/2001/10/12/19:04:12
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 -