output data files!

Dec 15, 2009 at 6:39am
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;

ifstream infile("K:\\EGR125\\mpg.txt");
ofstream outfile("K:\\EGR125\\mpgout.txt");


while(!infile.eof())
{
infile >> car_num >> miles >> used_gas >> mpg;
total_miles += miles;
total_gas += used_gas;
avg_mpg = miles/used_gas;
** mpg[i]=avg_mpg;
i++;
}
outfile.close();
system("pause");
return 0;
}

**: invalid types `double[int]' for array subscript
Dec 15, 2009 at 7:31am
What exactly are you trying to achieve with ** mpg[i]=avg_mpg;
Dec 15, 2009 at 7:41am
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";


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
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
	//string read;
	double car_num, miles, used_gas, mpg;
	double total_miles = 0;
	double total_gas = 0;
	double avg_mpg[5]; // number of entries in file...

	int i=0;

	ifstream infile("K:\\EGR125\\mpg.txt");
	ofstream outfile("K:\\EGR125\\mpgout.txt");


	while(!infile.eof())
	{
		infile >> car_num >> miles >> used_gas >> mpg;
		total_miles += miles;
		total_gas += used_gas;
		avg_mpg[i] = miles/used_gas;
		i++;
	}
	infile.close();

	outfile << "Total Miles : " << total_miles << "\n";
	outfile << "Total Gas : " << total_gas << "\n";
	outfile << "Average MPG : " << avg_mpg[0] << " " << avg_mpg[1] << " " << avg_mpg[2] << " " << avg_mpg[3] << " " << avg_mpg[4] << "\n";
	outfile.close(); 

	system("pause");
	return 0;
}


EDIT:
I would recommend changing this:
1
2
ifstream infile("K:\\EGR125\\mpg.txt");
ofstream outfile("K:\\EGR125\\mpgout.txt");

to
1
2
ifstream infile("mpg.txt");
ofstream outfile("mpgout.txt");


And put the mpg.txt in the same location as where you made this project. So put the file where your .cpp file is inside the project folder etc.
Last edited on Dec 15, 2009 at 7:44am
Dec 15, 2009 at 12:16pm
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.
Dec 15, 2009 at 9:15pm
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?
Last edited on Dec 15, 2009 at 9:16pm
Dec 16, 2009 at 3:16am
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;

ifstream infile("G:\\EGR125\\mpg.txt");
ofstream outfile("G:\\EGR125\\mpgout.txt");
infile >> car_num >> miles >> used_gas >> mpg;
while(!infile.eof())
{
total_miles += miles;
total_gas += used_gas;
avg_mpg[i] = miles/used_gas;
i++;
infile >> car_num >> miles >> used_gas >> mpg;
}
infile.close();

outfile << "Total Miles = " << total_miles << "\n";
outfile << "Total Gas = " << total_gas << "\n";
outfile << "Average MPG for all the cars = ";
for(i=0; i <5; i++)
outfile << avg_mpg[i] << " ";
outfile.close();

system("pause");
return 0;
}
Topic archived. No new replies allowed.