What's wrong with my program?? Help appreciated

Re-posting for beginner forum, I haven't got any working suggestions there. They said I should implement a copy constructor but that didn't seem to help.

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 they 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. In fact I used "cout" to test the output and it was fine after the sort, it is when it leaves the sorting function that it gets screwed up.

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
41
#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
205
206
207
208
209
210
#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();
 
        //Running fine here
	switch(menuOption)
	{
		case 1:	displayAll(student,studentSize);
				cin.ignore();
				cin.get();
                                //Running fine here
			break;
                           
		case 2: //Running fine here
                           sortStudent(student,studentSize); //Runs sorting function
                           //Not running fine anymore, some grades for gibberish
			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;
	}

         //Sorted grades are how they are supposed to be
}

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
Last edited on
You should write programs starting with a small code snip and before to make the next step to be sure that this small code snip is correct.

So try to write your program from the very begining. It is not interesting to analyze such a big code instead of you.
Of course, but I have tried to find what is wrong for a day now and I can't figure it out. I am writing the code to specifications so this is how it is supposed to be done, and you don't really have to analyze it all, I already said where the problem is..

Everything looks good until I run it through the sorting function, it seems to sort fine until the function goes back to main, leaving gibberish for some grades. I don't know why that is.
Topic archived. No new replies allowed.