Unable to Call Function

Pages: 123
i tried adding this
char bestStudent(student Students[]) {
int x;
double arr;
arr[10] = Students[x].fgrade;

}
but cant seem to get it to work either im not really sure what to do.
Ok, here is the code I would have wrote. Let me know if you have any problems.

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
// Header files
#include <iostream>
#include<string>
#include<iomanip>
// Using namespace standard
using namespace std;
// Structure student
struct student
{
string SFname;
string SLname;
int Sid;
double sgrade[4]; // Hold four grades only
double fgrade; // Final grade score
char lgrade; // Letter grade
};
// Function header
void input(student Students[]); // X is passed by reference
void output(student Students[], double);
void finalGrade(student Students[]);
void letterGrade(student Students[]);
double bestStudent(student Students[]);
// Main
int main()
{
	// Variables declared.
	char highest;
	double highestScore;
	// Constant declared and initialized.
	const int MAX_STUDENTS = 10;
	// Object declared
	student Students[MAX_STUDENTS];
	// 10 function calls
	input(Students); // When students is passed, it passes the array.
	// Final grade calculator.
	finalGrade(Students);
	// Letter grade
	letterGrade(Students);
	// Best student
	highestScore = bestStudent(Students);
	// Output everything
	output(Students, highestScore);
return 0;
}
// Function
void input(student Students[]) // X is passed by reference
{
	int x = 0;
	// Enter data for ten students.
	for (x = 0; x < 10; x++)
	{
		cout << "Please enter Students first name ";
		cin >> Students[x].SFname;
		cout << "Please enter Students last name ";
		cin >> Students[x].SLname;
		cout << "Please enter the Students ID ";
		cin >> Students[x].Sid;
		cout << "Please enter the students first grade ";
		cin >> Students[x].sgrade[0];
		cout << "Please enter the students second grade ";
		cin >> Students[x].sgrade[1];
		cout << "Please enter the students third grade ";
		cin >> Students[x].sgrade[2];
		cout << "Please enter the students fourth grade ";
		cin >> Students[x].sgrade[3];
		cout << endl << endl;
	}
}
// Display function
void output(student Students[], double highestValue)
{
	// Local variables
	int i,
		j,
		k;
	// Display passing students.
	cout << "PASS" << endl;
	cout << "--------------------------------------------------------------" << endl;
	for (int i = 0; i < 10; i++) 
	{
		if (Students[i].fgrade > 59)
		{
		cout << "Name" << setw(30) << "ID" << setw(30) << "Grade" << endl;
		cout << Students[i].SLname << " , "
 			 << Students[i].SFname << setw(30) << right 
			 << Students[i].Sid << setw(30) << right << Students[i].lgrade << endl << endl;
		}
	}
	// Displays failing students
	cout << "FAIL" << endl;
	cout << "--------------------------------------------------------------" << endl;
	for (int j = 0; j < 10; j++) 
	{
		if (Students[i].fgrade < 60)
		{
		cout << "Name" << setw(30) << "ID" << setw(30) << "Grade" << endl;
		cout << Students[i].SLname << " , "
 			 << Students[i].SFname << setw(30) << right
			 << Students[i].Sid << setw(30) << right << Students[i].lgrade << endl << endl;
		}
	}
	// Who had the highest score.
	cout << "HIGHEST SCORE" << endl;
	cout << "--------------------------------------------------------------" << endl;
	for (k = 0; k < 10; k++)
	{
		if(Students[i].fgrade == highestValue)
		{
			cout << "Name" << setw(30) << "ID" << setw(30) << "Grade" << endl;
			cout << Students[i].SLname << " , "  
 				 << Students[i].SFname << setw(30) << right
			 	 << Students[i].Sid << setw(30) << right << Students[i].lgrade << endl << endl;
		}
	}
}
// Final grade function
void finalGrade(student Students[])
{	
	// Local variables.
	int i;
	double totalVar = 0; // Initialized to zero
	// For loop to determine final grades.
	for (i = 0; i < 10; i++)
	{
		// Temporarily store the students grade percentage total in a variable.
		totalVar = Students[i].sgrade[0] + Students[i].sgrade[1] + Students[i].sgrade[2] + Students[i].sgrade[3];
		Students[i].fgrade = totalVar / 4; // Average grade is the final grade
	}
}
// Letter grade function
void letterGrade(student Students[])
{
	// Local variables
	int i;
	// For loop goes through all the students and assigns a grade.
	for (i = 0; i < 10; i++)
	{
		// If...else if statement that determines the grade of students.
		if (Students[i].fgrade <= 100 && Students[i].fgrade >= 90)
			Students[i].lgrade = 'A';
		else if (Students[i].fgrade >= 80 && Students[i].fgrade <= 89)
			Students[i].lgrade = 'B';
		else if (Students[i].fgrade >= 70 && Students[i].fgrade <= 79)
			Students[i].lgrade = 'C';
		else if (Students[i].fgrade >= 60 && Students[i].fgrade <= 69)
			Students[i].lgrade = 'D';
		else if (Students[i].fgrade >= 0 && Students[i].fgrade <= 59)
			Students[i].lgrade = 'F';
	}
}
// Best student function
double bestStudent(student Students[])
{
	// Local variables
	int i;
	// Separated to initialize
	double highScore = Students[0].fgrade; // Set the highest score to the first element in the array.
	// For loop to determine who has the highest score.
	for (i = 0; i < 10; i++)
	{
		// Determines if the next element is higher.
		if (Students[i].fgrade > highScore)
			highScore = Students[i].fgrade; // If it is higher, then it assigns to highScore.
	}
	// Returns highest score.
	return highScore;
}
did you manage to get the best student function to work?
I just checked it. It seems to be working fine. Although letter grade isn't coming out right.
yeah it is no compiling for me now
#include <iostream>
#include<string>
#include<iomanip>
#include<cmath>

