Unable to Call Function

Pages: 123
Hello I cant see to call one of my functions and i am not sure why if anyone could help figure it out that would be appreciated

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

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

};




void input(student Students[10], int x);


int main()
{

int x;
int size;
student all;
student Students[10];

input();

cin.get(); cin.get();
return 0;
}
void input(student Students[10], 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];
}

}
Help to read your code would be called code tags.


http://www.cplusplus.com/articles/jEywvCM9/

I am aware of the tags i must not of explained myself correctly. i am more asking for how to fix what is wrong it says i have too few arguments and anything i try to use to call the function just fails
This is your function prototype:
void input(student Students[10], int x);

It expects to receive two arguments.

This is how you try to call your function:
input();

You're not passing it any arguments.
Hi, your proper function call should look like
 
input(Students, x);


You will find that your code has a logic error.
Okay this seems to have worked but now i am running into new errors. i did add a couple things which i'm sure is the problem. Basically what i am trying to do is input data for 10 separate students each of which would have 4 separate test scores. i then want to add each students score to figure out a final grade. i then want to compare the the 10 students to find who had the highest overall grade.

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

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

};




void input(student Students[10], int x,double fgrade);


int main()
{

int x;
double fgrade;
student Students[10];
input(Students, x, fgrade);


cin.get(); cin.get();
return 0;
}
void input(student Students[10], int x,double fgrade){
while (x < 10) {
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];
}
}
while (x < 10) {
for (int x = 0; x < 10; x++)
fgrade = Students[x].sgrade[0] + Students[x].sgrade[1] + Students[x].sgrade[2] + Students[x].sgrade[3];

}
}
I can't honestly help you until you tell any future helper more of what your code is trying to do. Are you trying to use ACII values of the character to determine or are you converting these letter grades to percentage scores?
I am trying to turn percentage into a letter grade i then want to find the average of each student then compare all 10 students and find which student has the highest grade of them all.i am aware the code is no where near done. i am relatively new to functions and arrays. if possible id like to have a calculation function that does all the math need.
This is an example of an array and function together,

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
// Header file
#include <iostream>
// Using namespace standard
using namespace std;
// Function declarations
void Display(int []);
// Main
int main()
{
	// Variable declaration.
	int x,
		y = 0;
	// Constant declaration
	const int MAX_STUDENTS = 10;
	// Array that has zero initialized in every address.
	int studentScores[MAX_STUDENTS] = {0};
	// Prompt.
	cout << "One score per student!";
	cout << endl << endl;
	// For loop
	for (x = 0; x < MAX_STUDENTS; x++)
	{
		cout << "Enter a score for student " << x + 1<< " ";
		cin >> y;
		studentScores[x] = y;
		cout << endl;
	}
	
	cout << endl << endl;
	// Function call, passing the parameter studentScores
	Display(studentScores);
	
	return 0;
}
// Function
void Display(int Scores[10]) // Scores is initialized to hold ten values
{
	// Local variable declarations
	int x;
	// For loop to display passed array studentScores..
	for (x = 0; x < 10; x++)
	{
		cout << "Student " << x + 1 << " score  was " << Scores[x];
		cout << endl;
	}
}


What you have is a structure. Also, if you want the percentages entered don't bother converting them.



Last edited on
Basically what i need to do is add 4 separate scores from each student so for instance student 1 get a 96 a 78 a 83 and a 90 so i need the average of those scores which is 86.75. so if i use a 10% grading scale 100-90 is an A 90-80 is a B 80-70 is a C 70-60 is a D and anything below a 60 can be considered an F so once i have all 10 students final grades ill have to check them like

if(student[x].fgrade<=90)=A if (student[x].fgrade <=89 || == 80) =B and so on but i then need to take the final calculated grades from all 10 students so like

student 1 =100 (A)
Student 2 =60(F)
Student 3=99(A)
it would then display student 1 for being the highest.

i also need to seperate the students those who have passed letter grade C or higher and those who have not D or lower.
the out put of each i would need to look as such


lastname , firstname studentID lettergrade


and so forth. i realize this is a lot but i am kind of in a pickle right now i am still having issues with my current code saying variables are unlocalized



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

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

};




void input(student Students[10], int x,double fgrade);


