Guest User

Untitled

a guest
Nov 21st, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. //*********************************************************************
  2. //* *
  3. //* FILE NAME: gasMileage.cpp Assignment: #1 *
  4. //* *
  5. //* AUTHOR: __________________________ *
  6. //* Tyler Knipfer *
  7. //* *
  8. //* COURSE #: CSC 24400 11 DUE DATE: September 14th, 2012 *
  9. //* *
  10. //*********************************************************************
  11.  
  12. //*************************Program Description*************************
  13. //* *
  14. //* PROCESS: This program is designed to take input from a file that *
  15. //* will then calculate the the gas mileage (mpg) for each time the *
  16. //* driver stops and fills up the tank. *
  17. //* *
  18. //*********************************************************************
  19.  
  20.  
  21.  
  22. #include <iostream>
  23. #include <fstream>
  24. #include <iomanip>
  25.  
  26. using namespace std;
  27.  
  28. void readData(ifstream &readFile, char &model, int &startingDistance);
  29. void printData(ofstream &outFile, int &beginningMileage, int &endingMileage, float &gasUsed, int &distanceDriven, float &mpg, float &totalGasUsed, int &totalDistance, float &totalMPG);
  30. void printHeading(ofstream &outFile, char &model);
  31. void computeMileage(int &beginningMileage, int &endingMileage, int &nextMileage, float &totalGasUsed, float &gasUsed, int &distanceDriven, float &mpg, float &totalMPG, int &totalDistance);
  32. void printHeader(ofstream &outFile);
  33. void printFooter(ofstream &outFile);
  34.  
  35. int main()
  36. {
  37. char model = 0; //The first character in the file that represents the model of car
  38. int startingMileage = 0; //The first odometer reading when starting their trip
  39. float gallonsUsed = 0; //The amount of gas they used after stopping
  40. int mileage = 0; //The mileage after the first odometer reading
  41. int distanceDriven = 0; //used to calculate the distance driven
  42. float mpg = 0; //The mpg that they got after stopping for gas
  43. float totalMPG = 0; //The total mpg for the entire trip
  44. int totalDistance = 0; //The total distance that they have driven including in between stops
  45. int nextMileage = 0;
  46. float totalGasUsed = 0;
  47.  
  48. //File to output the car data to
  49. ofstream outFile("outputFile.txt");
  50.  
  51. //The main input file where all data is coming from
  52. ifstream inFile("data1.txt");
  53.  
  54. //Get the initial model type from the file
  55. inFile >> model;
  56.  
  57. //Get the initail startingMileage from the file
  58. inFile >> startingMileage;
  59.  
  60. printHeader(outFile);
  61.  
  62. printHeading(outFile, model);
  63.  
  64. while(model != 'X')
  65. {
  66. inFile >> mileage;
  67.  
  68. //If the mileage is equal to a negative number then we are done
  69. //with the current car and move to the next one and print it out
  70. if(mileage < 0)
  71. {
  72. //This is the start of a new car so all of the values need to be reset
  73. //to their starting point of zero and then reacalculate everything.
  74. model = 0;
  75. startingMileage = 0;
  76. gallonsUsed = 0;
  77. mileage = 0;
  78. distanceDriven = 0;
  79. mpg = 0;
  80. totalMPG = 0;
  81. totalDistance = 0;
  82. nextMileage = 0;
  83. totalGasUsed = 0;
  84.  
  85. readData(inFile, model, startingMileage);
  86.  
  87. if(model == 'X')
  88. {
  89. break;
  90. }
  91.  
  92. for(int x = 0; x < 50; x++)
  93. {
  94. outFile << endl;
  95. }
  96.  
  97. printHeader(outFile);
  98. printHeading(outFile, model);
  99. continue;
  100. }
  101.  
  102. inFile >> gallonsUsed;
  103.  
  104. computeMileage(startingMileage, mileage, nextMileage, totalGasUsed, gallonsUsed, distanceDriven, mpg, totalMPG, totalDistance);
  105. printData(outFile, startingMileage, mileage, gallonsUsed, distanceDriven, mpg, totalGasUsed, totalDistance, totalMPG);
  106. }
  107.  
  108.  
  109.  
  110. printFooter(outFile);
  111.  
  112. outFile.close();
  113. inFile.close();
  114. return 0;
  115. }
  116.  
  117. void printHeader(ofstream &outFile)
  118. {
  119. //Receives the output file
  120. //Task - Prints the output file
  121. //Return - nothing
  122.  
  123. outFile << setw(32) << "Tyler Knipfer";
  124. outFile << setw(17) << "CSC 24400";
  125. outFile << setw(15) << "Section 11" << endl;
  126. outFile << setw(30) << "Fall 2012";
  127. outFile << setw(20) << "Assignment #1" << endl;
  128. outFile << setw(35) << "---------------------------------------";
  129. outFile << setw(35) << "----------------------------------------\n\n";
  130. return;
  131. }
  132.  
  133. void readData(ifstream &readFile, char &model, int &startingMileage)
  134. {
  135. readFile >> model;
  136. readFile >> startingMileage;
  137. }
  138.  
  139. void printData(ofstream &outFile, int &beginningMileage, int &endingMileage, float &gasUsed, int &distanceDriven, float &mpg, float &totalGasUsed, int &totalDistance, float &totalMPG)
  140. {
  141. //First line of the information table
  142. outFile << left << setw(13) << beginningMileage << left << setw(13) << endingMileage << left << setw(13) << gasUsed;
  143. 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;
  144.  
  145. }
  146.  
  147. void printHeading(ofstream &outFile, char &model)
  148. {
  149. outFile << setw(50) << "TEST DATA" << endl;
  150. outFile << setw(50) << "Car ID - " << model;
  151. outFile << endl;
  152. outFile << setw(43) << "Tank Information";
  153. outFile << setw(30) << "Trip Information" << endl;
  154.  
  155. //First line of the heading table
  156. outFile << left << setw(13) << "Beginning" << left << setw(13) << "Ending" << left << setw(13) << "Amount";
  157. outFile << left << setw(13) << "Leg" << left << setw(13) << "Miles";
  158. outFile << left << setw(13) << "Amount" << left << setw(13) << "Total" << left << setw(13) << "Miles" << endl;
  159.  
  160. //Second line of the heading table
  161. outFile << left << setw(13) << "Odometer" << left << setw(13) << "Odometer" << left << setw(13) << "Of Gas";
  162. outFile << left << setw(13) << "Distance" << left << setw(13) << "per";
  163. outFile << left << setw(13) << "Of Gas" << left << setw(13) << "Distance" << left << setw(13) << "per" << endl;
  164.  
  165. //Third line of the heading table
  166. outFile << left << setw(13) << "Reading" << left << setw(13) << "Odometer" << left << setw(13) << "Used";
  167. outFile << left << setw(13) << "Driven" << left << setw(13) << "Gallon";
  168. outFile << left << setw(13) << "Used" << left << setw(13) << "Driven" << left << setw(13) << "Gallon" << endl;
  169.  
  170. outFile << "============================================================================================" << endl;
  171. }
  172.  
  173. void computeMileage(int &beginningMileage, int &endingMileage, int &nextMileage, float &totalGasUsed, float &gasUsed, int &distanceDriven, float &mpg, float &totalMPG, int &totalDistance)
  174. {
  175. distanceDriven = endingMileage - beginningMileage;
  176.  
  177.  
  178. totalDistance += distanceDriven;
  179. totalGasUsed += gasUsed;
  180. totalMPG = totalDistance / totalGasUsed;
  181. mpg = totalDistance / gasUsed;
  182. }
  183.  
  184. void printFooter(ofstream &outFile)
  185. {
  186. //Receives - the output file
  187. //Task - prints the program footer
  188. //Returns - nothing
  189.  
  190. outFile << endl;
  191. outFile << setw(35) << "-----------------------------------------" << endl;
  192. outFile << setw(35) << "| END OF PROGRAM OUTPUT |" << endl;
  193. outFile << setw(35) << "-----------------------------------------" << endl;
  194.  
  195. return;
  196. }
Add Comment
Please, Sign In to add comment