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
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?
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?
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.