the user is done the program should calculate the MPG by calling GetMPG, catching the exception if the user did not enter any values. Then it should show the result. Stream Errors cout

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

When the user is done the program should calculate the MPG by calling GetMPG, catching the exception if the user did not enter any values. Then it should show the result. Stream Errors cout

 

d. The main program should loop and get the Gallons and Miles catching any exceptions
that were thrown. Then ask if they want to enter another tank. If they enter gallons
and miles correctly, put the values into the vectors given. When the user is done the
program should calculate the MPG by calling GetMPG, catching the exception if the user
did not enter any values. Then it should show the result.
Stream Errors
cout <« "Enter a number:
<« endl;
cin >> number;
if (cin.fail (0) {
// Clear error state
cin.clear ();
// Ignore characters in stream until newline
cin.ignore (numeric_limits<streamsize>: :max (), '\n');
cout << "There was an error:
« endl;
Throwing Errors
throw runtime_error ("Invalid value.");
Catching Errors
try {
// Code to try
catch (runtime_error sexcpt) {
// Prints the error message passed by throw statement
cout « excpt.what () <« endl;
}
Make sure you include stdexcept and vector as well as the other standard modules.
Transcribed Image Text:d. The main program should loop and get the Gallons and Miles catching any exceptions that were thrown. Then ask if they want to enter another tank. If they enter gallons and miles correctly, put the values into the vectors given. When the user is done the program should calculate the MPG by calling GetMPG, catching the exception if the user did not enter any values. Then it should show the result. Stream Errors cout <« "Enter a number: <« endl; cin >> number; if (cin.fail (0) { // Clear error state cin.clear (); // Ignore characters in stream until newline cin.ignore (numeric_limits<streamsize>: :max (), '\n'); cout << "There was an error: « endl; Throwing Errors throw runtime_error ("Invalid value."); Catching Errors try { // Code to try catch (runtime_error sexcpt) { // Prints the error message passed by throw statement cout « excpt.what () <« endl; } Make sure you include stdexcept and vector as well as the other standard modules.
1. Calculating fuel economy. This program will use exceptions and stream errors to make a robust
application that gets the number of miles and gallons each time the user fuels their car. It will
put those values into vectors. Once the user wants to quit enter values, it will calculate the fuel
economy.
a. Create GetMiles() function that returns double. It should prompt the user via cout, and
read the result from cin.
i. If there is a stream error, then throw a runtime_error with the message Invalid
input received, you must enter a decimal number. Don't forget to clear the error
and ignore all characters until the end of the stream.
ii. If the value is less then or equal to zero, then throw a runtime_error with the
message. Miles cannot be less than 0.
ii. Otherwise, return the miles the user entered
b. Create GetGallons() function that returns double; again, the prompt is printed to cout,
and the user input entered from cin.
i. If there is a stream error, then throwa runtime_error with the message Invalid
input received, you must enter a decimal number. Don't forget to clear the error
and ignore all characters until the end of the stream.
ii. If the value is less than or equal to zero, then throw a runtime_error with the
Pro
message. Gallons cannot be less than 0.
ii. Otherwise, return the miles the user entered
c. Create GetMPG(vector<double miles, vector<double> gallons) function that returns a
double.
i. If the size of the vectors is 0, then throw a runtime_error with the message No
values recorded MPG is nan.
1. ('nan' stands for not-a-number; you can use the full phrase if you'd
rather.)
ii. Otherwise, total the miles and gallons and return the miles per gallon.
Transcribed Image Text:1. Calculating fuel economy. This program will use exceptions and stream errors to make a robust application that gets the number of miles and gallons each time the user fuels their car. It will put those values into vectors. Once the user wants to quit enter values, it will calculate the fuel economy. a. Create GetMiles() function that returns double. It should prompt the user via cout, and read the result from cin. i. If there is a stream error, then throw a runtime_error with the message Invalid input received, you must enter a decimal number. Don't forget to clear the error and ignore all characters until the end of the stream. ii. If the value is less then or equal to zero, then throw a runtime_error with the message. Miles cannot be less than 0. ii. Otherwise, return the miles the user entered b. Create GetGallons() function that returns double; again, the prompt is printed to cout, and the user input entered from cin. i. If there is a stream error, then throwa runtime_error with the message Invalid input received, you must enter a decimal number. Don't forget to clear the error and ignore all characters until the end of the stream. ii. If the value is less than or equal to zero, then throw a runtime_error with the Pro message. Gallons cannot be less than 0. ii. Otherwise, return the miles the user entered c. Create GetMPG(vector<double miles, vector<double> gallons) function that returns a double. i. If the size of the vectors is 0, then throw a runtime_error with the message No values recorded MPG is nan. 1. ('nan' stands for not-a-number; you can use the full phrase if you'd rather.) ii. Otherwise, total the miles and gallons and return the miles per gallon.
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY