Expected initializer before '.' token

I'm writing a class with an array and I'm supposed to calculate the average of some entered grades outside the class and in the main but I get the error
Expected Initializer before '.' 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
 #include <iostream>

using std::cout;
using std::cin;
using std::endl;

class Student{
    private:
    float grades[10];

    public:
    void setGrades()
    {
        float grades[10];
        int i;
        for(i = 0; i < 10; i++)
        {
            cout << "Enter Grade # " << i + 1 << endl;
            cin >> grades[i];
            cout << endl;
        }
    }

    float getGrade ()
    {
        int i;

        cout << "Grades" << endl;;

        for(i = 0; i < 10; i++)
        {
            cout << "  " << grades[i] << endl;
        }
    }

};

int main () {
    Student gradeAverage;
    float grades = 0.0;

    gradeAverage.setGrades();

    float gradeAverage.getGrade();
    float computeAverage = 0;

    for(int i = 0; i < 5; i++)
    {
        computeAverage = computeAverage + gradeAverage.getGrade();
    }

    cout << computeAverage/5 << endl;

    return 0;
}


I'm not to sure what to do or i even did the class right
Please help!
A compiler says more than just: "error". It also mentions line of code.
For example:
 In member function 'float Student::getGrade()':
34:5: warning: no return statement in function returning non-void [-Wreturn-type]

 In function 'int main()':
44:23: error: expected initializer before '.' token
40:11: warning: unused variable 'grades' [-Wunused-variable]

Two warnings.

What if I promised you $1000, but then just walk away, leaving you empty handed?
You make a promise on line 24, but you don't keep it.


The error is on line 44. Please explain this code:
float gradeAverage.getGrade();
Yeah sorry i forgot to put where i had the error.
I understand what you mean by that analogy but I don't understand what's a fix for it in the code.
And this was a mistake i didn't mean to type that
float gradeAverage.getGrade();
If the problem is
no return statement
, what do you think the solution might be?
Hello Brandon17,

Line 44 looks like a nice prototype, but what is it doing in the middle of the program?

In the function "getGrade()" Why are you printing the array when you should be returning something from the array. In this case the "getGrade()" function should have a parameter the represents the element of the array that you need.

As you have it "float getGrade ()" would be better named "void printGrades ()".

Hope that helps,

Andy
I know that i shouldn't be printing the there but the question was my teacher gave me was this
Write a class called Student that contains a property called grades that can store a
maximum of 10 grades. Create a setter and a getter method. The setter method
will take no parameters and return no parameters. Instead, within the setter
method you must construct a loop that will ask the user to enter 10 grades. The
getter method will simply print the 10 grades; so, it will take no and return no
parameters. Yes, that is a misnomer; this is because passing and returning arrays
has not been covered yet. Create another method called computeAverage that
return the average of all the grades. Create another method called
minimumGrade that returns the minimum grade the student received.

He knows it's a misnomer but he wants us to do it anyway.
He hasn't covered it yet so I was just looking it up but nothing I find helps
Hello Brandon17,

Now that I see what is required I will take a look tomorrow and see what I can do.

Andy
The following cannot be compiled.
float gradeAverage.getGrade();

To resolve compilation error, change to
grades=gradeAverage.getGrade();
or simply remove "float" as follows
gradeAverage.getGrade();

Last edited on
you have some issues to resolve here,

i think you should rename setGrades() and getGrades() to inputGrades() and outputGrades() so the names match the functionality better, you are calling getGrade() expecting to get a grade but thats not what the function does, and it does not return a grade at all, its name has misled you. If your tutor did not tell you what to call the functions then rename them to be more meaningful and because it does not return a value it should be "void".

you have not added a function computeAverage() that your tutor asked for. Let it calculate the average from the grades array and return it. because it will return a float value, it should be declared as float computeAverage().

1
2
3
4
    Student student;
    student.setGrades();  // assign the grades
    student.getGrades();  // list the grades
    cout << student.computeAverage() << endl;  // output the average 


Last edited on
Hello Brandon17,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Write a class called Student that contains a property called grades that can store a maximum of 10 grades.

Create a setter and a getter method.

The setter method will take no parameters and return no parameters. Instead, within the setter method you must construct
a loop that will ask the user to enter 10 grades.

The getter method will simply print the 10 grades; so, it will take no and return no parameters.

Yes, that is a misnomer; this is because passing and returning arrays has not been covered yet.

Create another method called computeAverage that return the average of all the grades.

Create another method called minimumGrade that returns the minimum grade the student received.

He knows it's a misnomer but he wants us to do it anyway. 

In lines 5 and 8 it says will take no parameters and return no parameters yet the get function is trying to return a float, but it should return nothing.

Now you need to create two functions "computeAverage" and " minimumGrade" and both do return a value.

Give it a try and see what you can come up with. We can always fix what is wrong.

Hope that helps,

Andy
Hello Brandon17,

I reworked the "set" function to speed up testing. I looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void setGrades()  // <--- Should be renamed.
{
	double gradesArr[MAXSIZE]{ 85, 89, 77, 100, 95, 91, 87, 97, 93, 87 };  // <--- Added for testing. Comment out when finished.
	int i;

	for (i = 0; i < MAXSIZE; i++)
	{
		//cout << "Enter Grade # " << i + 1 << ": ";  // <--- Changed. Removed the "endl". Puts input on same line as prompt.
		//cin >> grades[i];
		grades[i] = gradesArr[i];  // <--- Added for testing.
		//cout << endl;  // <--- Commented for testing.
	}

	std::cout << std::endl;  // <--- Used for testing.
}

This way the array in the class is loaded with the same numbers each time the program is run. It helps in seeing how other functions work because you are using the same numbers each time and you do not have to enter ten numbers each time the program runs. Once working you can change back to getting the user input.

Just a little trick to cut down on testing time.

Hope that helps,

Andy
Topic archived. No new replies allowed.