By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,944 Members | 1,299 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,944 IT Pros & Developers. It's quick & easy.

making sure only integer is input

P: n/a
Hi All,

I have a number guessing game in which users try to guess a random number.
Obviously, input is required of type int.
BUT, when a user inputs a string the program will result in an undesired
infinite loop.
Now, I know a string is not what we're after but in terms of error-handling;
How can we prevent users from entering a string?
and if we can't do that:
How can we make sure if a string is entered, it is realized and discarded.
Implying, that the user is warned about the mistake and prompted again in
hope of correct input this time??

I have not included my program here because it is unncessarily large (still
a begginer), but if y'all want; more than happy to attach it.

Thanks for all your time in advance.
Nov 19 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On 2005-11-19, Radith <ra****@xtra.co.nz> wrote:
Hi All,

I have a number guessing game in which users try to guess a random number.
Obviously, input is required of type int.
BUT, when a user inputs a string the program will result in an undesired
infinite loop.


Don't use scanf in that way.
Nov 19 '05 #2

P: n/a
Jordan Abel wrote:
On 2005-11-19, Radith <ra****@xtra.co.nz> wrote:
Hi All,

I have a number guessing game in which users try to guess a random number.
Obviously, input is required of type int.
BUT, when a user inputs a string the program will result in an undesired
infinite loop.


Don't use scanf in that way.


or still use scanf since he is a biginner and check the return value
and provide a appropiate prompt before excepting input again. Nothing
wrong with using scanf as long as you use it correctly.

while (scanf("%d",&num) !=1)/*while not input looking for*/
while( (getchar() !='\n') ); /*eat garbege in stdin that scanf
has*/
puts("puts please enter an integer");/*prompt for proper input*/

Nov 19 '05 #3

P: n/a
Try to deal every input as a string.if it is a num,change it into the
number.
or fflush(stdin); after your scanf(...)
just like:

int a;
while(scanf("%d",&a)!=1)fflush(stdin);

Nov 19 '05 #4

P: n/a
vire wrote:
Try to deal every input as a string.if it is a num,change it into the
number.
or fflush(stdin); after your scanf(...)
just like:

int a;
while(scanf("%d",&a)!=1)fflush(stdin);


Do *not* use fflush(stdin). It's not just nonstandard, it's completely
undefined. See http://www.eskimo.com/~scs/C-faq/q12.26.html. Write code
that doesn't need such tricks instead.

S.
Nov 19 '05 #5

P: n/a
Radith wrote:
Hi All,

I have a number guessing game in which users try to guess a random number.
Obviously, input is required of type int.
BUT, when a user inputs a string the program will result in an undesired
infinite loop.
Now, I know a string is not what we're after but in terms of error-handling;
How can we prevent users from entering a string?
and if we can't do that:
How can we make sure if a string is entered, it is realized and discarded.
Implying, that the user is warned about the mistake and prompted again in
hope of correct input this time??

I have not included my program here because it is unncessarily large (still
a begginer), but if y'all want; more than happy to attach it.

Thanks for all your time in advance.


Here's a function that demonstrates one safe way to read in an integer,
and reject invalid input. It reads a line of text and then checks
whether it consists solely of digits. If so, it is converted to an
integer and if there was no error, the integer is returned. Otherwise,
another line is read, and so on.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

long read_integer(void)
{
/* declare a buffer for holding the input line */
char buf[100] = "";

/* output the initial prompt */
printf("Enter an integer: ");
fflush(stdout);

/* while the user inputs a line of text */
while(fgets(buf, sizeof buf, stdin))
{
/* Remove the newline character from the buffer */
char *p = strchr(buf, '\n');
if(p) *p = 0;

/* check if input contains only digits */
if(strspn(buf, "0123456789") == strlen(buf))
{
long result;
errno = 0;

/* if no conversion error, then break */
result = strtol(buf, 0, 10);
if(errno == 0)
return result;
}

/* Otherwise, output another prompt */
printf("Invalid input, try again: ");
fflush(stdout);
}

if(feof(stdin))
{
printf("End of file reached\n");
return -1;
}

printf("Error reading input\n");
return -2;
}

If the number returned by this function is negative, some unrecoverable
error occurred.

--
Simon.
Nov 19 '05 #6

