Code won't compile

Can someone fix this so it could compile? 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
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
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

const int NO_OR_STUDENTS = 20;


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);


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


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


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


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;


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




int highestScore(const studentType sList[], int scores[], int listSize)
{
    int highestScore = scores[0];
    for (int i = 0; i < listSize; i++)
     {

        if (scores[i] > highestScore)
         {
            highestScore = scores[i];
        }

}    


void printResult(ofstream& outFile, const studentType sList[], int listSize)
{
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;
}
Last edited on
Hi,


Code won't compile


Always post your compile errors (copy & paste) here - it saves us having to do it.

On lines 102 & 109 your for loops will cause the first element in sList to be missed, and an over run of the array, because arrays start at 0. The normal idiom for a a for loop is:

1
2
3
4
for (i = 0; i < listSize; i++) {
  // your code here

}


You are probably missing braces for your for loops - always use them even if there is 1 LOC. This applies to any statement that can have a compound statement (statements in braces), such as if- then-else, while, switch etc.

Don't have line 6 - Google to see why , and what to do instead.

Hope all is well.

Here is the updated errors:

1>c:\users\me\documents\visual studio 2010\projects\hw22\ok22\we.cpp(68): warning C4700: uninitialized local variable 'score' used
1>c:\users\me\documents\visual studio 2010\projects\hw22\ok22\we.cpp(94): error C4716: 'highestScore' : must return a value
Last edited on
Hi,

The errors / warnings are quite clear:

score was never assigned a value. Do you need to extract that value out of the array?

The function highestScore is declared to return an int but it never does.

The for loop on line 104 still starts with 1 not zero.

I would add more braces, especially in the calculateGrade function - as I said earlier, use braces even if there is only 1 line of code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void calculateGrade(studentType sList[], int listSize)
{
  int score;
  for (int i = 0; i < listSize; i++)  { 
	if (score >= 90)  {
		sList[i].testScore = 'A'; 
        } 
	else if (score >= 80)  {
		sList[i].testScore = 'B'; 
        } 
	else if (score >= 70)  {
	   sList[i].testScore = 'C';  
        }
	else if (score >= 60)  {
		sList[i].testScore = 'D';
        }
	else  {
	  sList[i].testScore = 'F';
        }
  }
}


It would also be good if you improved the indentation - your IDE should do it automatically, and you can force it to tidy things up.

Hope all is well :+)

Here is the updated code:

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

using namespace std;

const int NO_OR_STUDENTS = 20;


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);


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

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


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

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

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


int highestScore(const studentType sList[], int scores[], int listSize)
{
    int highestScore = scores[0];
    for (int i = 0; i < listSize; i++)
     {

        if (scores[i] > highestScore)
         {
            highestScore = scores[i];

        }

}    
	return 0;
}


void printResult(ofstream& outFile, const studentType sList[], int listSize)
{
int maxScore = highestScore(sList, listSize);
int i;
outFile << setw(15) << "Student Name "
<< setw(10) << "Test Score"
<< setw(7) << "Grade" << endl;
for (i = 0; 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 = 0; i < listSize; i++) {
if (sList[i].testScore == maxScore)
outFile << sList[i].studentLName + ", " + sList[i].studentFName << endl;
}
}


But i'm getting this error now:
1>error LNK2019: unresolved external symbol "int __cdecl highestScore(struct studentType const * const,int)" (?highestScore@@YAHQBUstudentType@@H@Z) referenced in function "void __cdecl printResult(class std::basic_ofstream<char,struct std::char_traits<char> > &,struct studentType const * const,int)" (?printResult@@YAXAAV?$basic_ofstream@DU?$char_traits@D@std@@@std@@QBUstudentType@@H@Z)

1>C:\Users\Me\documents\visual studio 2010\Projects\hw22\Debug\ok22.exe : fatal error LNK1120: 1 unresolved externals
Last edited on
Line 106 calls a function highestScore that has been declared on line 22. However, there is no implementation for that function. There is a different function, an overload, on lines 87-101.

In other words, the
int highestScore(const studentType sList[], int listSize)
is not the same as
int highestScore(const studentType sList[], int scores[], int listSize)

You have to either implement the function that printResult calls, or make the printResult to call the already implemented function.
1
2
3
4
void getData(ifstream& inFile, studentType sList[], int listSize);
void calculateGrade(studentType sList[], int listSize);
int highestScore(const studentType sList[], int scores[], int listSize);
void printResult(ofstream& outFile, const studentType sList[], int listSize);



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int highestScore(const studentType sList[], int scores[], int listSize)
{
    int highestScore = scores[0];
    for (int i = 0; i < listSize; i++)
     {

        if (scores[i] > highestScore)
         {
            highestScore = scores[i];

        }

}    
	return 0;
}


Now I get this.
error C2660: 'highestScore' : function does not take 2 arguments
Of course. Now the declaration and implementation refer to the same highestScore function. The one that takes 3 arguments. However, the line that calls highestScore has not changed and that line uses only two arguments: sList and listSize.

Now you have to edit the printResult.
Was this the change that I should have made?

1
2
3
void printResult(ofstream& outFile, const studentType sList[], int scores[], int listSize)
{
int maxScore = highestScore(sList, listSize);


Because I still get the same error, and listsize on line 3 in this little snippet of code here is underlined.
Last edited on
How many arguments do you have in the call of highestScore on that line 3?
What arguments should you have on that call?
Topic archived. No new replies allowed.