I/O Files

Hello all,
I am reading from a txt. file and have 3 columns of numerical data.
I want to sum 2 of the columns and return the total at the bottom of the applicable column. Any help would be appreciated.

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
//This program writes data to a file
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>


using namespace std;

int main()
{
	ifstream inputFile;
	string name;
	int carNum;
	int milesDriven;
	int gallonsUsed;
    double MPG;
		
	
	// Open the file.
	inputFile.open("cars.txt");

	cout << "Reading data from the file. \n";

	getline(inputFile,name);
	cout << name << setw(5) << "MPG" << endl;
	getline(inputFile,name);
	cout << name << endl;


	
	while (inputFile >> carNum >> milesDriven >> gallonsUsed)
		
	{
			MPG = milesDriven/gallonsUsed;
			cout << carNum << setw(10) << milesDriven << setw(5) << gallonsUsed
				<< setw(10) << milesDriven/gallonsUsed << endl;
			

	}

	cout << "----------------------------" << endl;
	
	



	
	
inputFile.close();







system ("pause");
return 0;
}
Is it currently working well? As in, is it actually outputting the file contents?

If so, then all you need to do is this (differences in bold):
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
//This program writes data to a file
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
    ifstream inputFile;
    string name;
    int carNum, totalcarNum = 0;
    int milesDriven, totalMiles = 0;
    int gallonsUsed, totalGallons = 0;
    double MPG;
		
    // Open the file.
    inputFile.open("cars.txt");

    cout << "Reading data from the file. \n";

    getline(inputFile,name);
    cout << name << setw(5) << "MPG" << endl;
    getline(inputFile,name);
    cout << name << endl;

    while (inputFile >> carNum >> milesDriven >> gallonsUsed)	
    {
        MPG = milesDriven/gallonsUsed;
        cout << carNum << setw(10) << milesDriven << setw(5) << gallonsUsed
             << setw(10) << milesDriven/gallonsUsed << endl;
    
        totalcarNum += carNum;
        totalMiles += milesDriven;
        totalGallons += gallonsUsed;
    }

    cout << "----------------------------" << endl;	
    cout << totalcarNum << setw(10) << totalMiles << setw(5) << totalGallons
         << setw(10) << totalMiles/totalGallons << endl;	

    inputFile.close();

    system ("pause");
    return 0;
}
Last edited on
I bow to your greatness. That worked perfectly and was exactly what I wanted.
Yes I was able to read from file and able to calculate the MPG correctly.
I couldn't figure out if I needed to do the totals within the while loop and didn't even think about using the += to tabulate the totals. Also I get bound up with the initialization sometimes being a "newbie".

Once again thank you. I want to learn programming and this site is awesome for helping me through rough patches.

Thank you once again Stewbond!!!!!



Topic archived. No new replies allowed.