What is wrong with my program? (Long code read if you dare to care)

I am stumped by this. I am close to getting it (I think) but sometimes the displayed grades go crazy. For example when I enter 3 students and sort them, the second student always has something like -1.E242421 or something.

I honestly hate to ask for help but I have been staying up all night trying to figure this one out. I realize there are probably more problems than that but I just want to figure out why it is giving me crazy grades when I sort the students

Header
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
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
#include <iostream>
using namespace std;

class Student
{
	private:
		string firstName;
		string lastName;
		int studentID;
		int gradeNum;
		double *grades;
	public:

		Student();
		Student(string, string,int,int,double[]);

		void setFirstN(string f)
		{ firstName=f; }
		void setLastN(string l)
		{ lastName=l; }
		void setStudentID(int);
		void setGrades(int, double[]);

		string getFirstN()
		{ return firstName;}
		string getLastN()
		{ return lastName; }
		int getStudentID()
		{ return studentID; }
		double *getGrades()
		{ return grades; }

		double calcAverage();
		void displayGrades();
		~Student();
};
#endif 


Class Functions
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
#include "Student.h"

Student::Student()
{
	firstName=lastName="Default";
	studentID=0;
	grades=0;
	gradeNum=0;
}

Student::Student(string fn, string ln, int id, int size, double arrayN[])
{
	firstName=fn;
	lastName=ln;
	studentID=id;

	for(int i=0;i<size;i++)
	{
		grades[i]=arrayN[i];
	}
}

void Student::setStudentID(int i)
{
	if(i>0)
		studentID=i;
	else
		studentID=0;

}

void Student::setGrades(int size,double arrGrades[])
{
	gradeNum=size;
	
	grades=new double[size];

	for(int i=0;i<size;i++)
	{
		grades[i]=arrGrades[i];
	}
}

double Student::calcAverage()
{
	double average=0;

	for(int i=0;i<gradeNum;i++)
	{
		average+=grades[i];
	}

	return (average/=gradeNum);
}

void Student::displayGrades()
{
	for(int i=0;i<gradeNum;i++)
	{
		cout<<grades[i]<<". ";
	}
}

Student::~Student()
{
	delete [] grades;
	grades=0;
}


Program
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include "Student.h"
using namespace std;

int menu();
void displayAll(Student *, int);
void sortStudent(Student *, int);
void exitSave(Student *, int);

int main()
{
	Student *student;
	int studentSize,
		gradeNum,
		ID,
		menuOption;

	string firstLast;

	double *Grades;

	cout<<"Enter the number of students: ";
	cin>>studentSize;
	while(studentSize<0)
	{
		cout<<"ERROR."<<endl;
		cout<<"Enter positive amount: ";
		cin>>studentSize;
	}

	student=new Student[studentSize];

	for(int i=0;i<studentSize;i++)
	{
		cout<<"Enter the information for Student "<<i+1<<": "<<endl;

		cout<<"First Name: ";
		cin.ignore();
		getline(cin,firstLast);
		student[i].setFirstN(firstLast);

		cout<<"Last Name: ";
		getline(cin,firstLast);
		student[i].setLastN(firstLast);

		cout<<"Student ID: ";
		cin>>ID;
		while(ID<0)
		{
			cout<<"ERROR. Enter positive ID: ";
			cin>>ID;
		}
		student[i].setStudentID(ID);

		cout<<"Enter the number of grades: ";
		cin>>gradeNum;
		while(gradeNum<0)
		{
			cout<<"ERROR."<<endl;
			cout<<"Enter a positive amount: ";
			cin>>gradeNum;
		}

		Grades=new double[gradeNum];

		for(int count=0;count<gradeNum;count++)
		{
			cout<<"Grade "<<count+1<<": ";
			cin>>Grades[count];
			while(Grades[count]<0 || Grades[count]>100)
			{
				cout<<"ERROR. Enter a valid grade: ";
				cin>>Grades[count];
			}
		}
		student[i].setGrades(gradeNum,Grades);

		delete [] Grades;
		Grades=0;

		system("CLS");
	}

	do
	{
	menuOption=menu();
	switch(menuOption)
	{
		case 1: displayAll(student,studentSize);
				cin.ignore();
				cin.get();
			break;
		case 2: sortStudent(student,studentSize);
			break;
		case 3: exitSave(student,studentSize);
			break;
	}

	}while(menuOption!=3);

	delete [] student;
	student=0;
	delete [] Grades;
	Grades=0;

	return 0;

}