int main()
{

int x;
double fgrade;
student Students[10];
input(Students, x, fgrade);


cin.get(); cin.get();
return 0;
}
void input(student Students[10], int x,double fgrade){
while (x < 10) {
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];
}
}
while (x < 10) {
for (int x = 0; x < 10; x++)
fgrade = Students[x].sgrade[0] + Students[x].sgrade[1] + Students[x].sgrade[2] + Students[x].sgrade[3];

}
}

it seems to be the variable fgrade and x in the input()
I am not very familiar with structures, but this seems to have solved your problem.

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
// Header files
#include <iostream>
#include<string>
// Using namespace standard
using namespace std;
// Structure student
struct student
{
string SFname;
string SLname;
int Sid;
double sgrade[4];
double fgrade;
};
// Function header
void input(student Students[10], int &x); // X is passed by reference
// Main
int main()
{
	// Variable initiliazed to zero because it is a counter.
	int x = 0;
	// Object declared
	student Students[10];
	// 10 function calls
	input(Students, x); // Students is being passed an address to the array.
	input(Students, x);
	input(Students, x);
	input(Students, x);
	input(Students, x);
	input(Students, x);
	input(Students, x);
	input(Students, x);
	input(Students, x);
	input(Students, x);

return 0;
}
// Function
void input(student Students[10], int &x) // X is passed by reference
{
	// Enter data into structure!
	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];
        x++ // This is a part of the counter.
}


I took out what I thought was unnecessary during this stage to be able to show you what is needed at this point.
Last edited on
Okay so i edited and got it to run to where it calls the function and i am able to enter data now but once i enter 10 students i goes into a loop that never ends. any suggestions?
once this working i do need a way to calculate the average for each student which is just grade 1 grade 2 grade 3 and grade 4 added together then divided by 4 but to pass the perimeters from the first function to another is where i will end up running into an issue.
#include <iostream>
#include<string>
#include<iomanip>
#include<cmath>

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

};




void input(student Students[10], int x);


int main()
{

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


cin.get(); cin.get();
return 0;
}
void input(student Students[10], int x){
while (x <= 10) {
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];
}
}
/* while (x < 10) {
for (int x = 0; x < 10; x++)
fgrade = Students[x].sgrade[0] + Students[x].sgrade[1] + Students[x].sgrade[2] + Students[x].sgrade[3];

// }*/
}
Here is what I came up with based on what you gave me to correct your program,
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
// Header files
#include <iostream>
#include<string>
// Using namespace standard
using namespace std;
// Structure student
struct student
{
string SFname;
string SLname;
int Sid;
double sgrade[4];
double fgrade;
};
// Function header
void input(student Students[10], int &x); // X is passed by reference
void finalGrade(student Students[10]);
// Main
int main()
{
	// Variable initiliazed to zero because it is a counter.
	int x = 0;
	// Object declared
	student Students[10];
	// 10 function calls
	input(Students, x); // When students is passed, it passes the array.
	input(Students, x);
	input(Students, x);
	input(Students, x);
	input(Students, x);
	input(Students, x);
	input(Students, x);
	input(Students, x);
	input(Students, x);
	input(Students, x);
	// Final grade calculator.
	finalGrade(Students);

return 0;
}
// Function
void input(student Students[10], int &x) // X is passed by reference
{
	// Enter data into structure!
	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];
	x++;
}
// Final grade function
void finalGrade(student Students[10])
{	
	// 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
		cout << endl << "Student " << i + 1 << " average was " << Students[i].fgrade; // Output
	}
}
Last edited on
Okay i made the changes the cout in the last function will have to go seeing as it still is not done. i now need to to all 10 students fgrade and seperate them on there letter grade. to i will need to convert them then order them according to what their grade is.
#include <iostream>
#include<string>
#include<iomanip>
#include<cmath>

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

};




void input(student Students[10], int &x);
void Calc(student Students[10]);

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

cin.get(); cin.get();
return 0;
}
void input(student Students[10], 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[10]){

double grade;
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];

cout << Students[x].SFname << x + 1 << "'s Average" << Students[x].fgrade = total / 4;
}

return ;
}



cout << Students[x].SFname << x + 1 << "'s Average" << Students[x].fgrade = total / 4;
the = sign in this line is causing problems. "binary '=': no operator found which take right-hand operand of type 'double' (or there is no acceptable conversion)
Last edited on
I honestly will let you show me code to fix. I won't do it for you. I don't know what you mean by separate, that is basically what I did. Each student is getting a final grade based on their average score. If you want to convert fgrade to a letter grade, try using a decision statement (and maybe a new data member called lgrade).

 
Students[x].fgrade = total / 4;


