when it asks for the speed limit, it asks twice, and I'm unsure of how to fix that. Secondly, when I enter something outside of the input validation parameters, I get an endless loop error
So I have this code, and everything runs fine when I'm within the parameters of the input validation. But, I am having two issues with this code. First off, when it asks for the speed limit, it asks twice, and I'm unsure of how to fix that. Secondly, when I enter something outside of the input validation parameters, I get an endless loop error. Any advice as to how to fix these problems? I'm not a c++ expert, I'm rather new at the whole thing, so help would be greatly appreciated
#include <iostream>
#include <string>
float getSpeed(double speedLimit, double speed, double calcSpeed, double overSpeed);
using namespace std;
const int lowSpeed = 20;
const int highSpeed = 70;
int main()
{
double speedLimit, speed, calcSpeed, overSpeed();
{
cout << "Enter the speed limit: " ;
cin >>speedLimit;
}
while (speedLimit <=lowSpeed || speedLimit >= highSpeed)
cout << "ERROR: the speed limit must be between, 20 mph and 70 mph." << endl;
cout << "Enter the speed limit." <<endl;
cin >>speedLimit;
cout << "Enter your speed:"<< endl;
cin >> speed;
calcSpeed = speed - speedLimit;
cout << "You were going " ;
cout <<calcSpeed;
cout <<"mph over the speed limit.";
return 0;}
So here what I understood is, You are trying to check a speed limit is between the two speeds. And if yes, then print the message like "You were going 40mph within the speed limit.", Else print the warning message.
So in the below C++ program I am expecting 3 inputs like Minimum speed limit, Maximum limit And speed. After the input reading, I am comparing the min and max values with the speed and printing the corresponding messages
Step by step
Solved in 3 steps with 3 images