Question
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and other files, loops, conditionals, data types, assignment. Calculating fuel
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and other files, loops, conditionals, data types, assignment.
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.
Create GetMiles() function that returns double.
If there is a stream error, then throw a runtime_error with the message Invalid input received, you must enter a decimal number. Dont forget to clear the error and ignore all characters until the end of the stream.
If the value is less then or equal to zero, then throw a runtime_error with the message. Miles cannot be less than 0.
Otherwise, return the miles the user entered
Create GetGallons() function that returns double.
If there is a stream error, then throw a runtime_error with the message Invalid input received, you must enter a decimal number. Dont forget to clear the error and ignore all characters until the end of the stream.
If the value is less than or equal to zero, then throw a runtime_error with the message. Gallons cannot be less than 0.
Otherwise, return the miles the user entered
Create GetMPG(vector gallons) function that returns a double.
If the size of the vectors is 0, then throw a runtime_error with the message No values recorded MPG is nan
Otherwise, total the miles and gallons and return the miles per gallon.
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 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
cin >> number;
if (cin.fail()) {
// Clear error state
cin.clear();
// Ignore characters in stream until newline
cin.ignore(numeric_limits::max(), ' ');
cout
}
Throwing Errors
throw runtime_error(\"Invalid value.\");
Catching Errors
try {
// Code to try
}
catch (runtime_error &excpt) {
// Prints the error message passed by throw statement
cout
}
Make sure you include stdexcept and vector as well as the other standard modules.
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