int menu()
{
	int option;

	cout<<"MENU"<<endl;
	cout<<"1. Display All"<<endl;
	cout<<"2. Sort students"<<endl;
	cout<<"3. Exit and save"<<endl;

	cout<<"\nEnter choice: ";
	cin>>option;
	while(option<1 || option>3)
	{
		cout<<"ERROR. Enter 1-3: ";
		cin>>option;
	}

	return option;
}

void displayAll(Student *students, int size)
{
	for(int i=0;i<size;i++)
	{
		cout<<i+1<<". ";
		cout<<students[i].getFirstN()<<" ";
		cout<<students[i].getLastN()<<endl;
		cout<<"  ID: "<<students[i].getStudentID()<<", ";
		cout<<"  Grades: ";
		students[i].displayGrades();
		cout<<endl;
	}
}

void sortStudent(Student *students,int size)
{
	int scan,
		minInd;

	Student holder;
	
	double min;

	for(scan=0;scan<(size-1);scan++)
	{
		minInd=scan;
		min=students[scan].calcAverage();
		holder=students[scan];
		
		for(int i=(scan+1);i<size;i++)
		{
			if(students[i].calcAverage()>min)
			{
				minInd=i;
				min=students[i].calcAverage();
				holder=students[i];
			}
		}

		students[minInd]=students[scan];
		students[scan]=holder;
		
	}
}

void exitSave(Student *students, int size)
{
	ofstream writeFile;

	writeFile.open("studentinfo.txt");

	double *grade;

	for(int i=0;i<size;i++)
	{
		writeFile<<i+1<<". ";
		writeFile<<students[i].getFirstN()<<" ";
		writeFile<<students[i].getLastN()<<endl;
		writeFile<<"  ID: "<<students[i].getStudentID()<<", ";
		writeFile<<"  Grades: ";
		grade=students[i].getGrades();
		for(int count=0;count<size;count++)
		{
			writeFile<<grade[count]<<" ";
		}
		writeFile<<endl;
	}

	writeFile.close();
}
Last edited on
Where is your overloaded operator= and copy constructor for Student?

You require both.

Where is gradeNum set and where is memory allocated for grades in Student::Student(string fn, string ln, int id, int size, double arrayN[])?

In void Student::setGrades(int size,double arrGrades[]) don't you think you should do something if grades is pointing to previously allocated memory?
Last edited on
I am not familiar with the initialization for
Student::Student(string fn, string ln, int id, int size, double arrayN[])
I only figured I would put it that way since it was required.

gradeNum is initialized when the user enters the number of grades in the first section of the main function

I am not sure what is meant by overloaded operator=, nor what a copy constructor is. I am just entering the concept of pointers, so I am not much familiar with some terms.
Last edited on
I am not sure, I didn't see a problem since I figured each instance of Student would have its own allocated memory for grades. But the problem isn't in that function I think, when I enter the information everything runs fine and displayAll works perfectly, it is when I sort it that it gets messed up.

Replying again to hopefully keep this bumped for a while longer.
gradeNum is initialized when the user enters the number of grades in the first section of the main function


gradeNum can only be initialized in the constructor. Fortunately for you, you don't use that particular constructor. Unfortunately for you, you do use operator= and the copy constructor and the defaulted ones (those implicitly generated by the compiler) are not going to do the job. You must define them explicitly. This might be the point where you crack open the book and read about classes again.

Failing that, read through http://www.cplusplus.com/articles/y8hv0pDG/
Last edited on
I see. I do not recall that from the lectures and I don't think it is in the book either (C++ Early Objects) atleast not in the chapters I am in currently.

Is that related to why some of the grades are going berserk or is that simply necessary advice for future programming?
Is that related to why some of the grades are going berserk

Yes.
Topic archived. No new replies allowed.