using namespace std;
struct student {
string SFname;
string SLname;
int Sid;
double sgrade[4];
double fgrade;
char lgrade;
};


void output(student Students[], double highestValue);
void input(student Students[], int &x);
void Calc(student Students[]);
void gradecalc(student Students[]);
char bestStudent(student Students[]);
int main()
{

double fgrade = 0.0;
int x = 0;
//double fgrade;
student Students[10];
input(Students, x);
Calc(Students);
gradecalc(Students);
bestStudent(Students);


cin.get(); cin.get();
return 0;
}
void input(student Students[], int &x){

for (int x = 0; x < 10; x++) {
cout << "Please enter Students first name" << endl;
cin >> Students[x].SFname;
cout << "Please enter Students last name" << endl;
cin >> Students[x].SLname;
cout << "Please enter the Students ID" << endl;
cin >> Students[x].Sid;
cout << "Please enter the students first grade" << endl;
cin >> Students[x].sgrade[0];
cout << "Please enter the students second grade" << endl;
cin >> Students[x].sgrade[1];
cout << "Please enter the students third grade" << endl;
cin >> Students[x].sgrade[2];
cout << "Please enter the students fourth grade" << endl;
cin >> Students[x].sgrade[3];

}

}
void Calc(student Students[]){

double grade=0.0;
double total;
total = 0;
for (int x = 0; x < 10; x++) {

total = Students[x].sgrade[0] + Students[x].sgrade[1] + Students[x].sgrade[2] + Students[x].sgrade[3];

Students[x].fgrade = total / 4;


}
return ;
}




