Need help with loop

I need help getting the points for each class the students have. The way it is supposed to work is that the students grade is converted to an actual value. For example an 'A' is a 4.0. I am trying to use the Find_points function to convert it. I am not sure what exactly I need to pass in order to transfer the grades correctly. The current code I have takes everything from the file it is supposed to so there is no problem with that. Here is a sample input of what the file looks like.


Smiles Sr., Jack
191-82-3763 2
Physics I
A 5
English 1A
B 4

Jules, Buck
171-51-1621 3
Physics I
A 5
Chemistry 1A
B 5
Computer Science 1
A 4

Bradley, Neily
232-81-1341 7
Physics I
A 5
English 1A
B 4
English 1B
F 4
Physics II
A 5
Chemistry 1A
B 5
Computer Science 1
A 4
Chemistry 1A Lab
B 1



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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#define MAX 45
using namespace std;

struct Student {
string lastname;
string firstname;
string id;
int number_of_classes;
string classname[20];
char grade[20];
int units[20];
float GPA;
float points[40];
};

bool openFile(ifstream &fin);
void Read_info(Student &NewStudent, ifstream &fin);
void Print_info_one(Student NewStudent);
void Find_points(Student Stu_list, char grade[]);

int Read_list(Student Stu_list[], int max_size);
void Print_list(Student Stu_list[], int size);

int main()
{
	Student NewStudent[MAX];
	Student Studentlist[20]; 
	ifstream fin;
	int counter=0;
	counter = Read_list(NewStudent, MAX);
	if(counter>0)
	{
		Find_points(Studentlist, Studentlist.grade);
		Print_list(NewStudent, counter);
	}
	else
		cout << "Empty file. " << endl;
	

	fin.close();
	return 0;
}

bool openFile(ifstream&fin)
{
	string file_name;
	cout << " Enter file name including the path: ";
	getline(cin, file_name);
	fin.open(file_name.c_str());	
	if (fin.fail())
	return false;
	else
	return true ;
}

int Read_list(Student Stu_list[], int max_size)
{
	ifstream fin;
	int i=0;

	if (openFile(fin)==true)
	{  
		Read_info(Stu_list[i], fin);
		while(!fin.eof())
		{
			i++;
			if (i==max_size)
			{
				cout << "\nArray is full. " << endl;
				return (i);
			}
			Read_info(Stu_list[i], fin);
		}
	}
	else 
	{
		cout << "Bad file entered. " << endl;
		exit(0);
	}
	return(i);
}
void Read_info(Student &NewStudent,  ifstream &fin)
{
	int size;
	getline(fin, NewStudent.lastname, ',');
	getline(fin, NewStudent.firstname);
	getline(fin, NewStudent.id, ' ');
	fin >> NewStudent.number_of_classes;
	fin.ignore(10, '\n');
	size = NewStudent.number_of_classes;
		for (int i=0; i<size; i++)
		{
		getline(fin, NewStudent.classname[i]);
		fin >> NewStudent.grade[i];
		fin >> NewStudent.units[i];
		fin.ignore(10, '\n');
		}
}

void Print_info_one(Student NewStudent)
{
	int size = NewStudent.number_of_classes;

	cout << "\n\n\tName: " << right << setw(24) << NewStudent.lastname << "," <<  NewStudent.firstname <<endl;
	cout << "\tID Number: " << right << setw(21) << NewStudent.id << endl;
	cout << "\tNumber of classes: " << NewStudent.number_of_classes << endl;
	cout << "\tCourse: " << NewStudent.classname[0] << endl;
			for(int i=0; i<size; i++)
			{
	cout << "\tCourse 1 " << right << setw(18) << "Name: " << left << setw(14) << NewStudent.classname[i] << "Grade: " << left << setw(5) << NewStudent.grade[i] << "Units: " << NewStudent.units[i] << endl;
	cout << "\tGPA: " << right << setw(21) << showpoint << fixed << setprecision(3) << NewStudent.GPA << endl << endl;
	
			}
}

