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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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;}

 

Expert Solution
Step 1

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

steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
Math class and its different methods
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education