P: n/a
On Sat, 19 Nov 2005 14:52:53 +1300, in comp.lang.c , "Radith"
<ra****@xtra.co.nz> wrote:
Hi All,

I have a number guessing game in which users try to guess a random number.
Obviously, input is required of type int.
BUT, when a user inputs a string the program will result in an undesired
infinite loop.


Yes, if you're using scanf. Don't do that. The FAQ has some hints
about why not to use scanf.

Use fgets, parse the string to see if its a number, then handle
appropriately.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 19 '05 #7

P: n/a
Simon Biber wrote:
Radith wrote:
How can we make sure if a string is entered, it is realized and
discarded. Implying, that the user is warned about the mistake and
prompted again in hope of correct input this time??


Here's a function that demonstrates one safe way to read in an integer,
and reject invalid input. It reads a line of text and then checks
whether it consists solely of digits. If so, it is converted to an
integer and if there was no error, the integer is returned. Otherwise,
another line is read, and so on.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

long read_integer(void)
{
/* declare a buffer for holding the input line */
char buf[100] = "";

/* output the initial prompt */
printf("Enter an integer: ");
fflush(stdout);

/* while the user inputs a line of text */
while(fgets(buf, sizeof buf, stdin))
{
/* Remove the newline character from the buffer */
char *p = strchr(buf, '\n');
if(p) *p = 0;

/* check if input contains only digits */
if(strspn(buf, "0123456789") == strlen(buf))
{
long result;
errno = 0;

/* if no conversion error, then break */
result = strtol(buf, 0, 10);
if(errno == 0)
return result;
}

/* Otherwise, output another prompt */
printf("Invalid input, try again: ");
fflush(stdout);
}

if(feof(stdin))
{
printf("End of file reached\n");
return -1;
}

printf("Error reading input\n");
return -2;
}

If the number returned by this function is negative, some unrecoverable
error occurred.

Just because I'm feeling mean, I'll point out that the code listed above
does the wrong thing when presented with the output of either of the
programs listed below.

#include <stdio.h>
int main(void)
{
printf("%*s99\n", 99, "");
return 0;
}
#include <stdio.h>
int main(void)
{
printf("\n");
return 0;
}
Rob
Nov 23 '05 #8

P: n/a
Rob Adams wrote:
Simon Biber wrote:
Radith wrote:
How can we make sure if a string is entered, it is realized and
discarded. Implying, that the user is warned about the mistake and
prompted again in hope of correct input this time??


Here's a function that demonstrates one safe way to read in an
integer, and reject invalid input. It reads a line of text and then
checks whether it consists solely of digits. If so, it is converted to
an integer and if there was no error, the integer is returned.
Otherwise, another line is read, and so on.
[snip]
If the number returned by this function is negative, some
unrecoverable error occurred.


Just because I'm feeling mean, I'll point out that the code listed above
does the wrong thing when presented with the output of either of the
programs listed below.

#include <stdio.h>
int main(void)
{
printf("%*s99\n", 99, "");
return 0;
}
#include <stdio.h>
int main(void)
{
printf("\n");
return 0;
}


Good catches. I think this solves the problems.

long read_integer(void)
{
/* declare a buffer for holding the input line */
char buf[100] = "";

/* output the initial prompt */
printf("Enter an integer: ");
fflush(stdout);

/* while the user inputs a line of text */
while(fgets(buf, sizeof buf, stdin))
{
/* Remove the newline character from the buffer */
char *p = strchr(buf, '\n');
if(p)
{
*p = 0;
}
else /* no newline found */
{
int ch;
/* read and discard the rest of the line */
while((ch = getchar()) != EOF && ch != '\n');
if(ch == EOF) break;
printf("Line too long, try again: ");
fflush(stdout);
continue;
}

if(buf[0] == 0) /* empty line */
{
printf("Empty line not acceptable, try again: ");
fflush(stdout);
continue;
}

/* check if input contains only digits */
if(strspn(buf, "0123456789") == strlen(buf))
{
long result;
errno = 0;

/* if no conversion error, then return */
result = strtol(buf, 0, 10);
if(errno == 0)
return result;
}

/* Otherwise, output another prompt */
printf("Invalid input, try again: ");
fflush(stdout);
}

if(feof(stdin))
{
printf("End of file reached\n");
return -1;
}

printf("Error reading input\n");
return -2;
}

--
Simon.
Nov 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.