Readin integers and names from a file

Hi, I'm supposed to readin names and 3 test grades for each and find the average, now I got the entire code done but I still can't figure out how to readin the names and grades into my program. Please help! The data in the file are in this format "John Smith\n86 77 92"

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

ifstream fin;

const int MAXSIZE=30;
class students {
	public:
	string name;
	int grades[3];
};
class calc{
	public:
	students info;
	double sum;
	double average;
	char letter;
};
void readin(calc [],int );
void average(calc [],int );
void lettergrade(calc [],int );
void print(calc [],int );


int main()
{
	calc student[MAXSIZE];
	int numb;
        cout<<"Enter the number of students";
        cin>>numb;
	cout<<"   Name\t\t\tAverage\t\t\tLetter Grade"<<endl;
	fin.open("data.txt");
	readin(student,numb);
	average(student,numb);
	lettergrade(student,numb);
	print(student,numb);
	
}

void readin(calc a[],int n)
{

	//This is what I need help with.
}


void average(calc a[],int n)
{
	for (int z=0;z<n;z++){
		for(int i=0;i<3;i++)
			a[z].sum+=a[z].info.grades[i];
		a[z].average=a[z].sum/3;
	}
}


void lettergrade(calc a[],int n)
{	
	for(int i=0;i<n;i++){
		if(a[i].average>=90)
			a[i].letter='A';
		else
		if(a[i].average>=80 && a[i].average<90)
			a[i].letter='B';
		else
		if(a[i].average>=70 && a[i].average<80)
			a[i].letter='C';
		else
		if(a[i].average>=60 && a[i].average<70)
			a[i].letter='D';
		else
		if(a[i].average<60)
			a[i].letter='F';
	}
}


void print(calc a[],int n)
{
	for(int i=0;i<n;i++)
		cout<<a[i].info.name<<"\t\t"<<a[i].average<<"\t\t\t     "<<a[i].letter<<endl;
}
Last edited on
1
2
3
4
5
6
7
8
9
void readin(calc a[],int &n)
{
	for(int i{0}; i < n; ++i){
		std::getline(fin, a[i].info);
		for(int c{0}; c < 3; ++c){
			fin >> a[i].info.grades[c];
		}
	}
}
Last edited on
@benbalach, This is exactly what I had but it does work, it only works for the first student then it gets messed up for the rest of the file, thanks anyways.
Last edited on
It's obvious, since n is 0 (therefore the file will attempt to read 0 students.
Do not loop on n.
@essgeeich I fixed that but I forgot to update the thread, I asked the user to enter the number of students.
Did you check if fin is open?
Are you checking for the file's state?
@Esegerich No, but when I know it's working because when I only do one student, everything goes well but anything above that ruins the program. Now my issue is the order of the data in the file, they go like this "John Smith\n72 98 90\nFirst Last\n80 70 60 etc." so I need a way to readin the data from the file properly so they get assigned where they should..
To remove all potential bugs from your code and to make it simpler I recommend:

* Switching the plain C style arrays to std::array, because plain old C style arrays don't know their own size and they turn into a pointer to their first element at the slightest provocation.

* Change all for loops that traverse the arrays to range based for loops

* Overloading the IO stream operators for both classes.

This will make the code much easier to understand and you might just solve the problem indirectly by making these changes.
Last edited on
Topic archived. No new replies allowed.