Class program to sort student name alphabetically crashes on run.

This program is to allow user to input student's name, age and marks. A in the class would then sort the student's names alphabetically. However, this program crashes when I try to run it. Any help is appreciated! please be gentle, i'm a very new beginner

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

#include<iostream>
#include<string.h>
#include<conio.h>
using namespace std;

class Student{
	string name;
	int age;
	float avg,marks;
	
public: 
	
Student(){
		name.assign(NULL);
		age=0;
		marks=0;
		avg=0;
	} 


Student(string n, int a, float m){   //overload
	set_data(n,a,m);
	}

~Student(){}//destructor
	
float average(){
		avg=(marks/5);
		return avg;
	}
	
//get
void set_data(string n, int a, float m){		
		name=n;
		age=a;
		marks=m;
}

string get_name(){
		return name;
	}
int get_age(){
		return age;
	}
float get_marks(){
		return marks;
	}
void display(Student []);

void ssort(Student s1[]){
	int i;
	Student temp;

	for(i=0; i<5; ++i){
		if(s1[i].get_name() > s1[i+1].get_name()){		
			temp=s1[i];
			s1[i+1]=s1[i];
			s1[i]=temp;	
			
			cout<<s1[i].get_name(); //testing
		}
	}
}

};

int main()
{
	
	int i,a;
	float m;
	string n;
	
	Student s1[5];
	Student s2;
	
	for(i=0; i<5; ++i){
		cout<<"Enter student name: ";
		getchar();
		getline(cin,n);
		cout<<"Enter age of student: ";
		cin>>a;
		cout<<"Enter students marks: ";
		cin>>m;	
		cout<<"\n";

		s1[i].set_data(n, a, m);
	}	
	
	s2.ssort(s1);
}


void Student::display( Student s1[]){

	int i;
	
	for(i=0; i<5; ++i){
		cout<<"Name: "<<s1[i].get_name()<<endl;
		cout<<"Age: "<<s1[i].get_age()<<endl;
		cout<<"Marks: "<<s1[i].get_marks()<<endl;
		
		cout<<"\nAverage marks of the five students are: "<<average();
	}
}
name.assign(NULL);

This says "read the memory at memory location NULL, it's an array of char with a zero on the end, and copy those char to the string".

Trying to read memory at NULL is very very bad. Don't do that.
Last edited on
How many elements are in the array?
What is the value of i on the last iteration of the loop on lines 55-63?
What is therefore the value of i+1 on lines 56 and 58?
Is that a valid index?
5 elements in the array, value of I was initialized to 0, it was planned to be a loop to compare names in each array
if(s1[i].get_name() > s1[i+1].get_name()){

So when i is 4, how valid is that line?
If you are not sure, where the "4" above did came from, look at line 55:
for ( i=0; i<5; ++i )
Yes, the i is initialized to 0.
After each iteration the i is incremented by 1.
Therefore, the i will have values 0, 1, 2, ...
When i==5, the condition (i<5) is false and the loop ends.
What was the last value of i, when the condition was still true?
It was 4.



You do have an another issue with both the Student::display() and the Student::ssort():
That are hardcoded to loop over an array of 5 (?) elements. Why should a student know or care?

What if the main program gets 42 students from somewhere? Your display and ssort still still do 5.

What if you want to show one student only?

Do the display and ssort have to be member functions? No. They can use the get_**() functions.


The average(). Average of what? Let me guess, your plan is to call display in main() something like:
s2.display(s1);
Whose average() will be called? Of s2.
What does s2.average() print? s2.marks/5. The s2 is default initialized, so s2.marks is 0.
Isn't it lovely how this rotten apple student s2, who has never taken any course, could bring the average of five A-grade students to flat 0?
closed account (E0p9LyTq)
Your display() and ssort() functions should not be class methods, they should be separate stand-alone functions. Both functions deal with an array of Students. Your C++ class deals with the particulars of a single student, not a classroom of students.
Thank you all for your help!
Topic archived. No new replies allowed.