Thread: Beginner User Input Question

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    11

    Question Beginner User Input Question

    I need to verify that all numerical input is positive. If not, output an error message that only positive numbers are valid and reprompt one more time. I have looked around, but am pretty confused. Maybe the while loop, but I just don't understand it well.

    cout <<"The cost of a hybrid car?" << endl;
    cin >> hybrid_cost;

    For example, what would I do after this to meet the above paragraph?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Assuming that your variable is signed, all you have to do is loop while the input isn't positive.
    Devoted my life to programming...

  3. #3
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    The search button will take you for miles and miles

    loop until you've got a non-negative number.
    read value into days
    loop through each character
    if this character is not a digit
    break
    end loop
    end loop
    how to check if input is only positive number?

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    11
    How about this?


    int hybrid_cost = 0;
    do
    {
    cout <<"The cost of a hybrid car?" << endl;
    cin >> hybrid_cost;
    }
    while ( ! ( 0 < hybrid_cost ) );

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by anchorblue804 View Post
    How about this?


    Code:
    int hybrid_cost = 0;
    do
    {
       cout <<"The cost of a hybrid car?" << endl; 
      cin >> hybrid_cost;
    } 
    while ( ! ( 0 < hybrid_cost ) );
    Quite good actually. If you'd add an error message or something it'd be even better. Oh, and do please put your code in code tags, it's more readable that way.
    Devoted my life to programming...

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Well, while ( hybrid_cost < 0 ) ); is much more understandable than while ( ! ( 0 < hybrid_cost ) );
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by WaltP View Post
    Well, while ( hybrid_cost < 0 ) ); is much more understandable than while ( ! ( 0 < hybrid_cost ) );
    There is a nice term for the later, guess what !?!

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by WaltP View Post
    Well, while ( hybrid_cost < 0 ) ); is much more understandable than while ( ! ( 0 < hybrid_cost ) );
    That's possibly true but, more importantly, they mean different things.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Yes, and so does "Make a turn at the next intersection, but not a left turn"
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  10. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by WaltP View Post
    Yes, and so does "Make a turn at the next intersection, but not a left turn"
    !(something > 0) is equivalent to (something <= 0). See, you missed the possible equality. For some, zero isn't positive.
    Devoted my life to programming...

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Details... They always bite you in the ...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about user input validaton
    By TexasKid in forum C Programming
    Replies: 11
    Last Post: 04-27-2012, 05:45 AM
  2. User determined input question
    By lyoncourt in forum C Programming
    Replies: 8
    Last Post: 09-30-2007, 06:10 PM
  3. newbie question regarding user input
    By cantore in forum C Programming
    Replies: 4
    Last Post: 03-05-2006, 08:57 PM
  4. Testing user input question?
    By Hoser83 in forum C Programming
    Replies: 18
    Last Post: 02-15-2006, 01:18 PM
  5. Beginner question- user input termination
    By westm2000 in forum C Programming
    Replies: 3
    Last Post: 12-02-2001, 02:48 PM