Jump to content

Validating Input

- - - - -

  • Please log in to reply
7 replies to this topic

#1
NorCalAirman

NorCalAirman

    Newbie

  • Members
  • Pip
  • 5 posts
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.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
I believe the most simple way would be this:
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.

#3
prajmus

prajmus

    Newbie

  • Members
  • PipPip
  • 13 posts
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
NorCalAirman

NorCalAirman

    Newbie

  • Members
  • Pip
  • 5 posts
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.

#5
prajmus

prajmus

    Newbie

  • Members
  • PipPip
  • 13 posts
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.
Nothing is impossible ... the impossible just takes longer ;)

#6
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
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
Wow I changed my sig!

#7
NorCalAirman

NorCalAirman

    Newbie

  • Members
  • Pip
  • 5 posts
Thanks, guys. This helps a lot.

#8
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
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.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users