In: Computer Science
IN C++ PLEASE. Use ONLY: exception handling, read and write files, arrays, vectors, functions, headers and other files, loops, conditionals, data types, assignment.
Stream Errors
cout << "Enter a number: " << endl;
cin >> number;
if (cin.fail()) {
// 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 &excpt) {
// 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.
// C++ program to calculate Fuel Economy
#include <iostream>
#include <stdexcept>
#include <limits>
using namespace std;
// function declaration
double GetMiles();
double GetGallons();
double GetMPG(vector<double> miles, vector<double>
gallons);
int main()
{
vector<double> miles, gallons;
double in_miles, in_gallons;
char res;
// loop to input the miles and gallons and insert it into the
vectors till the user wants
do
{
try
{
// input miles and gallons
in_miles = GetMiles();
in_gallons = GetGallons();
// insert the input into vectors
miles.push_back(in_miles);
gallons.push_back(in_gallons);
}catch(runtime_error &e)
{
// catch any exception raised
cout<<e.what()<<endl;
}
// ask if the user wants to enter another data
cout<<"Do you want to enter another tank (y/n)? ";
cin>>res;
}while(res == 'y' || res == 'Y');
// calculate and display the MPG
try{
cout<<endl<<"MPG: "<<GetMPG(miles,
gallons);
}catch(runtime_error &e)
{
cout<<e.what()<<endl;
}
return 0;
}
// function that inputs miles from user and return it
double GetMiles()
{
double miles;
// input miles
cout<<"Enter the miles: ";
cin>>miles;
// check if there is any stream error
if(cin.fail())
{
cin.clear(); // clear the error
cin.ignore(numeric_limits<streamsize>::max(),'\n'); // ignore
the characters until the end of line
throw runtime_error("Invalid input received, you must enter a
decimal number.");
}else if(miles <= 0) // check if miles is less than or equal to
0
throw runtime_error("Miles cannot be less than 0");
return miles;
}
// function that inputs gallons from user and return it
double GetGallons()
{
double gallons;
// input gallons
cout<<"Enter the gallons: ";
cin>>gallons;
// check if there is any stream error
if(cin.fail())
{
cin.clear(); // clear the error
cin.ignore(numeric_limits<streamsize>::max(),'\n'); // ignore
the characters until the end of line
throw runtime_error("Invalid input received, you must enter a
decimal number.");
}else if(gallons <= 0) // check if gallons is less than or equal
to 0
throw runtime_error("Gallons cannot be less than 0");
return gallons;
}
// function to calculate and return the miles per gallon
(MPG)
double GetMPG(vector<double> miles, vector<double>
gallons)
{
// check if the vectors contain any value
if(miles.size() == 0)
{
throw runtime_error("No values recorded MPG is nan");
}
// calculate the total miles and total gallons
double total_miles =0 , total_gallons = 0;
for(size_t i=0;i<miles.size();i++)
{
total_miles += miles[i];
total_gallons += gallons[i];
}
// calculate and return the MPG
return total_miles/total_gallons;
}
// end of program
Output: