Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Intro to C++ allow the user to add the make and model of the car they drove on the trip and display this along with

Intro to C++

allow the user to add the make and model of the car they drove on the trip and display this along with the data as explained in Ex 5-2. Exercise 2 is modifying exercise 1, so you only need to submit a single cpp file for this exercise. The output & formatting in trips.txt should look similar to this:

Miles Gallons Make Model

800.0 10.0 Chevy Volt

400.0 8.0 Honda CRX

356.0 10.0 GMC Denali

#include #include #include #include

using namespace std;

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

// define variables double miles; double gallons; double mpg; double total_miles; double total_gallons; double avg_mpg;

// 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";

// print existing file data ifstream infile; infile.open(filename); if (infile) { total_miles = 0; total_gallons = 0; cout << setw(8) << "Miles" << setw(12) << "Gallons" << setw(12) << "MPG" << endl; while (infile >> miles >> gallons) { total_miles += miles; total_gallons += gallons; mpg = miles / gallons; mpg = round(mpg * 100) / 100; cout << fixed << setprecision(1); cout << setw(8) << miles << setw(12) << gallons; cout << fixed << setprecision(2); cout << setw(12) << mpg << endl; } infile.close();

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

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 = miles / gallons; mpg = round(mpg * 100) / 100; 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 infile.open("trips.txt"); if (infile) { total_miles = 0; total_gallons = 0; cout << setw(8) << "Miles" << setw(12) << "Gallons" << setw(12) << "MPG" << endl; while (infile >> miles >> gallons) { total_miles += miles; total_gallons += gallons; mpg = miles / gallons; mpg = round(mpg * 100) / 100; cout << fixed << setprecision(1); cout << setw(8) << miles << setw(12) << gallons; cout << fixed << setprecision(2); cout << setw(12) << mpg << endl; } infile.close();

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

// 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! "; }

trips.txt:

225.2 17.0 1374.8 64.5 274.0 18.5

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 11g SQL

Authors: Jason Price

1st Edition

0071498508, 978-0071498500

More Books

Students also viewed these Databases questions