Please use code tags. Edit your original post, highlight the code and click the <> button to the right of the edit window. This will automatically add line numbers and syntax highlighting to make it easier to refer to your code.
Your for (x...) loop only reads 3 students. It needs to read 5.
Your code to find the high score has the wrong comparison (< instead of >)
@ImCristal I've made some fixes to your code. Sorted your GPA array. So that, now the program knows what is the highest and lowest averages, but couldn't figure out how to match them with names. Will work on it. It's 1 AM here, need to sleep. See you tomorrow.
Here's your code, fixed in styling and added this part:
char name[3];
This leaves only 2 characters for a student name. You should probably make the array larger. Better yet, use a string. cin>>stud[x].name;
This will read to the first whitespace character. So if you enter "John Doe" it will only read "John." If you want to read a full name, use fgets() or better yet, change name to a string and use getline().
loname = stud[z].ave; Well that doesn't seem right :)
To find the student with the low and high score, keep track of their index into the array rather than than their name and average separately. It makes the code easier. Also, you can initialize the both indices to 0 and then start the search from index 1.
1 2 3 4 5 6 7 8 9 10 11 12 13
int his = 0, los = 0; // index of students with high/low average
for (int z = 1; z < 5; z++) {
if (stud[z].ave < stud[los].ave) {
los = z;
}
if (stud[z].ave > stud[his].ave) {
his = z;
}
}
...
cout << "Highest student: " << stud[his].name << ", " << stud[his].ave << endl;
cout << "Lowest student: " << stud[los].name << ", " << stud[los].ave << endl;
}
Those kind of problems happen a lot in c++ and they better be caught as fast as possible. If your'e getting to hard detecting them you try using programs like checkmarx or others but it's also recommended to do it by yourself if you can.
Good luck with it!
Ben.
std::minmax_elements() would remove the need for tedious calculations if operator < was overloaded for struct Student even while using a C-style array: