Function Problem

Where I haves grades[i] = score(testAv[i]); its telling me that i cant use score as a function. What can i do to fix this? Thank you guys for your time :D

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
  #include <iostream>
using namespace std;

//User Libraries

//Global Constants - Math/Physics Constants, Conversions,
//                   2-D Array Dimensions

//Function Prototypes

//Execution Begins Here
int main(int argc, char** argv) {
    //set random number seed
    
    //Declare Variables
    string names [5];
    float scores [5][4];
    char grades[5];
    float testAv[5];
    float score;
    
    for(int i=0;i<5;i++){
        cout<<"\nEnter "<<(i+1)<<" student "<<" name:   ";
        cin>>names[i];
        cout<<"Enter his or her 4 test scores:  ";
        for(int j=0;j<4;j++){
            cin>>scores[i][j];
            while(scores[i][j]>100||scores[i][j]<0){
                cout<<"\nTest score should be between 0 & 100"<<endl;
                cout<<"Enter test score:  ";
                cin>>scores[i][j];
            }
        }
    }
    for(int i=0;i<5;i++){
        testAv[i] = 0;
        for(int j=0;j<4;j++)
            testAv[i] += scores[i][j];
        testAv[i] /=4;
        grades[i] = score(testAv[i]);
        
    }
    cout<<"\nStudent Name Average Score Letter Grade"<<endl;
    for(int i=0;i<5;i++){
        cout<<names[i]<<testAv[i]<<grades[i]<<endl;
    }
    if(score >= 90 && score <=100)
            return 'A';
        else if(score >= 80 && score <=90)
            return 'B';
        else if(score >= 70 && score <=80)
            return 'C';
        else if(score >= 60 && score <=70)
            return 'D';
        else
            return 'F';
    
    
    //Initialize Variables
    
    //Process/Map inputs to outputs
    
    //Output data
    
    //Exit stage right!
    return 0;
}
grades[i] = score(testAv[i]);
Explain to us what you're trying to do on this line.
I want the testAv(test average) to be sent to score so it can select a later grade so that it equals to grades.
What is testAv(test average)? Can you explain in only English, and not code?
testAv is the average of the test scores you input.
If this helps this is the problem.


A teacher has five students who have taken four tests. The teacher uses the following grading scale to assign a letter grade to a student, based on the average of his or her four test scores.

Test Score L etter Grade
90–100 A
80–89 B
70–79 C
60–69 D
0–59 F
Write a program that uses an array of string objects to hold the five student names, an array of five characters to hold the five students’ letter grades, and five arrays of four double s to hold each student’s set of test scores.
The program should allow the user to enter each student’s name and his or her four test scores. It should then calculate and display each student’s average test score and a letter grade based on the average.
Last edited on
Oh, that would explain the weird return statements you have.

It looks like what you are trying to do is make "score" be a function.
First, delete the declaration you have of it being a float on line 20.

You currently have this code randomly within your main function:
1
2
3
4
5
6
7
8
9
10
    if(score >= 90 && score <=100)
            return 'A';
        else if(score >= 80 && score <=90)
            return 'B';
        else if(score >= 70 && score <=80)
            return 'C';
        else if(score >= 60 && score <=70)
            return 'D';
        else
            return 'F';

This looks like it should belong in its own function, a function which takes in a float (the score), and returns a char (the letter grade).

To learn how to declare and use a function, I suggest reading this
http://www.cplusplus.com/doc/tutorial/functions/

Let us know if you need help making a proper function.

I'll help you by giving you a skeleton
1
2
3
4
5
char get_letter_grade(float score)
{
    // fill in your logic here
    return 'A';
}


And call the function like this:
grades[i] = get_letter_grade(testAv[i]);

Its telling me that the letter grade is not decalared in this scope as well as the "{" in
char letterG(float score) {
^is an unexpected token



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
#include <iostream>
using namespace std;

//User Libraries

//Global Constants - Math/Physics Constants, Conversions,
//                   2-D Array Dimensions

//Function Prototypes

//Execution Begins Here
int main(int argc, char** argv) {
    //set random number seed
    
    //Declare Variables
    string names [5];
    float scores [5][4];
    char grades[5];
    float testAv[5];
    float score;
    
    for(int i=0;i<5;i++){
        cout<<"\nEnter "<<(i+1)<<" student "<<" name:   ";
        cin>>names[i];
        cout<<"Enter his or her 4 test scores:  ";
        for(int j=0;j<4;j++){
            cin>>scores[i][j];
            while(scores[i][j]>100||scores[i][j]<0){
                cout<<"\nTest score should be between 0 & 100"<<endl;
                cout<<"Enter test score:  ";
                cin>>scores[i][j];
            }
        }
    }
    for(int i=0;i<5;i++){
        testAv[i] = 0;
        for(int j=0;j<4;j++)
            testAv[i] += scores[i][j];
        testAv[i] /=4;
        grades[i] = letterG(testAv[i]);
        
    }
    char letterG(float score){
        if(score >= 90 && score <=100)
            return 'A';
        else if(score >= 80 && score <=90)
            return 'B';
        else if(score >= 70 && score <=80)
            return 'C';
        else if(score >= 60 && score <=70)
            return 'D';
        else
            return 'F';
    }
    
    
    cout<<"\nStudent Name Average Score Letter Grade"<<endl;
    for(int i=0;i<5;i++){
        cout<<names[i]<<testAv[i]<<grades[i]<<endl;
    }
    
    //Initialize Variables
    
    //Process/Map inputs to outputs
    
    //Output data
    
    //Exit stage right!
    return 0;
}
You have to move the declaration of the function outside of the scope of the main function.

like

1
2
3
4
5
6
7
8
9
10
11
char letterG(float score)
{
    /// ....
}

int main()
{
    /// ...

          grades[i] = letterG(testAv[i]);
}
You're awesome. Thank you so much.
Topic archived. No new replies allowed.