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
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();
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 ()".
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
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
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.
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.