Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //*********************************************************************
- //* *
- //* FILE NAME: gasMileage.cpp Assignment: #1 *
- //* *
- //* AUTHOR: __________________________ *
- //* Tyler Knipfer *
- //* *
- //* COURSE #: CSC 24400 11 DUE DATE: September 14th, 2012 *
- //* *
- //*********************************************************************
- //*************************Program Description*************************
- //* *
- //* PROCESS: This program is designed to take input from a file that *
- //* will then calculate the the gas mileage (mpg) for each time the *
- //* driver stops and fills up the tank. *
- //* *
- //*********************************************************************
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- using namespace std;
- void readData(ifstream &readFile, char &model, int &startingDistance);
- void printData(ofstream &outFile, int &beginningMileage, int &endingMileage, float &gasUsed, int &distanceDriven, float &mpg, float &totalGasUsed, int &totalDistance, float &totalMPG);
- void printHeading(ofstream &outFile, char &model);
- void computeMileage(int &beginningMileage, int &endingMileage, int &nextMileage, float &totalGasUsed, float &gasUsed, int &distanceDriven, float &mpg, float &totalMPG, int &totalDistance);
- void printHeader(ofstream &outFile);
- void printFooter(ofstream &outFile);
- int main()
- {
- char model = 0; //The first character in the file that represents the model of car
- int startingMileage = 0; //The first odometer reading when starting their trip
- float gallonsUsed = 0; //The amount of gas they used after stopping
- int mileage = 0; //The mileage after the first odometer reading
- int distanceDriven = 0; //used to calculate the distance driven
- float mpg = 0; //The mpg that they got after stopping for gas
- float totalMPG = 0; //The total mpg for the entire trip
- int totalDistance = 0; //The total distance that they have driven including in between stops
- int nextMileage = 0;
- float totalGasUsed = 0;
- //File to output the car data to
- ofstream outFile("outputFile.txt");
- //The main input file where all data is coming from
- ifstream inFile("data1.txt");
- //Get the initial model type from the file
- inFile >> model;
- //Get the initail startingMileage from the file
- inFile >> startingMileage;
- printHeader(outFile);
- printHeading(outFile, model);
- while(model != 'X')
- {
- inFile >> mileage;
- //If the mileage is equal to a negative number then we are done
- //with the current car and move to the next one and print it out
- if(mileage < 0)
- {
- //This is the start of a new car so all of the values need to be reset
- //to their starting point of zero and then reacalculate everything.
- model = 0;
- startingMileage = 0;
- gallonsUsed = 0;
- mileage = 0;
- distanceDriven = 0;
- mpg = 0;
- totalMPG = 0;
- totalDistance = 0;
- nextMileage = 0;
- totalGasUsed = 0;
- readData(inFile, model, startingMileage);
- if(model == 'X')
- {
- break;
- }
- for(int x = 0; x < 50; x++)
- {
- outFile << endl;
- }
- printHeader(outFile);
- printHeading(outFile, model);
- continue;
- }
- inFile >> gallonsUsed;
- computeMileage(startingMileage, mileage, nextMileage, totalGasUsed, gallonsUsed, distanceDriven, mpg, totalMPG, totalDistance);
- printData(outFile, startingMileage, mileage, gallonsUsed, distanceDriven, mpg, totalGasUsed, totalDistance, totalMPG);
- }
- printFooter(outFile);
- outFile.close();
- inFile.close();
- return 0;
- }
- void printHeader(ofstream &outFile)
- {
- //Receives the output file
- //Task - Prints the output file
- //Return - nothing
- outFile << setw(32) << "Tyler Knipfer";
- outFile << setw(17) << "CSC 24400";
- outFile << setw(15) << "Section 11" << endl;
- outFile << setw(30) << "Fall 2012";
- outFile << setw(20) << "Assignment #1" << endl;
- outFile << setw(35) << "---------------------------------------";
- outFile << setw(35) << "----------------------------------------\n\n";
- return;
- }
- void readData(ifstream &readFile, char &model, int &startingMileage)
- {
- readFile >> model;
- readFile >> startingMileage;
- }
- void printData(ofstream &outFile, int &beginningMileage, int &endingMileage, float &gasUsed, int &distanceDriven, float &mpg, float &totalGasUsed, int &totalDistance, float &totalMPG)
- {
- //First line of the information table
- outFile << left << setw(13) << beginningMileage << left << setw(13) << endingMileage << left << setw(13) << gasUsed;
- outFile << left << setw(13) << distanceDriven << left << setw(13) << fixed << setprecision(2) << mpg << left << setw(13) << totalGasUsed << left << setw(13) << totalDistance << left << setw(13) << totalMPG << endl;
- }
- void printHeading(ofstream &outFile, char &model)
- {
- outFile << setw(50) << "TEST DATA" << endl;
- outFile << setw(50) << "Car ID - " << model;
- outFile << endl;
- outFile << setw(43) << "Tank Information";
- outFile << setw(30) << "Trip Information" << endl;
- //First line of the heading table
- outFile << left << setw(13) << "Beginning" << left << setw(13) << "Ending" << left << setw(13) << "Amount";
- outFile << left << setw(13) << "Leg" << left << setw(13) << "Miles";
- outFile << left << setw(13) << "Amount" << left << setw(13) << "Total" << left << setw(13) << "Miles" << endl;
- //Second line of the heading table
- outFile << left << setw(13) << "Odometer" << left << setw(13) << "Odometer" << left << setw(13) << "Of Gas";
- outFile << left << setw(13) << "Distance" << left << setw(13) << "per";
- outFile << left << setw(13) << "Of Gas" << left << setw(13) << "Distance" << left << setw(13) << "per" << endl;
- //Third line of the heading table
- outFile << left << setw(13) << "Reading" << left << setw(13) << "Odometer" << left << setw(13) << "Used";
- outFile << left << setw(13) << "Driven" << left << setw(13) << "Gallon";
- outFile << left << setw(13) << "Used" << left << setw(13) << "Driven" << left << setw(13) << "Gallon" << endl;
- outFile << "============================================================================================" << endl;
- }
- void computeMileage(int &beginningMileage, int &endingMileage, int &nextMileage, float &totalGasUsed, float &gasUsed, int &distanceDriven, float &mpg, float &totalMPG, int &totalDistance)
- {
- distanceDriven = endingMileage - beginningMileage;
- totalDistance += distanceDriven;
- totalGasUsed += gasUsed;
- totalMPG = totalDistance / totalGasUsed;
- mpg = totalDistance / gasUsed;
- }
- void printFooter(ofstream &outFile)
- {
- //Receives - the output file
- //Task - prints the program footer
- //Returns - nothing
- outFile << endl;
- outFile << setw(35) << "-----------------------------------------" << endl;
- outFile << setw(35) << "| END OF PROGRAM OUTPUT |" << endl;
- outFile << setw(35) << "-----------------------------------------" << endl;
- return;
- }
Add Comment
Please, Sign In to add comment