Structures and Arrays: error code: "local function definitions are illegal"

This is my next assignment.:

Write a program that reads a student's name followed by their test score. The program should output each students name followed by the test scores and the relevant grade. It should also find and print the highest test score and the name of the students having the highest test score.

Student data should be stored in a struct variable of type studentType, which has four components: studentFName and studentLName of type string, testscore of type int (testscore is between 0 and 100), and grade of type char. Suppose that the class has 20 students. Use an array of 20 components of type studentType.

The program must contain at least the following functions:

a.) a function to read the students data into the array.'
b.) a function to assign the relevant grade to each student.
c.) a function to find the highest test score.
d.) a function to print the names of the students having the highest test score.

The program must output each students name in this form: last name followed by a comma, followed by a space, followed by the 1st name, and the name must be left-justified. Moreover, other than declaring the variables and opening the input and output files, the function main should only be a collection of functions calls.








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
//header files
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>


//namespace
using namespace std;

//constant
const int NO_OR_STUDENTS = 20;

//struct
struct studentType
{
	string studentFName;
	string studentLName;
	int testScore;
	char grade;
};


void getData(ifstream& inFile, studentType sList[], int listSize);
void calculateGrade(studentType sList[], int listSize);
int highestScore(const studentType sList[], int listSize);
void printResult(ofstream& outFile, const studentType sList[], int listSize);

//input output file
int main()
{
	ifstream inData;
	ofstream outData;
	studentType studentList[NO_OR_STUDENTS];
	inData.open("Ch9_Ex2Data.txt");
	if (!inData)
	{
		cout << "The input file does not exist. Program terminates!"
			<< endl;
		system("PAUSE");
		return 1;
	}//endif


	outData.open("Ch9_Ex2Out.txt");
	if (!outData)
	{
		cout << "Cannot open the output file. Program terminates!"
			<< endl;
		system("PAUSE");
		return 1;
	}//endif


	getData(inData, studentList, NO_OR_STUDENTS);
	calculateGrade(studentList, NO_OR_STUDENTS);
	printResult(outData, studentList, NO_OR_STUDENTS);
	system("PAUSE");
	return 0;
}//endmain

//get data
void getData(ifstream& inFile, studentType sList[], int listSize)
{
	for (int i = 0; i < listSize; i++)
		inFile >> sList[i].studentFName >> sList[i].studentLName
		>> sList[i].testScore;
}//endgetData

//calculate grade
void calculateGrade(studentType sList[], int listSize)
{
	int score;
	for (int i = 0; i < listSize; i++)
		if (score >= 90)
			sList[i].grade = 'A';
		else if (score >= 80)
			sList[i].grade = 'B';
		else if (score >= 70)
			sList[i].grade = 'C';
		else if (score >= 60)
			sList[i].grade = 'D';
		else
			sList[i].grade = 'F';

		
}//endcalculateGrade

//highest score
int highestScore(const studentType sList[], int listSize)
{
	int score[100];
	int highscore = score[0];
	for (int i = 0; i < listSize; i++) 
	{

		if (score[i] > highscore)
		{
			highscore = score[i];
		}


	}//endhighestScore

	// print result
	void printResult(ofstream& outFile, const studentType sList[], int listSize) // I'm getting error codes here saying " 'printResult': local function definitions are illegal"
	{
		int maxScore = highestScore(sList, listSize);
		int i;
		outFile << setw(15) << "Student Name "
			<< setw(10) << "Test Score"
			<< setw(7) << "Grade" << endl;
		for (i = 1; i < listSize; i++)
			outFile << left << setw(25)
			<< sList[i].studentLName + ", " + sList[i].studentFName
			<< right << " " << setw(5) << sList[i].testScore
			<< setw(6) << " " << sList[i].grade << endl;
		outFile << endl << "Highest Test Score: " << maxScore << endl;
		outFile << "Students having the highest test score:" << endl;
		for (i = 1; i < listSize; i++)
			if (sList[i].testScore == maxScore)
				outFile << sList[i].studentLName + ", " + sList[i].studentFName << endl;
	}//endprintResult

//I'm also getting an error code at the end saying "end of file found before left brace" 
[/code]
Last edited on
You havent close highest score function, the last } only close for statement, not the function
Okay, so after I put the missing } in, I get an error code that reads: " 'highest score' : must return value. What do I do now? What am I missing?
You're missing a return value
Topic archived. No new replies allowed.