Question: Intro to C++ move the code that gets the input for the miles driven and gallons used into header and implementation files (see page 270)

Intro to C++

move the code that gets the input for the miles driven and gallons used into header and implementation files (see page 270) that includes functions called get_miles_driven() and get_gallons_used(). You can find an example book app that uses a header and implementation file in book_apps/ch07d_future_value/. So for this exercise, you should submit three files. The submit two .ccp files (main.cpp & the implementation file) and one header file.

Also, create a hierarchy outline (see pg. 249) for the program and place it in the comments section at the top of the app.

#include #include #include #include

using namespace std;

double calculate_mpg(double miles, double gallons);

void display_file_data();

int main() { // print name of program cout << "Miles Per Gallon ";

// define variables double miles; double gallons; double mpg;

// print existing file data display_file_data();

char another_entry = 'y'; while (tolower(another_entry) == 'y') {

// get miles driven miles = 0; while (true) { cout << "Enter miles driven: "; cin >> miles; cin.ignore(numeric_limits::max(), ' '); if (miles <= 0) { cout << "Miles driven must be greater than zero. Please try again. "; continue; } else { break; } }

// get gallons of gas used gallons = 0; while (true) { cout << "Enter gallons of gas used: "; cin >> gallons; cin.ignore(numeric_limits::max(), ' ');

if (gallons <= 0) { cout << "Gallons used must be greater than zero. Please try again. "; continue; } else { break; } }

// calculate and display miles per gallon mpg = calculate_mpg(miles, gallons); cout << "Miles per gallon: " << mpg << endl << endl;

// write entry to file ofstream outfile; outfile.open("trips.txt", ios::app); outfile << fixed << setprecision(1); outfile << miles << '\t' << gallons << ' '; outfile.close();

// print file data display_file_data();

// see if the user wants to enter more data cout << "Get entries for another trip? (y/n): "; cin >> another_entry; cin.ignore(numeric_limits::max(), ' ');

cout << endl; }

cout << "Bye! "; }

double calculate_mpg(double miles, double gallons) { double mpg = miles / gallons; mpg = round(mpg * 100) / 100; return mpg; }

void display_file_data() { // set a full path to the correct file const char* home = getenv("HOME"); string user_home = ""; if (home) { user_home = home; } else { // if home isn't found, edit 'username' so it's correct for your system user_home = "/Users/username"; } string file_path = "/Documents/murach/cpp/files/"; string filename = user_home + file_path + "trips.txt";

ifstream infile; infile.open(filename); if (infile) { double miles; double gallons; double total_miles = 0; double total_gallons = 0; cout << setw(8) << "Miles" << setw(12) << "Gallons" << setw(12) << "MPG" << endl; while (infile >> miles >> gallons) { total_miles += miles; total_gallons += gallons; double mpg = calculate_mpg(miles, gallons); cout << fixed << setprecision(1); cout << setw(8) << miles << setw(12) << gallons; cout << fixed << setprecision(2); cout << setw(12) << mpg << endl; } infile.close();

double avg_mpg = total_miles / total_gallons;

cout << fixed << setprecision(2) << showpoint << endl; cout << left << setw(15) << "Total miles:" << right << setw(10) << total_miles << endl; cout << left << setw(15) << "Total gallons:" << right << setw(10) << total_gallons << endl; cout << left << setw(15) << "Average MPG:" << right << setw(10) << avg_mpg << endl; cout << endl; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!