I wrote a subnet calculator that works great, but if the user enters a letter instead of a number in one of the octets, how can I tell them it's invalid? I already have it set to limit them to 0-255, but if they use a letter, it accepts the input and just leaves it as a zero (which I declared to be the default values). Some way to prevent incorrect input or display an error if a letter is typed is all I need.
7 replies to this topic
#1
Posted 18 May 2011 - 02:01 PM
|
|
|
#2
Posted 18 May 2011 - 02:07 PM
I believe the most simple way would be this:
You could improve the safety of this by reading the whole buffer with fgets, and then using sscanf to scan the retrieved line for tokens rather than reading directly from stdin with scanf. This is not required, it can however prevent unwanted garbage remaining in stdin on next read (i.e. newlines or invalid characters)
if( scanf("%i", &i) != 1 ) {
puts("Invalid input");
//...
}You could place this in to a while loop, until the scanf statement returns 1 signifying one token (of type "%i") has successfully been parsed.You could improve the safety of this by reading the whole buffer with fgets, and then using sscanf to scan the retrieved line for tokens rather than reading directly from stdin with scanf. This is not required, it can however prevent unwanted garbage remaining in stdin on next read (i.e. newlines or invalid characters)
Edited by Alexander, 18 May 2011 - 02:11 PM.
Information
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#3
Posted 18 May 2011 - 02:08 PM
Are you writing in C or C++? You should basicly check the state of input stream.
Nothing is impossible ... the impossible just takes longer ;)
#4
Posted 18 May 2011 - 02:19 PM
What exactly is that first line doing? It works, I compiled it and it does what I want, I just want to understand it. First, I didn't know you could put "scanf" inside the parenthesis after an "if". Second, what is "!= 1" doing? Isn't that just saying "is not equal to 1?" What am I missing?
Also, what is "puts?" Is that the same as "printf?" That's all we've been taught so far.
Also, what is "puts?" Is that the same as "printf?" That's all we've been taught so far.
#5
Posted 18 May 2011 - 03:08 PM
scanf() is a function and it's got a return value int. It returns a number of read variables, so if it's not at least 1 that means it hasn't read anything.
puts() is just for strings.
puts() is just for strings.
Nothing is impossible ... the impossible just takes longer ;)
#6
Posted 18 May 2011 - 03:11 PM
scanf returns the number of correctly read symbols from it's read operation back as an integer, and you can use that to compare with a value. Since scanf was only asked for one value, success would be indicated with a "1" returned, so if it does not equal 1, it must be wrong.
puts is something of a simpler way to enter non-formatted text to the standard output stream. The printf function is primarily used to include format specifiers and arguments, so you can format other data types to the output stream.
EDIT: Looks like prajmus got it first. XD
puts is something of a simpler way to enter non-formatted text to the standard output stream. The printf function is primarily used to include format specifiers and arguments, so you can format other data types to the output stream.
EDIT: Looks like prajmus got it first. XD
Wow I changed my sig!
#7
Posted 18 May 2011 - 03:20 PM
Thanks, guys. This helps a lot.
#8
Posted 18 May 2011 - 04:29 PM
puts() also appends a newline at the end of the sentence, it is a lot more nice to see puts("foobar"); than printf("foobar\n");
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









