-
January 14th, 2011, 04:56 AM
#1
Junior Member
scanf problem
peace upon you all every one :
i'm having a littel problem with the scanf() function
the following code asks three users about their age using
scanf("%i",&user_age)
and then it matches if user_age >= 18 and user_age <= 100 and scanf() returned something except zero.
if the user entered a character or any except integer scanf() fails and return zero then the code loop back and try to reread the user age .
the problem is that scanf() won’t re prompt the user to enter any thing !!
but just keep returning 0 and the program keep trying to reread the age
here is the code :
Code:
#include <stdio.h>
int main()
{
int i = 0;
int user_age = 0;
int scanf_result = 0;
for (i=1 ; i<=5 ; i++)
{
printf("user number %i Enter your Age : ",i);
scanf_result = scanf("%i",&user_age);
if (user_age >= 18 && user_age <= 100 && scanf_result > 0)
{
//if the user input is >= 18 and <= 100 and scanf() returned non zero (succeeded) then
//do some work with the user input
}
else
{
printf("your age should be an integer from 18 to 100 try again\n");
i = i -1;
}
}
printf ("\n");
return 0;
}
Decode the following to 8-Bit ASCII : 01001001 01110011 01101100 01100001 01101101 00100000 01101001 01110011 00100000 01110100 01101000 01100101 00100000 01110011 01101111 01101100 01110101 01110100 01101001 01101111 01101110
[SIGPIC] http://www.opensuse.org/en/[/SIGPIC]
Similar Threads
-
By AngelicKnight in forum General Computer Discussions
Replies: 14
Last Post: June 15th, 2006, 04:04 AM
-
By IcSilk in forum Operating Systems
Replies: 8
Last Post: October 30th, 2005, 11:01 PM
-
By Tedob1 in forum Tech Humor
Replies: 0
Last Post: December 23rd, 2002, 04:58 PM
-
By Rna in forum General Programming Questions
Replies: 4
Last Post: May 22nd, 2002, 07:03 AM
-
By thesecretfire in forum Hardware
Replies: 16
Last Post: May 17th, 2002, 12:31 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|