Array of Structs: Function calls “Undefined” even though they are declared

I am getting 6 errors all pertaining to my function calls in Main. It is saying each identifier is "undefined" even though they are declared within the struct def. When I take them out of the struct def and put them just above Main I still get the undefined error for the function calls. In the struct examples I have found, my syntax should not produce errors. I realized that I need to remove the "StudentType compStudents[]" parameter out of the declarations if I am going to leave them inside the struct def though. After I get that worked out, I can compile to test if my code to print the results works. Thank you.

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

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string> 

using namespace std;

struct StudentType//declares struct type Student
//defines it having the members within the brackets
{
int IDnumber;
string Firstname;
string Middlename;
string Lastname;
int Examgrade1;
int Examgrade2;
int Examgrade3;
int Labgrade1;
int Labgrade2;
int Labgrade3;
int Labgrade4;
char Lettergrade;

void Read_StudentData(ifstream&In_Data, StudentType compStudents[]);
//function read grades from a file including all 3 exams and all 4 labs
float Calc_Final_Grade(int Total_Examgrade, int Total_Labgrade, StudentType compStudents[]);
//function calculates the final grade of each of the 20 students
char AssignGrade(float Final_grade, StudentType compStudents[]);
//function outputs a letter grade according to the student's calculated numeric final grade
//Grading scale: 100-90: A , 90-80: B, 80-70: C, 70-0: D: Failure
float Calc_Average_Grade(int Average_Examgrade, int Average_Labgrade, StudentType
    compStudents[]);
//function calculates average grade of the class
void Display_Result(ofstream Out_Data, StudentType compStudents[]);
//Postconditions: Out will be a horizontal "list" of each of the 20 students' full name, 
//each of the 3 exam grades, each of the 4 lab grades, final grade.
    //The output will be sent to screen and an output file
};

StudentType compStudents[20];//initializes an array called students of the type StudentType

//to "access" any student's ID number: Student.IDnumber
//Student.Labgrade1;//would access the first lab grade for each of the 20 students

ifstream In_Data;
ofstream Out_Data;
int Total_Examgrade, Total_Labgrade;
int Average_Examgrade, Average_Labgrade;


int main()

{
Read_StudentData(In_Data, compStudents);
Calc_Final_Grade(Total_Examgrade, Total_Labgrade, compStudents);
AssignGrade(Final_grade, compStudents);
Calc_Average_Grade(Average_Examgrade, Average_Labgrade, compStudents);
Display_Result(Out_Data, compStudents);
return 0;
}
I think the }; of line 39 should be removed and put in line 24
Yes, that takes care of that problem, however, when I do that, then the variables in this function are now being called "undefined".

But despite that, from my book and the examples I have seen online, I should be able to put the function declarations either at line 39 or 24.