void output(student Students[], double highestValue)
{
// Local variables
int x,
y,
z;
// Display passing students.
cout << "PASS" << endl;
cout << "--------------------------------------------------------------" << endl;
for (int x = 0; x < 10; x++)
{
if (Students[x].fgrade > 59)
{
cout << "Name" << setw(30) << "ID" << setw(30) << "Grade" << endl;
cout << Students[x].SLname << " , "
<< Students[x].SFname << setw(30) << right
<< Students[x].Sid << setw(30) << right << Students[x].lgrade << endl << endl;
}
}
// Displays failing students
cout << "FAIL" << endl;
cout << "--------------------------------------------------------------" << endl;
for (int y = 0; y < 10; y++)
{
if (Students[x].fgrade < 60)
{
cout << "Name" << setw(30) << "ID" << setw(30) << "Grade" << endl;
cout << Students[x].SLname << " , "
<< Students[x].SFname << setw(30) << right
<< Students[x].Sid << setw(30) << right << Students[x].lgrade << endl << endl;
}
}

cout << "HIGHEST SCORE" << endl;
cout << "--------------------------------------------------------------" << endl;
for (z = 0; z < 10; z++)
{
if (Students[x].fgrade == highestValue)

cout << "Name" << setw(30) << "ID" << setw(30) << "Grade" << endl;
cout << Students[x].SLname << " , "
<< Students[x].SFname << setw(30) << right
<< Students[x].Sid << setw(30) << right << Students[x].lgrade << endl << endl;
}


void gradecalc(student Students[]) {
int x=0;
for (int x = 0; x < 10; x++) {
if (Students[x].fgrade <= 100 && Students[x].fgrade >= 90)
Students[x].lgrade = 'A';
else if (Students[x].fgrade >= 80 && Students[x].fgrade <= 89)
Students[x].lgrade = 'B';
else if (Students[x].fgrade >= 70 && Students[x].fgrade <= 79)
Students[x].lgrade = 'C';
else if (Students[x].fgrade >= 60 && Students[x].fgrade <= 69)
Students[x].lgrade = 'D';
else if (Students[x].fgrade >= 0 && Students[x].fgrade <= 59)
Students[x].lgrade = 'F';
cout << "Student " << x + 1 << " grade is " << Students[x].lgrade << endl;
}




}

char bestStudent(student Students[])
{

int x;

double highScore = Students[0].fgrade;
for (x = 0; x < 10; x++)

if (Students[x].fgrade > highScore)
highScore = Students[x].fgrade;
}

return highScore;
}
i see its the gradecalc but i dont understand why i wants something before the {
The only issue with my code right now is getting the output to align into a table which is a quick fix. It compiles perfectly fine, but the letter is not showing up as it is assigned. I just typed in an F letter grade and got the letter alpha back.
Last edited on
well i fixed majority of the issues but now is saying highscore in beststudent isnt defined when i already defined it


#include <iostream>
#include<string>
#include<iomanip>
#include<cmath>

using namespace std;
struct student {
string SFname;
string SLname;
int Sid;
double sgrade[4];
double fgrade;
char lgrade;
};


void output(student Students[], double highestValue);
void input(student Students[], int &x);
void Calc(student Students[]);
void gradecalc(student Students[]);
char bestStudent(student Students[]);
int main()
{
double highscore;
double fgrade = 0.0;
int x = 0;
//double fgrade;
student Students[10];
input(Students, x);
Calc(Students);
gradecalc(Students);
bestStudent(Students);


cin.get(); cin.get();
return 0;
}
void input(student Students[], int &x){

for (int x = 0; x < 10; x++) {
cout << "Please enter Students first name" << endl;
cin >> Students[x].SFname;
cout << "Please enter Students last name" << endl;
cin >> Students[x].SLname;
cout << "Please enter the Students ID" << endl;
cin >> Students[x].Sid;
cout << "Please enter the students first grade" << endl;
cin >> Students[x].sgrade[0];
cout << "Please enter the students second grade" << endl;
cin >> Students[x].sgrade[1];
cout << "Please enter the students third grade" << endl;
cin >> Students[x].sgrade[2];
cout << "Please enter the students fourth grade" << endl;
cin >> Students[x].sgrade[3];

}

}
void Calc(student Students[]){

double grade=0.0;
double total;
total = 0;
for (int x = 0; x < 10; x++) {

total = Students[x].sgrade[0] + Students[x].sgrade[1] + Students[x].sgrade[2] + Students[x].sgrade[3];

Students[x].fgrade = total / 4;


}
return ;
}




void output(student Students[], double highestValue)
{

int x,
y,
z;

cout << "PASS" << endl;
cout << "--------------------------------------------------------------" << endl;
for (int x = 0; x < 10; x++)
{
if (Students[x].fgrade > 59)
{
cout << "Name" << setw(30) << "ID" << setw(30) << "Grade" << endl;
cout << Students[x].SLname << " , "
<< Students[x].SFname << setw(30) << right
<< Students[x].Sid << setw(30) << right << Students[x].lgrade << endl << endl;
}
}
// Displays failing students
cout << "FAIL" << endl;
cout << "--------------------------------------------------------------" << endl;
for (int y = 0; y < 10; y++)
{
if (Students[x].fgrade < 60)
{
cout << "Name" << setw(30) << "ID" << setw(30) << "Grade" << endl;
cout << Students[x].SLname << " , "
<< Students[x].SFname << setw(30) << right
<< Students[x].Sid << setw(30) << right << Students[x].lgrade << endl << endl;
}
}

cout << "HIGHEST SCORE" << endl;
cout << "--------------------------------------------------------------" << endl;
for (z = 0; z < 10; z++)
{
if (Students[x].fgrade == highestValue)

cout << "Name" << setw(30) << "ID" << setw(30) << "Grade" << endl;
cout << Students[x].SLname << " , "
<< Students[x].SFname << setw(30) << right
<< Students[x].Sid << setw(30) << right << Students[x].lgrade << endl << endl;
};


void gradecalc(student Students[]);
{
int x=0;
for (int x = 0; x < 10; x++) {
if (Students[x].fgrade <= 100 && Students[x].fgrade >= 90)
Students[x].lgrade = 'A';
else if (Students[x].fgrade >= 80 && Students[x].fgrade <= 89)
Students[x].lgrade = 'B';
else if (Students[x].fgrade >= 70 && Students[x].fgrade <= 79)
Students[x].lgrade = 'C';
else if (Students[x].fgrade >= 60 && Students[x].fgrade <= 69)
Students[x].lgrade = 'D';
else if (Students[x].fgrade >= 0 && Students[x].fgrade <= 59)
Students[x].lgrade = 'F';
cout << "Student " << x + 1 << " grade is " << Students[x].lgrade << endl;
}




}

char bestStudent(student Students[]);
{

int x;

double highScore = Students[0].fgrade;
for (x = 0; x < 10; x++)

if (Students[x].fgrade > highScore)
highScore = Students[x].fgrade;
}

return highScore;
}
i am still having issues compiling it saying i have a undefined variable but shows no error until i attempt to run it

#include <iostream>
#include<string>
#include<iomanip>
#include<cmath>

using namespace std;
struct student {
string SFname;
string SLname;
int Sid;
double sgrade[4];
double fgrade;
char lgrade;
};


void output(student Students[], double highestValue);
void input(student Students[], int &x);
void Calc(student Students[]);
void gradecalc(student Students[]);
double bestStudent(student Students[]);
int main()
{
double highScore;
double fgrade = 0.0;
int x = 0;
//double fgrade;
student Students[10];
input(Students, x);
Calc(Students);
gradecalc(Students);
bestStudent(Students);


cin.get(); cin.get();
return 0;
}
void input(student Students[], int &x){

for (int x = 0; x < 10; x++) {
cout << "Please enter Students first name" << endl;
cin >> Students[x].SFname;
cout << "Please enter Students last name" << endl;
cin >> Students[x].SLname;
cout << "Please enter the Students ID" << endl;
cin >> Students[x].Sid;
cout << "Please enter the students first grade" << endl;
cin >> Students[x].sgrade[0];
cout << "Please enter the students second grade" << endl;
cin >> Students[x].sgrade[1];
cout << "Please enter the students third grade" << endl;
cin >> Students[x].sgrade[2];
cout << "Please enter the students fourth grade" << endl;
cin >> Students[x].sgrade[3];

}

}
void Calc(student Students[]){

double grade=0.0;
double total;
total = 0;
for (int x = 0; x < 10; x++) {

total = Students[x].sgrade[0] + Students[x].sgrade[1] + Students[x].sgrade[2] + Students[x].sgrade[3];

Students[x].fgrade = total / 4;


}
return ;
}




void output(student Students[], double highestValue)
{

int x,
y,
z;

cout << "PASS" << endl;
cout << "--------------------------------------------------------------" << endl;
for (int x = 0; x < 10; x++)
{
if (Students[x].fgrade > 59)
{
cout << "Name" << setw(30) << "ID" << setw(30) << "Grade" << endl;
cout << Students[x].SLname << " , "
<< Students[x].SFname << setw(30) << right
<< Students[x].Sid << setw(30) << right << Students[x].lgrade << endl << endl;
}
}

cout << "FAIL" << endl;
cout << "--------------------------------------------------------------" << endl;
for (int y = 0; y < 10; y++)
{
if (Students[x].fgrade < 60)
{
cout << "Name" << setw(30) << "ID" << setw(30) << "Grade" << endl;
cout << Students[x].SLname << " , "
<< Students[x].SFname << setw(30) << right
<< Students[x].Sid << setw(30) << right << Students[x].lgrade << endl << endl;
}
}

cout << "HIGHEST SCORE" << endl;
cout << "--------------------------------------------------------------" << endl;
for (z = 0; z < 10; z++)
{
if (Students[x].fgrade == highestValue)

cout << "Name" << setw(30) << "ID" << setw(30) << "Grade" << endl;
cout << Students[x].SLname << " , "
<< Students[x].SFname << setw(30) << right
<< Students[x].Sid << setw(30) << right << Students[x].lgrade << endl << endl;
}

}
void gradecalc(student Students[])
{
int x=0;
for (int x = 0; x < 10; x++) {
if (Students[x].fgrade <= 100 && Students[x].fgrade >= 90)
Students[x].lgrade = 'A';
else if (Students[x].fgrade >= 80 && Students[x].fgrade <= 89)
Students[x].lgrade = 'B';
else if (Students[x].fgrade >= 70 && Students[x].fgrade <= 79)
Students[x].lgrade = 'C';
else if (Students[x].fgrade >= 60 && Students[x].fgrade <= 69)
Students[x].lgrade = 'D';
else if (Students[x].fgrade >= 0 && Students[x].fgrade <= 59)
Students[x].lgrade = 'F';
cout << "Student " << x + 1 << " grade is " << Students[x].lgrade << endl;
}




}

double bestStudent(student Students[])
{

int x;

double highScore = Students[0].fgrade;
for (x = 0; x < 10; x++)
{
if (Students[x].fgrade > highScore)
highScore = Students[x].fgrade;
}

return highScore;

}
its saying that it is the X right after the FAIL output
closed account (48T7M4Gy)
@OP use code tags, specify the line number you are talking about and give a listing of the error message in full.
:)
I got it to compile so that is good but the output i all messed up also the bestStudent function seems to not be working as it is not picking which student has the highest score
closed account (48T7M4Gy)
@OP So extract the offending function, make up some test data instead of all the menu and typing and write a short test program that can be pasted back in. When you've done that show us the program, test data and the results and maybe help is possible. Don't forget proper code tags as requested before.
Topic archived. No new replies allowed.
Pages: 123