Why doesn't my program work? Help appreciated

What this program is basically supposed to do is get a name, student ID and varying amount of grades. The thing is, grades keep getting messed up on the output. I would appreciate if someone took the time to run it and see what you think is wrong with it.

For example, when I input 3 people and display them everything is fine, but upon sorting it into descending order(sorts through average of grades), the grade numbers for the second highest are gibberish. Upon sorting it again, some other grades are gibberish. I don't see what is wrong with my sorting function.

Header File
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[],int);

		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 


Header 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
69
#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[],int gradeSize)
{
	firstName=fn;
	lastName=ln;
	studentID=id;
	gradeNum=gradeSize;

	for(int i=0;i<gradeSize;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
203
204
#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()<<endl;
		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();
}


If there is a better way to go about doing it, I would appreciate that as well. Thanks in advance
You'll probably need to implement the copy constructor and the assignment operator because you have a pointer in your class.
In fact, the sorting is just fine according to cout tests, it is when it leaves the sorting function that it gets messed up.
I got that response last time, but I don't understand. We haven't been taught any of that yet, no copy constructors or assignment operators. Isn't there another way?
Don't use a pointer in the class. Statically allocate the array (array[50]).

Hmm, well he clearly mentioned using a pointer in the class. I already tried using a copy constructor, unless I am doing something wrong that doesn't fix the program
With the original code I gave you, both the copy constructor and operator= were used.

Without supplying both the copy constructor and operator=, you wouldn't be fixing the problem. Since you modified the code I think you could get by with just supplying operator=, however the class still requires both to be correct.

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
Student::Student(const Student& s)
    : firstName(s.firstName), lastName(s.lastName), studentID(s.studentID), 
      gradeNum(s.gradeNum), grades(new double[s.gradeNum])
{
    for ( unsigned i=0; i<gradeNum; ++i )
        grades[i] = s.grades[i] ;
}

Student& Student::operator=(const Student& s)
{
    if ( this != &s )
    {
        delete [] grades ;

        firstName = s.firstName ;
        lastName = s.lastName ;
        studentID = s.studentID ;
        gradeNum = s.gradeNum ;
        
        grades = new double[gradeNum] ;
        for ( unsigned i=0; i<gradeNum; ++i )
            grades[i] = s.grades[i] ;
    }
    return *this ;
}
Show your current code.

Where are you allocating memory for that pointer?

Last edited on
Ah thank you! Yes I had not done the operator= correctly. Now it works perfectly, thank you very much
Topic archived. No new replies allowed.