Count the average from an external textfile. (containing strings)

Hey,

Im trying to make a program with an external .txt file. It should contain
First name, last name and the age of the persons. Then get the info from the .txt-file and print the information, sum all the ages and then count the average age.
I don't know how to count the number of integers so I can calculate the average
age. Should I save the information in another way? (Im a rookie). Help me please! Should i use a for-loop in some kind of way?

This is what I have done so far:

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

int main() {
int sum = 0;
int age;
int avg=0;
string name;
string lname;


ifstream inFile;

inFile.open("elevinfo.txt");

if (!inFile) 
    {
        cout << "Unable to open file";
        exit(1);
    }


while (inFile >> name >> lname >> age) 
    {
        cout <<name<<" "<<lname <<" "<<endl<<age<< '\0'<<endl<<endl;
        sum = sum + age;
    }

inFile.close();

        cout <<"Sum: " <<sum<<endl;
        cout <<"Average: ?"<<endl<<endl<<endl;

return 0;
}



-----------


The .txt document looks like this:

1
2
3
4
Person Lastname		29
Person Lastname		35
Person Lastname		45
Person Lastname		27
Last edited on
1
2
3
4
5
6
7
8
9
just count them as you read them. 

unsigned int count = 0;
while (inFile >> name >> lname >> age) 
    {
        cout <<name<<" "<<lname <<" "<<endl<<age<< '\0'<<endl<<endl;
        sum = sum + age;
        count ++;
    }


and divide by count.
Last edited on
Thank you!!!!! :D
Hi,

I have a problem with calculating the new average and sum of the ages when I add more students. And when I press 2 in "How many students do you want to add?"

The "Enter name" and "Enter last name" shows up at the same time, something wrong with the for-loop?

The program is supposed to take the full name in one string but I don't know how?

Please help! (rookie)

Or give me some hints?

Thank you!!



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

using namespace std;

int main()
{

int sum = 0;
int age;
string name;
string lname;
unsigned int count=0;
int newstudent;

ifstream inFile;

inFile.open("elevinfo.txt");

if (!inFile)
    {
        cout << "Unable to open file";
        exit(1);
    }


while (inFile >> name >> lname >> age)
    {
        cout <<name<<" "<<lname <<" "<<endl<<age<< '\0'<<endl<<endl;
        sum = sum + age;
        count++;
    }

inFile.close();

        cout <<"Sum: " <<sum<<endl;
        cout <<"Average: "<<sum/count<<endl<<endl<<endl;


ofstream outputfile;
outputfile.open("elevinfo.txt", ios::app);


cout<<"How many students do you want to add?"<<endl;
cin>>newstudent;

for(int i=0; i<newstudent; i++)
{
        cout<< "Enter name: ";
        getline(cin,name);
        outputfile <<name;

        cout << "Enter last name: ";
        getline(cin,lname);
        outputfile <<lname;

        cout<<"Enter age: ";
        cin>>age;
        outputfile <<age;
}

        cout <<"Sum: " <<sum<<endl;
        cout <<"Average: "<<sum/count<<endl<<endl<<endl;

outputfile.close();

return 0;
}
Last edited on
Bump. please anybody?
You need to update your sum and count in your for loop.
Also you need to write a separator when you add the new students.
Thank you,

everything works except when I write newstudents it looks like this:

Person Lastname 29
Person Lastname 35
Person Lastname 45
Person Lastname 27
Person Lastname34PersonLastname43

How do I make a new line? <<endl; in some kind of way? and to get the space between them..

There something with the >> or << right?
How i "cin" them in?

Sorry I'm so confused =(

Adding a separator is as simple as:
1
2
3
4
5
6
7
8
9
10
11
12
cout<< "Enter name: ";
getline(cin,name);
outputfile <<name;
outputfile << ' ';
cout << "Enter last name: ";
getline(cin,lname);
outputfile <<lname;
outputfile << ' ';
cout<<"Enter age: ";
cin>>age;
outputfile <<age;
outputfile << ' \n';
Thank you! you´re the best! I have to study more..
Topic archived. No new replies allowed.