Pulling best/worst data from a file
Jun 16, 2018 at 9:41pm UTC
everything in my code is working as it needs to, except the best and worst MPG.
two issues are happening:
1: the best/worst MPG is not giving the correct #
2: the vehicle tag number associated with the best and worse is off.
I also include the print outs I'm getting. Thanks for any help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
ifstream inFile;
string vehicleID, best, worst;
int count = 0; // number of lines of vehicle data
double miles, gallons, aveMPG;
double totalMil = 0, totalMPG = 0, bestMPG = 0, worstMPG = 99999;
cout << fixed << setprecision(1);
cout << "AMSCO Fleet Report \n"
<< "Vehicle ID" << " " << "Miles" << " " << "Gallons" << " " << "MPG" << endl;
cout << "-------------------------------------------------- \n" ;
inFile.open("FleetInput.txt" ); // opening file "fleetinput.txt"
if (inFile)
{
while (inFile >> vehicleID >> miles >> gallons) // pulling the three columns in the file.
{
double MPG = miles / gallons;
cout << vehicleID << " " << miles << " " << setw(5) << gallons << " " << MPG << endl;
count++;
totalMil += miles++;
totalMPG += MPG++;
aveMPG = totalMPG / count;
if (MPG > bestMPG)
{
bestMPG = MPG;
best = vehicleID;
}
else if (MPG < worstMPG)
worstMPG = MPG;
worst = vehicleID;
}
inFile.close();
cout << "-------------------------------------------------- \n" ;
cout << "Count:" << "\t\t" << count << endl;
cout << "Total Miles:" << "\t" << totalMil << endl;
cout << "Average MPG:" << "\t" << aveMPG << endl;
cout << "Vehicle ID with Best MPG:" << "\t" << best << " " << bestMPG << endl;
cout << "Vehicle ID with Worst MPG:" << "\t" << worst << " " << worstMPG << endl;
}
else
{
cout << "Failed to load \n" ;
}
return 0;
}
/*
AMSCO Fleet Report
Vehicle ID Miles Gallons MPG
--------------------------------------------------
279BX6KY2Z 582.2 20.5 28.4
802MX8BZ4Q 723.6 25.1 28.8
671AY6DZ3N 756.9 27.5 27.5
845DH6AJ3B 660.2 25.4 26.0
566GJ6KV2U 510.2 30.8 16.6
109QW6RP2Y 301.2 8.3 36.3
135TR4YC6H 387.6 12.2 31.8
--------------------------------------------------
Count: 7
Total Miles: 3921.9
Average MPG: 27.9
Vehicle ID with Best MPG: 109QW6RP2Y 37.3
Vehicle ID with Worst MPG: 135TR4YC6H 17.6
Press any key to continue . . .
*/
Jun 16, 2018 at 9:44pm UTC
1 2 3
else if (MPG < worstMPG)
worstMPG = MPG;
worst = vehicleID;
is the same as
1 2 3 4 5
else if (MPG < worstMPG)
{
worstMPG = MPG;
}
worst = vehicleID;
See the problem?
Now, look at where you calculate MPG. Look at where you store that value as the best (or worst) MPG. Look between those two points and see what you're doing to the MPG. I have no idea why you're changing MPG after you calculate it.
Last edited on Jun 16, 2018 at 9:46pm UTC
Jun 16, 2018 at 10:03pm UTC
Ah.. the brackets always get me. Thank you.
so, I changed that part of the code to this:
1 2 3 4 5 6 7 8 9 10
if (miles/gallons > bestMPG)
{
bestMPG = MPG;
best = vehicleID;
}
else if (miles/gallons < worstMPG)
{
worstMPG = MPG;
worst = vehicleID;
}
vehicle ID's are all set, but I'm still off by one on each of the best/worst. Still not sure what I'm doing wrong for that.
Jun 16, 2018 at 10:09pm UTC
You're adding one to the MPG after you calculate it.
Jun 16, 2018 at 10:12pm UTC
Yeah just caught. thanks for your quick responses. Its all working now.
Topic archived. No new replies allowed.