Question
/*Help fixing ERRORS: 1. The entire program must loop giving the user a chance to enter another vehicle. 2. If I enter characters, You go
/*Help fixing ERRORS:
1. The entire program must loop giving the user a chance to enter another vehicle.
2. If I enter characters, You go into an endless loop.
*/
#include
#include
#include
using namespace std;
int main(){
float speed, hours;
double distanceTravelled;
/// Speed
do {
cout << "Please enter the speed of the vehicle in mph?: ";
cin >> speed;
/// loop until they put valid speed, no negatives
if (speed < 0)
cout << "Please enter positive value for speed, no negatives allowed: " << endl;
} while (speed < 0);
/// Hours Travelled
do {
cout << " How many hours has the vehicle travelled? : ";
cin >> hours ;
/// loop until they enter valid time above 1 hour
if (hours < 1)
cout << "Please enter valid hours higher than 1 : " << endl;
} while (hours < 1);
distanceTravelled = speed * hours;
cout << "Hours" << "\t" << "distanceTravelled" << endl;
cout << "-----------------------" << endl;
for (int i = 1; i <= hours; i++)
{
cout << " " << i << "\t\t" << speed * i << endl;
}
return 0;
}
/*
Please enter the speed of the vehicle in mph?: -1
Please enter positive value for speed, no negatives allowed:
Please enter the speed of the vehicle in mph?: 40
How many hours has the vehicle travelled? : 0
Please enter valid hours higher than 1 :
How many hours has the vehicle travelled? : 3
Hours distanceTravelled
-----------------------
1 40
2 80
3 120
*/
/*
Please enter the speed of the vehicle in mph?: -2
Please enter positive value for speed, no negatives allowed:
Please enter the speed of the vehicle in mph?: 600
How many hours has the vehicle travelled? : 4
Hours distanceTravelled
-----------------------
1 600
2 1200
3 1800
4 2400
*/
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started