Like Tree4Likes
  • 2 Post By WaltP
  • 2 Post By WaltP

Beginner User Input Question

This is a discussion on Beginner User Input Question within the C++ Programming forums, part of the General Programming Boards category; I need to verify that all numerical input is positive. If not, output an error message that only positive numbers ...

  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
    1,625
    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
    RoD
    RoD is offline
    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
    1,625
    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,161
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    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 !?!
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,673
    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, Sunshine, and read this, this, and this before posting again.

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    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
    1,625
    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,161
    Details... They always bite you in the ...
    GReaper and laserlight like this.
    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, 06:45 AM
  2. User determined input question
    By lyoncourt in forum C Programming
    Replies: 8
    Last Post: 09-30-2007, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21