Hey guys, i'm fairly new to this so i'd appreciate any help.
i have to read in the information from a data file with the info
car# miles gas mpg
54 250 19 13.16
62 525 38 13.82
71 123 6 20.5
85 1322 86 15.37
97 235 14 16.79
where the first column is just an identifier, then calcuate the total miles, gas and the average mpg. finally send it to an output data file. Here is what i have so far but i keep getting an error message
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
//string read;
double car_num, miles, used_gas, mpg, total_miles,
total_gas, avg_mpg;
int i=0;
Taking a guess you could create an array of 5 elements - considering that how many elements you have in the file. You could then dump all the averages into this array as long as the file always has 5 elements.
You can read the file contents in how you did before. But remove the ** mpg[i]=avg_mpg and change that too avg_mpg[i] = miles/used_gas; etc.
You can output data like this:
outfile << "Total Miles : " << total_miles << "\n";
well this works but i'm getting the wrong answers. if i added all the miles the total = 2455, total gas = 163 and avg_mpg for all the cars = 15.06(total miles/total gas). This is somehow adding the last miles and gas used to perform its calculation.
I tried this program to see if it would run. I copied your data and put it in my c drive in a text file. I ran the program. it then said "press any key to continue" then I check the cdrive, it outputed to the c drive and it created an output file .txt, but it is blank. Any ideas?
i ended up writing this and it works fine.
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
double car_num, miles, used_gas, mpg, total_miles=0,
total_gas=0, avg_mpg[5];
int i=0;