Beginner User Input Question

closed account (16MjE3v7)
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?
First of all you can define the type of the entered object as unsigned.

Otherwise you can write

1
2
3
4
5
6
7
int hybrid_cost = 0;

do
{
   cout <<"The cost of a hybrid car?" << endl;
   cin >> hybrid_cost;
} while ( ! ( 0 < hybrid_cost ) );  
Last edited on
Topic archived. No new replies allowed.