CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2009
    Posts
    38

    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.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Input Validation muliples of 100?

    Use the modulus operator (&#37.

    Alternatively, you could divide by 100 and then multiply by 100, and check if the result is the same as the original.

  3. #3
    Join Date
    Oct 2009
    Posts
    38

    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

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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?

  5. #5
    Join Date
    Oct 2009
    Posts
    38

    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?

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Input Validation muliples of 100?

    Quote Originally Posted by Sabensohn70 View Post
    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

  7. #7
    Join Date
    Oct 2009
    Posts
    38

    Re: Input Validation muliples of 100?

    what i mean is how do i combine it to my code?

    Code:
    while ( store1 &#37; 100 != 0 && store1 < 0 || store1 > 4000 )
    i tried that
    that does not work.

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  9. #9
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    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
  •  





Click Here to Expand Forum to Full Width

Featured