I'm trying to learn C++, and right now I'm trying to make an idiot proof program (ie: enter in all the wrong values) so I use while loops. But I have a problem. If I input a letter where a number should go, the program enters an infiniteloop.
Here's an exmaple of the code I'm using:
double Water;
cout<<"Enter the amount of water used: ";
cin>>Water;
while (Water<0){
cout<<"Value must be zero, or a positive number"<<endl;
cout<<"Enter the amount of water used (in gallons): ";
cin>>Water;
}
When run and a letter is entered for Water, this is what gets displayed:
Enter the amount of water used: Value must be zero, or a positive number
Enter the amount of water used: Value must be zero, or a positive number
Enter the amount of water used: Value must be zero, or a positive number
etc.
What I should do to prevent the loop from happening?
Thanks
Tipycol
Here's an exmaple of the code I'm using:
double Water;
cout<<"Enter the amount of water used: ";
cin>>Water;
while (Water<0){
cout<<"Value must be zero, or a positive number"<<endl;
cout<<"Enter the amount of water used (in gallons): ";
cin>>Water;
}
When run and a letter is entered for Water, this is what gets displayed:
Enter the amount of water used: Value must be zero, or a positive number
Enter the amount of water used: Value must be zero, or a positive number
Enter the amount of water used: Value must be zero, or a positive number
etc.
What I should do to prevent the loop from happening?
Thanks
Tipycol