Homework Help

I keep getting an error on line 64, "letterGrade not declared in this scope". What the french toast is going on? I declared it as a struct.
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
// ******************************************************************
//
//  Grades.cpp
//
//  This program computes student grades. For each student, two
//  quiz grades (graded on a 10 point basis), one midterm exam grade
//  and one final exam grade (each on a 100 point basis) are read
//  in. The final numeric grade is computed weighing the final
//  exam 50%, the midterm 25%, and the quizzes 25%. The numeric
//  grade and corresponding letter grade are output.
//
// ******************************************************************

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

struct StudentRecord {
double quiz1;
double quiz2;
double midtermExam;
double finalExam;
double courseAverage;
char letterGrade;
};


void outputRecord (StudentRecord record)
{
    cout << endl;
    cout << "Quiz Scores: " << record.quiz1 << "  " << record.quiz2 << endl;
    cout << "Midterm Exam Score: " << record.midtermExam << endl;
    cout << "Final Exam Score: " << record.finalExam << endl;
    cout << endl;
    cout << "Course Average: " << record.courseAverage << endl;
    cout << "Final Letter Grade: " << record.letterGrade << endl;
    cout << endl;
}


void computeAverage (StudentRecord& record)
{
    const double EXAM_WT = 0.5;
    const double MIDTERM_WT = 0.25;
    const double QUIZ_WT = 0.25;
    double quiz1Percent, quiz2Percent;

    //
    // Convert the 10 point quizzes to a percent, then find the average
    //
    quiz1Percent = 100 * record.quiz1 / 10.0;
    quiz2Percent = 100 * record.quiz2 / 10.0;
    double quizAvg = (quiz1Percent + quiz2Percent) / 2;

    //
    // Compute the weighted average to get the numeric course grade
    //
    record.courseAverage = quizAvg * QUIZ_WT +
        record.midtermExam * MIDTERM_WT + record.finalExam * EXAM_WT;

    //
    // Call the letterGrade function to find the corresponding letter grade
    record.letterGrade = letterGrade(record.courseAverage);

}



char letterGrade (double numericGrade)
{
    char letter;

    if (numericGrade < 60)
        letter = 'F';
    else if (numericGrade < 70)
        letter = 'D';
    else if (numericGrade < 80)
        letter = 'C';
    else if (numericGrade < 90)
        letter = 'B';
    else
        letter = 'A';

    return letter;
}

int main()
{
    return 0;
}
Last edited on
The problem is that you are calling the letterGrade() function before it is defined.

Basically, the compiler is going through your code line by line and analyzing it all. Whenever it gets to the call for letterGrade() it checks it's "search history" and says "Hold the phone! I don't remember seeing this function anywhere! Error!"

You need to add what is called a function prototype to tell the compiler that the definition is coming later.
At the top of your code below all of the 'using' statements add this prototype.

char letterGrade(double);
Ah yeah derp. What's odd is all I did was add the struct, otherwise this is the teachers template, odd to think he would give it to us needing to correct it :P.
You should probably place the letterGrade prototype just above computeAverage function definition OR move the definition as a whole above the computeAverage.

What the french toast is going on?

The compiler as at the definition of computeAverage couldn't find letterGrade function because it hasn't been "declared or defined".
Mistakes happen. :)
since you are not using prototype ( declare function before uses it )
defining functions without declaring it , it should be always at the top who will use it
i mean...
example:

1
2
3
void func1() {
// calls func2();
}


in this snippet above, it says in the body that func1 calls func2 therefore, func2 should be define above the func1.
so the code should be like this
1
2
3
4
5
6
void func2() {
//code
}
void func1() {            //correct code!
func2();
}


1
2
3
4
5
6
void func1() {
func2();
}
void func2() {     //very bad code!
//code
}

if you define it under the func1 , it will surely give you an error that the func2 hasnt declared yet. ( it means you havent yet declared it or defined it above who will use it )
Last edited on
Consider changing computeAverage() and outputRecord() to be methods of struct Record. If you don't, then do change outputRecord() to take a reference to Record. The current code makes a copy of the record each time you call outputRecord()
Topic archived. No new replies allowed.