My problem here is that, it cant display the name and average of student with the highest and lowest average. Thank you guys!

#include<iostream>
using namespace std;


struct student
{
char name[3];
int score[2];
float ave, total;
string remarks;

}stud[5];

main()

{
float his=0, los=0;
string hiname, loname;

cout<<"~Enter Student Information~"<<endl;

for( int x=0; x<3; x++)
{
cout<<"Enter a name: "<<stud[x].name<<endl;
cin>>stud[x].name;
cout<<endl;



for(int y=0; y<2; y++)
{
cout<<"Enter test score: ";
cin>>stud[x].score[y];

stud[x].total+=stud[x].score[y];
stud[x].ave=stud[x].total/2;
}


if(stud[x].ave>=75)
{

stud[x].remarks="passed";
}
else if ( stud[x].ave<75)
{
stud[x].remarks="failed";
}
}


for( int z=0; z<5; z++)
{
if(z==0)
{
los=stud[z].ave;
loname=stud[z].name;
}
else
{
if(stud[z].ave<los)
{
los=stud[z].ave;
loname=stud[z].ave;
}
}
if(stud[z].ave<his)
{
his=stud[z].ave;
hiname=stud[z].name;
}
}

cout<<endl;
cout<<"Name:\t\tAve:\t\tRemark:"<<endl<<endl;

for(int x=0; x<3; x++)
{
cout<<stud[x].name<<"\t\t"<<stud[x].ave<<"\t\t"<<stud[x].remarks<<endl;

}
cout<<endl;
cout<<"Highest student"<<hiname<<","<<his<<endl;
cout<<"Lowest student"<<loname<<","<<los<<endl;

return 0;
}
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:
1
2
3
4
5
6
7
8
9
float temp;
	for(int k = 0; k < 3; k++){
		if(stud[k].ave>stud[k+1].ave)
		{
			temp = stud[k].ave;
			stud[k+1].ave = stud[k].ave;
			stud[k+1].ave = temp;
		}
	}


Good luck!

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
#include <iostream>
using namespace std;


struct student
{
	char name[3];
	int score[2];
	float ave, total;
	string remarks;
}stud[5];

int main(){

	float his=0, los=0;
	string hiname, loname;

	cout<<"~Enter Student Information~"<<endl;

	for(int x=0; x<3; x++)
	{
		cout<<"Enter a name: "<<stud[x].name<<endl;
		cin>>stud[x].name;
		cout<<endl;

		for(int y=0; y<2; y++)
		{
			cout<<"Enter test score: ";
			cin>>stud[x].score[y];

			stud[x].total+=stud[x].score[y];
			stud[x].ave=stud[x].total/2;
		}

		if(stud[x].ave>=75)
		{ stud[x].remarks="passed"; }
		else if (stud[x].ave<75)
		{ stud[x].remarks="failed"; }
	}


	for(int z=0; z<5; z++)
	{
		if(z==0)
		{
			los=stud[z].ave;
			loname=stud[z].name;
		}
		else
		{
			if(stud[z].ave<los)
			{
				los=stud[z].ave;
				loname=stud[z].ave;
			}
		}
		if(stud[z].ave<his)
		{
		his=stud[z].ave;
		hiname=stud[z].name;
		}
	}

	cout << endl;
	cout << "Name:\t\tAve:\t\tRemark:"<<endl<<endl;

	for(int x=0; x<3; x++)
	{ cout << stud[x].name << "\t\t"<<stud[x].ave << "\t\t"
		   << stud[x].remarks<<endl; }

	float temp;
	for(int k = 0; k < 3; k++){
		if(stud[k].ave>stud[k+1].ave)
		{
			temp = stud[k].ave;
			stud[k+1].ave = stud[k].ave;
			stud[k+1].ave = temp;
		}
	}

	cout << endl;
	cout << "Highest student average:" <<", "<<stud[2].ave<<endl;
	cout << "Lowest student average :" <<", "<<stud[0].ave<<endl;

return 0;
}
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;
}

Thank you guys!
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:
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
#include <iostream>
#include <string>
#include <algorithm>

struct Student
{
    std::string m_name;
    double m_score;
};
bool operator < (const Student& lhs, const Student& rhs)
{
    return lhs.m_score < rhs.m_score;
}

int main()
{
    Student students[3];

    students[0] = Student{"John", 12};
    students[1] = Student{"Jane", 45};
    students[2] = Student{"AN Other", 6};

    auto mn = std::minmax_element(std::begin(students), std::end(students));
    std::cout << "Minimum: " << (*(mn.first)).m_name << " " << (*(mn.first)).m_score << "\n";
    std::cout << "Maximum: " << (*(mn.second)).m_name << " " << (*(mn.second)).m_score << "\n";
}

and, needless to say, a std::vector<Student> would be even more efficient all around
Thank you guys for the help!
Topic archived. No new replies allowed.