I am wondering how I can make the while loop work (In C++). It is supposed to ke
ID: 3748737 • Letter: I
Question
I am wondering how I can make the while loop work (In C++). It is supposed to keep asking to "Enter car's miles" until the input is valid, but right now if I enter in a wrong value then the system goes crazy and infinitely repeats "Enter car's miles"
if (vehicle=="car") { //input is a car
cout << "Enter car's miles: ";
cin >> entry;
while (!entry) {
cout << "Enter car's miles: ";
cin >> entry;
}
car_miles = car_miles+entry;
cout << "Enter car's gallons: ";
cin >> entry;
while (!entry) {
cout << "Enter car's gallons: ";
cin >> entry;
}
car_gallons = car_gallons+entry;
Explanation / Answer
while (!(cin >> entry)) { cin.clear(); cin.ignore(1024, ' '); cout entry)) { cin.clear(); cin.ignore(1024, ' '); coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.