Also for the variables in the function listed below (such as Examgrade1, Examgrade2, etc, they should be considered defined because they were defined in the struct StudentType def.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

float StudentType::Calc_Final_Grade(int Total_Examgrade, int Total_Labgrade, StudentType compStudents[])

{
	int Total_Examgrade = (Examgrade1 + Examgrade2 + Examgrade3);
	//just noting Average_Examgrade = (Total_Examgrade / 3);
	//return (Total_Examgrade / 3)//use for cout

	int Total_Labgrade = (Labgrade1 + Labgrade2 + Labgrade3 + Labgrade4);
	//just noting Average_Labgrade = (Total_Labgrade / 4);
	//return (Total_Labgrade / 4)//use for cout

	float Final_grade = ((.60 * Total_Examgrade) + (.40 * Total_Labgrade));
	return Final_grade;
}


As long as your class member functions are properly scoped when you implement them (as done in the above snippet) you should have no problems accessing the variables from that function.

As for the problem with main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
StudentType compStudents[20];//initializes an array called students of the type StudentType

//to "access" any student's ID number: Student.IDnumber
//Student.Labgrade1;//would access the first lab grade for each of the 20 students

ifstream In_Data;
ofstream Out_Data;
int Total_Examgrade, Total_Labgrade;
int Average_Examgrade, Average_Labgrade;


int main()

{
Read_StudentData(In_Data, compStudents);
Calc_Final_Grade(Total_Examgrade, Total_Labgrade, compStudents);
AssignGrade(Final_grade, compStudents);
Calc_Average_Grade(Average_Examgrade, Average_Labgrade, compStudents);
Display_Result(Out_Data, compStudents);
return 0;
}


First why all the global variables, make the variable local to the function where they are required, main() in this case.
Next your functions are part of your class so you need to use tell the compiler what instance of the class you want to use:
1
2
3
4
5
6
...

   StudentType student;
...
   student.Calc_Final_Grade(Total_Examgrade, Total_Labgrade, compStudents);


But despite that, from my book and the examples I have seen online, I should be able to put the function declarations either at line 39 or 24.


Yes. But if your function is in the struct , then you need an object in main to access the functions.
Something like
1
2
StudentType student;
student.Read_StudentData(In_Data, compStudents);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
float StudentType::Calc_Final_Grade(int Total_Examgrade, int Total_Labgrade, StudentType compStudents[])

{
	int Total_Examgrade = (Examgrade1 + Examgrade2 + Examgrade3);
	//just noting Average_Examgrade = (Total_Examgrade / 3);
	//return (Total_Examgrade / 3)//use for cout

	int Total_Labgrade = (Labgrade1 + Labgrade2 + Labgrade3 + Labgrade4);
	//just noting Average_Labgrade = (Total_Labgrade / 4);
	//return (Total_Labgrade / 4)//use for cout

	float Final_grade = ((.60 * Total_Examgrade) + (.40 * Total_Labgrade));
	return Final_grade;
}

Why are you redefining Total_Examgrade and Toatl_Labgrade in line 4 and 8?
Examgrade1 ,Examgrade2 ,Examgrade3 are all undefined in this scope.
compStudents[] contain structs that contain those variables. You can only acess the variable thought it.
The main confusion I'm having is the Main function calls...

No matter what I do, the compile keeps finding issues with these function calls...saying that the parameters being passed are undeclared identifiers.

1
2
3
Calc_GPA(Examgrade, Labgrade, CompStud);
LetterGrade(GPA, CompStud);
Display_File(Out_Data, CompStud);


Student is the struct tag creating a "type" (just like int or float) and CompStud[size] means there are 20 compsci students who have the members (listed in the struct def) of "type" Student...this is how I understand it...let me know if that's wrong??

I have moved them around and tried different things with both the functions and objects w/accessors but still getting errors.

I have also tried moving the Student CompStud[size]; around and put it both in and out of Main.

I'm not concerned about errors related to my GPA[size] array as right now I'm just try to get the function calls so they'll work.

Thanks.

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

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

const int size = 20;//change here to change array size

struct Student//declares struct type Student
	//default public members
{
	string Firstname, Middlename, Lastname;
	float GPA;//each student has 1 GPA
	char Lettergrade;
	//int Examgrade1, Examgrade2, Examgrade3;
	int Examgrade[3];
	//int Labgrade1, Labgrade2, Labgrade3, Labgrade4;
	int Labgrade[4];

};//<----YES I PUT SEMICOLON

//to "access" any student's ID number: Student.IDnumber
//to "access" a student's 1st exam grade: Student.Examgrade[0]

struct Student Compstud[size];
//initializes an array within type Student to hold 'size' Compsci students

//function declarations
void Initialize(Student s[]);
//function initializes all the elements to 0

void Read_Data(ifstream& In_Data, Student s[]);
//function read values from a file, these values will be used for calculations
//postcondition: replaces 0 initialization with those values

float Calc_GPA(int Examgrade[], int Labgrade[], Student s[]);
//function calculates the GPA of each of the students
//Postcondition: Able to use the return values of this function to assign letter grade

char LetterGrade(float GPA, Student s[]);
//Precondition: Student has a GPA calculated
//function outputs a letter grade according to the student's calculated GPA
//Grading scale: 100-90: A , 90-80: B, 80-70: C, 70-0: D: Failure

float calc_ClassAverage(int Examgrade[], int Labgrade[], Student s[]);
//Precondition: Each student has a GPA calculated
//function uses all student's GPA to calculates average grade of the class
//use pointer to point to each student's GPA and sum them up

void Display_File(ofstream Out_Data, Student s[]);
//Postconditions: 
//Outputs each of the 20 students' full name, 3 exam grades,
//each of the 4 lab grades, and final grade to an output file
//Outputs the average GPA of whole class

void Display_Screen(Student s[]);
//Postconditions: 
//Outputs each of the 20 students' full name, 3 exam grades,
//each of the 4 lab grades, and final grade to the screen
//Outputs the average GPA of whole class

ifstream In_Data;
ofstream Out_Data;
float ClassAverage;

int main()
{
	CompStud *ptr;//declare pointer to student's GPA (s[i].GPA)

	//function calls begin
	Initialize(CompStud);
	Read_Data(In_Data, CompStud);
	Calc_GPA(Examgrade, Labgrade, CompStud);
	LetterGrade(GPA, CompStud);
	calc_ClassAverage(Examgrade, Labgrade, CompStud);
	Display_File(Out_Data, CompStud);
	Display_Screen(CompStud);
}
Last edited on
compStudents[size] is out of range.
Look at your code more critically.
Thank you, I'm still getting a bunch of errors in main function calls. I changed my code above to reflect what it looks like now.
Topic archived. No new replies allowed.