hey!
I'm working on a program that part of it asks for an integer input.
then if the value entered is not an integer, it should print an error message. If not, then the rest
of program (which I've got partly complete).
But, I have no idea how to distinguish between an integer and a non-integer value.
I have nothing to post here for that part,cuz i don't know where to start.
the only thing that came to my mind ( and did not work), was the max value for an integer and float, but it turned out to be crap!
any help is appreciated.
megan-smith
0
Newbie Poster
Recommended Answers
Jump to PostIf you're using scanf, you can just test the return value. It returns the number of items that were successfully converted:
int x; if ( scanf ( "%d", &x ) != 1 ) fprintf ( stderr, "Invalid input\n" );
The format string only has one specifier (%d), …
Jump to PostSomething like this, I suppose:
int GetIntInput(int min, int max) { printf("Please enter an integer:\n"); if (scanf ("%i", &val) != 1) { printf ( "Invalid input! Please enter your value again:\n" ); } else { if(ValueInRange( val, min, max)) { return val; } else { printf("This number …
Jump to Postany ideas?
Yes, don't use scanf. Link here.
All 12 Replies
Ptolemy
33
Junior Poster in Training
megan-smith
0
Newbie Poster
Ptolemy
33
Junior Poster in Training
megan-smith
0
Newbie Poster
Aia
1,977
Nearly a Posting Maven
megan-smith
0
Newbie Poster
megan-smith
0
Newbie Poster
Aia
1,977
Nearly a Posting Maven
Ptolemy
33
Junior Poster in Training
Aia
1,977
Nearly a Posting Maven
WaltP
2,905
Posting Sage w/ dash of thyme
Team Colleague
WaltP
2,905
Posting Sage w/ dash of thyme
Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.