Text file data calculations

Hi

The program below reads in a text file consisting of two columns. If the two columns are called x any, i want to multiply x and y for each row and display an answer. I then want to add these calculated (x*y) values. And then i want to find the averages by taking the sum and dividing it by the number of lines (rows) of the textfile. How do i go about doing this?

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream infile("data.txt");
if (!infile)
{
cout << "Unable to open" << endl;
return -1;
}

int x, y;

while (infile >> x >> y)
{
cout << x << " " << y << endl;

int product = (x*y);
cout << product << endl;
}

return 0;
}
Last edited on
first you create a counter for each time you multiply.
then add a variable to keep up with each time you multiply.
When you run out of numbers in the file you divide the total by the counter.
How do I write a function to calculate the average values of (x*y).
There are only four x and four y values in the text file.

#include <iostream>
#include <fstream>

using namespace std;

void getRecord(ifstream &f, int& x, int& y)
{
f >> x >> y;
}

int calcTotal(int x, int y)
{
int xy = x*y;
return xy;
}

int calcAvg (int xy)
{
return xy/4;
}

void writeOut(ofstream &f, int t)
{
f << t << endl;
}

int main()
{
int x1,x2,x3,x4,y1,y2,y3,y4,t1,t2,t3,t4,xy,a1;

ifstream infile("data.txt");

getRecord(infile, x1, y1);
getRecord(infile, x2, y2);
getRecord(infile, x3, y3);
getRecord(infile, x4, y4);
infile.close( );

t1 = calcTotal(x1,y1);
t2 = calcTotal(x2,y2);
t3 = calcTotal(x3,y3);
t4 = calcTotal(x4,y4);

a1 = calcAvg(xy);

ofstream outfile("results.txt");

writeOut(outfile, t1);
writeOut(outfile, t2);
writeOut(outfile, t3);
writeOut(outfile, t4);
writeOut(outfile, a1);

outfile.close();

return 0;
}
Try making following changes to your code

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
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream infile("data.txt");
if (!infile)
{
cout << "Unable to open" << endl;
return -1;
}

int x, y;

int counter=0; // to count the number of rows
int product=0;
int sum_product=0;
while (infile >> x >> y)
{
cout << x << " " << y << endl;

product = (x*y);
sum_product=sum_product+product; // keep adding the products
counter++;
cout << product << endl;
}

int avg=0;
avg=sum_product/counter;

return 0;
}
I have 9 columns and have changed my code to the following below. I only want to multiply the second last two columns and find an average. When i run the code it crashes. Any suggestions?

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
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream infile("data.txt");
if (!infile)
{
cout << "Unable to open" << endl;
return -1;
}

int a, b, c, d, e, f, g, h, i;

int counter=0; // to count the number of rows
int product=0;
int sum_product=0;
while (infile >> a >> b >> c >> d >> e >> f >> g >> h >> i)
{
cout << g << " " << h << endl;

product = (g*h);
sum_product=sum_product+product; // keep adding the products
counter++;
cout << product << endl;
}

int avg=0;
avg=sum_product/counter;

return 0;
}
Last edited on
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) You can use a debugger to step through your code, to see exactly where it crashes and what the state of the memory is at that point. I'd recommend doing that.

3) If I had to guess, I'd say the most likely reason is that you're dividing by zero when calculating the average. Are you sure the code inside your loop is being executed, even once?
Figured it out thanks. Had to use double instead of int.
How would you find the largest value of g*h from all the values in the file?

I tried:

1
2
3
while(counter)
    if (product>product)
        cout << product << endl;


Does not work.
Last edited on
I am approaching a similar problem where at the moment I would only like to be able to read in each column separately and have them print on the screen.

Currently I have this

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
#include <iostream>
#include <fstream>
using namespace std;

string Weather_ID, Date, Temperature, Maximum_Temperature, Minimum_Temperature, Sea_Level_Pressure, Humidity, PP, Vertical_Visibility, V, VM, VG, Rain, Snow, Thunder_Storm, Fog;

int main()
{
    ifstream Weather_test;
    Weather_test.open("/Users/MyName/Desktop/Weather_test.csv");
    
    if(!Weather_test)
    {
        cout << "The file did not open correctly";
    }
    
    int row=0;
    
    getline(Weather_test, Weather_ID, ',');
    getline(Weather_test, Date, ',');
    getline(Weather_test, Temperature, ',');
    getline(Weather_test, Maximum_Temperature, ',');
    getline(Weather_test, Minimum_Temperature, ',');
    getline(Weather_test, Sea_Level_Pressure, ',');
    getline(Weather_test, Humidity, ',');
    getline(Weather_test, PP, ',');
    getline(Weather_test, Vertical_Visibility, ',');
    getline(Weather_test, V, ',');
    getline(Weather_test, VM, ',');
    getline(Weather_test, VG, ',');
    getline(Weather_test, Rain, ',');
    getline(Weather_test, Snow, ',');
    getline(Weather_test, Thunder_Storm, ',');
    getline(Weather_test, Fog, '\n');
   
    cout << string(Date);
    cout << string(Fog);
    
    return 0;
}


The problem with this is that I am stuck on the first line and can't get to see each column. I think I need to use a while loop somewhere, hence why I have included the int row=0; line in anticipation (and based on what I saw here). I will keep rummaging around on the internet for some help but if anyone here knows what to do then that would be most appreciated.

Thanks!
Last edited on
Take another variable named initial_product:-

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
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream infile("data.txt");
if (!infile)
{
cout << "Unable to open" << endl;
return -1;
}

int a, b, c, d, e, f, g, h, i;

int counter=0; // to count the number of rows
int product=0;
int sum_product=0;
int initial_product=0; // stores the value of last product
int largest_product=0; // stores the value of largest product
while (infile >> a >> b >> c >> d >> e >> f >> g >> h >> i)
{
cout << g << " " << h << endl;

product = (g*h);
if(product>initial_product)
largest_product=product;
else
largest_product=initial_product;
sum_product=sum_product+product; // keep adding the products
counter++;
cout << product << endl;
initial_product=product;
}

int avg=0;
avg=sum_product/counter;

return 0;
}
Topic archived. No new replies allowed.