void Print_list(Student Stu_list[], int size)
{
	for (int i = 0 ; i < size; i++)
	{
		cout << "\n\n\tName: " << right << setw(24) << Stu_list[i].lastname << "," <<  Stu_list[i].firstname <<endl;
		cout << "\tID Number: " << right << setw(21) << Stu_list[i].id << endl;
		cout << "\tNumber of classes: " << Stu_list[i].number_of_classes << endl;
		for(int t=0; t < Stu_list[i].number_of_classes; t++)
		{
			cout << "\tCourse " << t+1 << right << setw(18) << "Name: " << left << setw(14) << Stu_list[i].classname[t] << "Grade: " << left << setw(5) << Stu_list[i].grade[t] << "Units: " << Stu_list[i].units[t] << endl;

			cout << "\tPoints: " << right << setw(21) << showpoint << fixed << setprecision(3) << Stu_list[i].points[t]<<endl;
		}
	}
}

/*
The points are found using this function
*/

void Find_points(Student Stu_list, char grade[])
{
	Student NewStudent;
	if (NewStudent[].grade[] == 'A') 
		NewStudent[].points[] = 4.0;
	if (NewStudent[].grade[] == 'B')
		NewStudent.points[] = 3.0;
	if (NewStudent[].grade[] == 'C')
		NewStudent.points[] = 2.0;
	if (NewStudent[].grade[] == 'D')
		NewStudent.points[] = 1.0;
	if (NewStudent[].grade[] == 'F')
		NewStudent.points[] = 0.0;
	return NewStudent.points[];
}
The problems I am having is fixing line 37 and the function Find_points
The first parameter of 'Find_points' is a single student. 'Studentlist' is an array of students. You probably want to pass the student array that you just read from the file.
The second parameter doesn't exist. Arrays have no 'grade' member.

Inside 'Find_points':
It's unclear why you're defining 'NewStudent', as it's plausible that you're supposed to use the student parameter to do whatever it's supposed to do.
NewStudent[].grade[] doesn't make sense. Look at the other functions to see how arrays are indexed.
The function returns an array but it's declared as 'void'.
Last edited on
I have this now but I am still having trouble with 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#define MAX 45
using namespace std;

struct Student {
string lastname;
string firstname;
string id;
int number_of_classes;
string classname[20];
char grade[20];
int units[20];
float GPA;
float points[40];
};

bool openFile(ifstream &fin);
void Read_info(Student &NewStudent, ifstream &fin);
void Print_info_one(Student NewStudent);
void Find_points(Student &NewStudent, char grade[]);

int Read_list(Student Stu_list[], int max_size);
void Print_list(Student Stu_list[], int size);

int main()
{
	Student NewStudent[MAX], Stu_new; 
	ifstream fin;
	int counter=0;
	counter = Read_list(NewStudent, MAX);
	for (int i = 0 ; i < counter; i++)
	{
	 Find_points(Stu_new, Stu_new.grade[i]);
	}
	if(counter>0)
	{
		Print_list(NewStudent, counter);
	}
	else
		cout << "Empty file. " << endl;
	

	fin.close();
	return 0;
}

bool openFile(ifstream&fin)
{
	string file_name;
	cout << " Enter file name including the path: ";
	getline(cin, file_name);
	fin.open(file_name.c_str());	
	if (fin.fail())
	return false;
	else
	return true ;
}

