How to read this information from file?

how do i read this information from text file?

the first number tells me how many test scores there are and i have to read the names and the scores and find the average of the score and output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
4
Jones, William B.    83 71 69 97
Henry, Jackson Q.    85 85 88 100
Tell, R. C.          41 95 23 59
Cumming, Rodney J.   92 87 88 96
Doran, Johan Jeffrey 89 79 64 71
Smith, John R.       68 93 84 79

so far i got this

	fin >> howmany;
	GetString(fin, tempname, 20);
	while (!fin.eof() && n < 40)
	{
		names[n] = tempname;
		fin.ignore(128, '\n');
		n++;
		GetString(fin, tempname, 20);
	}

Getstring is a function i developed that read string up to 20 column length
I come up with this see it

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

using namespace std;

int main()
{
	string name;
	int score1 = 0, score2 = 0, score3 = 0, score4 = 0, sum = 0;
	double avg = 0.00;
	ifstream data;
	data.open("score.txt");
	if (!data.fail())
	{
		while (data >> score1 >> score2 >> score3 >>
                score4&&data.ignore(30, '\n'),getline(data, name))
		{
			cout << "\nName " << name << endl;
			cout << "Score " << score1 << " " << score2 << " " << score3 << " "
				<< score4 << endl;
			sum = score1 + score2 + score3 + score4;
			avg = sum/4.0;
			cout << "\nAverage    " << avg << endl;
			avg = 0.00;
			sum = 0;
		}
	}
	else
	{
		cout << "\nFile Cannot Be Found";
		cin.get();
		exit(1);
	}

	system("pause");
}


I arrange your data like this


83 71 69 97
Jones, William B.
85 85 88 100
Henry, Jackson Q.
41 95 23 59
Tell, R. C.
92 87 88 96
Cumming, Rodney J.
89 79 64 71
Doran, Johan Jeffrey
68 93 84 79
Smith, John R.



output



Name Jones, William B.
Score 83 71 69 97
Average    80                                                                                                                       
                                                                                                                                    
Name Henry, Jackson Q.                                                                                                              
Score 85 85 88 100                                                                                                                  
                                                                                                                                    
Average    89.5                                                                                                                     
                                                                                                                                    
Name Tell, R. C.                                                                                                                    
Score 41 95 23 59                                                                                                                   
                                                                                                                                    
Average    54.5                                                                                                                     
                                                                                                                                    
Name Cumming, Rodney J.                                                                                                             
Score 92 87 88 96                                                                                                                   
                                                                                                                                    
Average    90.75                                                                                                                    
                                                                                                                                    
Name Doran, Johan Jeffrey                                                                                                           
Score 89 79 64 71                                                                                                                   
                                                                                                                                    
Average    75.75                                                                                                                    
                                                                                                                                    
Name Smith, John R.                                                                                                                 
Score 68 93 84 79                                                                                                                   
                                                                                                                                    
Average    81
Last edited on
Topic archived. No new replies allowed.