This should be on a line before the cout statement. I think the reason why you are having errors is because your assigning values in a cout statement.
Last edited on
Okay i wnet through and i think i have everything i need done only issue it passing the parameters between the functions. #include <iostream>
#include<string>
#include<iomanip>
#include<cmath>

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

};


void output();
int largest(const int list[], int low, int high);
void input(student Students[10], int &x);
void Calc(student Students[10]);

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

cin.get(); cin.get();
return 0;
}
void input(student Students[10], 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[10]){

double grade;
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 ;
}

int largest(const int list[], int low, int high) {
int max;
if (low == high)
return list[low];
else {
max = largest(list, low + 1, high);
if (list[low] >= max)
return list[low];
else
return max;
}
}
void output() {
for (int x = 0; x < 10; x++) {
cout << "Name" << setw(50) << "ID" << setw(50) << "Grade" << endl
cout << Students[x].SLname << setw(30) << right << "," << Students[x].SFname << setw(50) << Students.Sid << setw(50) << Students.fgrade << endl;
}
}

void gradecalc() {
char grade;
if (Students[x].fgrade <= 90)
grade = 'A'
if (Students[x].fgrade <= 80 || == 89)
grade = 'B'
if (Students[x].fgrade <= 70 || == 79)
grade = 'C'
if (Students[x].fgrade <=69||==60)
grade='D'
if (Students[x].frgrade <60)
grade='F'

//need an array with largest function not sure what

}

This should work, if it doesn't let me know.

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
// Header files
#include <iostream>
#include<string>
// 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[], int &x); // X is passed by reference
void finalGrade(student Students[]);
void letterGrade(student Students[]);
// Main
int main()
{
	// Variable initiliazed to zero because it is a counter.
	int x = 0;
	// Constant declared and initialized.
	const int MAX_STUDENTS = 10;
	// Object declared
	student Students[MAX_STUDENTS];
	// 10 function calls
	input(Students, x); // When students is passed, it passes the array.
	input(Students, x);
	input(Students, x);
	input(Students, x);
	input(Students, x);
	input(Students, x);
	input(Students, x);
	input(Students, x);
	input(Students, x);
	input(Students, x);
	// Final grade calculator.
	finalGrade(Students);
	// Letter grade
	letterGrade(Students);
	
return 0;
}
// Function
void input(student Students[], int &x) // X is passed by reference
{
	// Enter data into structure!
	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];
	x++;
	cout << 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;
        // This for loop goes through all students and assigns a letter grade.
	for (i = 0; i < 10; i++)
	{
                // If...else if statement that determines the letter grade.
		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';
		cout << "Student " << i + 1 << " grade is " << Students[i].lgrade << endl; // Output line
	}
}
Last edited on
okay so i changed a couple things and added your suggestions. i commented out stuff like the output function since we arnt working on that right now. i ran the program and it compiled but after entering all the data for all 10 students it just stops. nothing else happens until the program is closed. #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();
int largest(const int list[], int low, int high);
void input(student Students[10], int &x);
void Calc(student Students[10]);
void gradecalc(student Students[]);

int main()
{
double fgrade = 0.0;
int x = 0;
//double fgrade;
student Students[10];
input(Students, x);
Calc(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 ;
}

int largest(const int list[], int low, int high) {
int max;
if (low == high)
return list[low];
else {
max = largest(list, low + 1, high);
if (list[low] >= max)
return list[low];
else
return max;
}
}
/*void output() {
for (int x = 0; x < 10; x++) {
cout << "Name" << setw(50) << "ID" << setw(50) << "Grade" << endl;
cout << Students[x].SLname << setw(30) << right << "," << Students[x].SFname << setw(50) << Students.Sid << setw(50) << Students.fgrade << endl;
}
}*/

void gradecalc(student Students[]) {
int x=0;
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;

//need an array with largest function not sure what

}
Did you mean you commented the input function or do you mean this,

 
cout << "Student " << i + 1 << " grade is " << Students[i].lgrade << endl;


There is a logic error then if it is not showing anything.
if you look i added two functions that arnt implemented yet the void output function and the int largest function. the largest function is meant to take all the fgrade and compare them to see who has the highest of the 10. and i may have found the problem give me one second
Pages: 123