int Read_list(Student Stu_list[], int max_size)
{
	ifstream fin;
	int i=0;

	if (openFile(fin)==true)
	{  
		Read_info(Stu_list[i], fin);
		while(!fin.eof())
		{
			i++;
			if (i==max_size)
			{
				cout << "\nArray is full. " << endl;
				return (i);
			}
			Read_info(Stu_list[i], fin);
		}
	}
	else 
	{
		cout << "Bad file entered. " << endl;
		exit(0);
	}
	return(i);
}
void Read_info(Student &NewStudent,  ifstream &fin)
{
	int size;
	getline(fin, NewStudent.lastname, ',');
	getline(fin, NewStudent.firstname);
	getline(fin, NewStudent.id, ' ');
	fin >> NewStudent.number_of_classes;
	fin.ignore(10, '\n');
	size = NewStudent.number_of_classes;
		for (int i=0; i<size; i++)
		{
		getline(fin, NewStudent.classname[i]);
		fin >> NewStudent.grade[i];
		fin >> NewStudent.units[i];
		fin.ignore(10, '\n');
		}
}

void Print_info_one(Student NewStudent)
{
	int size = NewStudent.number_of_classes;

	cout << "\n\n\tName: " << right << setw(24) << NewStudent.lastname << "," <<  NewStudent.firstname <<endl;
	cout << "\tID Number: " << right << setw(21) << NewStudent.id << endl;
	cout << "\tNumber of classes: " << NewStudent.number_of_classes << endl;
	cout << "\tCourse: " << NewStudent.classname[0] << endl;
			for(int i=0; i<size; i++)
			{
	cout << "\tCourse 1 " << right << setw(18) << "Name: " << left << setw(14) << NewStudent.classname[i] << "Grade: " << left << setw(5) << NewStudent.grade[i] << "Units: " << NewStudent.units[i] << endl;
	cout << "\tGPA: " << right << setw(21) << showpoint << fixed << setprecision(3) << NewStudent.GPA << endl << endl;
	
			}
}

void Print_list(Student Stu_list[], int size)
{
	for (int i = 0 ; i < size; i++)
	{
		cout << "\n\n\tName: " << right << setw(24) << Stu_list[i].lastname << "," <<  Stu_list[i].firstname <<endl;
		cout << "\tID Number: " << right << setw(21) << Stu_list[i].id << endl;
		cout << "\tNumber of classes: " << Stu_list[i].number_of_classes << endl;
		for(int t=0; t < Stu_list[i].number_of_classes; t++)
		{
			cout << "\tCourse " << t+1 << right << setw(18) << "Name: " << left << setw(14) << Stu_list[i].classname[t] << "Grade: " << left << setw(5) << Stu_list[i].grade[t] << "Units: " << Stu_list[i].units[t] << endl;

			cout << "\tPoints: " << right << setw(21) << showpoint << fixed << setprecision(3) << Stu_list[i].points[t]<<endl;
		}
	}
}

/*
The points are found using this function
*/

void Find_points(Student &NewStudent, char grade[])
{
	Student Stu_list[45];
	int size;
	size= 0; 
		for (int i = 0 ; i < size; i++)
	{
	if (Stu_list[i].grade[i] == 'A') 
		Stu_list[i].points[i] = 4.0;
	if (Stu_list[i].grade[i] == 'B')
		Stu_list[i].points[i] = 3.0;
	if (Stu_list[i].grade[i] == 'C')
		Stu_list[i].points[i] = 2.0;
	if (Stu_list[i].grade[i] == 'D')
		Stu_list[i].points[i] = 1.0;
	if (Stu_list[i].grade[i] == 'F')
		Stu_list[i].points[i] = 0.0;
		}
}
Still not sure why line 36 is not working
Line 36: You're passing Stu_new, but at line 30, Stu_new is ininitialized. At line 36, you want to be passing new_student[i]. The second argument is not needed. Stu_new is not needed at all.

 
  Find_points(new_student[i]);


Line 147: how many times do you think this loop is going toi execute after setting size to 0?

Line 144: What do you think this array contains? Hint: nothing. You haven't stored anything into it.

Line 142: You pass in a single student by reference. That's fine, but lines 149-158 refer to stu_list[i], which contains garbage. You should be referencing NewStudent.
Topic archived. No new replies allowed.