-
November 17th, 2009, 05:36 PM
#1
Input Validation muliples of 100?
I am suppose to write a code to validate that the users input is between 0-4000 and are multiples of 100. How do i write a code to ensure that the cin is multiples of 100? So far i have
Code:
cin >> store1;
if ( store1 < 0 || store1 > 4000 )
{
cout << " Invalid entry ";
I just dont know how to make it so the program knows whether the cin is multiples of 100 or not.
-
November 17th, 2009, 05:38 PM
#2
Re: Input Validation muliples of 100?
Use the modulus operator (%.
Alternatively, you could divide by 100 and then multiply by 100, and check if the result is the same as the original.
-
November 17th, 2009, 05:48 PM
#3
Re: Input Validation muliples of 100?
how would the modulus operator work? isn't the function for the operator to show the remainder of a division problem
-
November 17th, 2009, 05:52 PM
#4
Re: Input Validation muliples of 100?
And if the value is a multiple of 100, then the remainder when dividing by 100 will be....
You should also consider whether or not you're required to check that the user's input really was an integer. If they enter "apple", can your code handle that?
-
November 17th, 2009, 05:59 PM
#5
Re: Input Validation muliples of 100?
well i'ts just a simple program assignment and my teacher has already given us a set of inputs. Could you give me an example of using the modulus operator for the multiple of 100?
-
November 17th, 2009, 06:18 PM
#6
Re: Input Validation muliples of 100?
Originally Posted by Sabensohn70
well i'ts just a simple program assignment and my teacher has already given us a set of inputs. Could you give me an example of using the modulus operator for the multiple of 100?
Google is your friend
-
November 17th, 2009, 06:28 PM
#7
Re: Input Validation muliples of 100?
what i mean is how do i combine it to my code?
Code:
while ( store1 % 100 != 0 && store1 < 0 || store1 > 4000 )
i tried that
that does not work.
-
November 17th, 2009, 06:38 PM
#8
Re: Input Validation muliples of 100?
Operator precedence issue, I suspect. Normally && has higher precedence than ||, but I think you intend the opposite to be true here. Throw on some parentheses to fix it.
-
November 18th, 2009, 03:17 AM
#9
Re: Input Validation muliples of 100?
This should enlighten you:
http://www.parashift.com/c++-faq-lit....html#faq-15.3
Read it, learn it. It might be a little advanced, but you don't need to understand every underlying mechanism, just how to use it.
Also, if this is for school, I recommend you replace "std::numeric_limits<std::streamsize>::max()" with "100", or your teacher might tell you you "cheated" (whatever that means).
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
|
Click Here to Expand Forum to Full Width
|