1. ## Vectors:

I am trying to make a grade book and using a vector to get the grades. I am getting errors all over and I figured this would happen because this is the first time I ever used vectors.

Code:
```

#include <iostream>
#include <vector>

using namespace std;

public:
{
int checker;

cout<< " Enter the students Grades and Enter -1 to stop. "<<endl;

while (checker != -1) {
cin>>checker;

for (int i =0; i<numGrades; ++i) {

}
}

}

{
double total;
double average;

for (int i=0; i<num; ++i) {
}

average = total / num;

return average;
}
{

}

private:

};

#endif```

2. Code:
`                averageGrades(grades[i],numGrades);`
averageGrades() expects a pointer to int, you're passing an int

Better would be to pass a const reference to vector<int> to averageGrades(). This way you wouldn't have to pass the size.
btw: move the call to averageGrades() out of the input loop.

Kurt

3. How many students?
How many grades per student?

Entityâ€“relationship model - Wikipedia, the free encyclopedia
Before you even reach for the keyboard, you need to grab a pencil and some paper, READ your assignment carefully through several times and start to sketch out some details.

Such as
A class is composed of 0 or more students.
A student has 1 or more grades.
A department has ....
A professor has ....

Only then can you start to assign meaningful class names (in code), along with the appropriate methods and data.

4. I was calling the function wrong. I decided to change a few things too. Still have a error in main though and the program is not done I was just testing it to make use average is sent to letterGrade(). The error is on line 61 and says "too few arguments to call function.

Code:
``` #include <iostream>
#include <vector>

using namespace std;

public:
{
int checker;
double total=0.0;

cout<< "Enter the students Grades and enter -1 to stop entering. "<<endl;

while (checker != -1) {
cin>>checker;          //stops program if -1 is entered
++numGrades;        // counts the amount of entries for the vector

for (int i =0; i<numGrades; ++i) {

}
}
}

double averageGrades(double total , int num)
{

double average=0.0;

average = total / num;

return average;
}

{
cout<<average<<endl;

}

private:

};

int main(int argc, const char * argv[])
{

return 0;
}```

5. This is why i choose to use a vector but didn't know how to do it. So I just write sizeof() in the next function and it will be good to go?

6. Salem, your right and yes I do need to work on that. However this time I knew there was no way for me to tell any of the above. So i decided to let the user tell the program.

7. Code:
``` #include <iostream>
#include <vector>

using namespace std;

public:
{

double total=0.0;
int amount;

cout<<"Enter the amount of grades you need to enter"<<endl;
cin>>amount;
cout<< "Enter the students Grades: "<<endl;

for (int i =0; i < amount; ++i) {

}

}

double averageGrades(double total , int num)
{

double average = 0.0;

average = total / num;

return average;
}

{
int number=0;

cout<<averageGrades(grade, number)<<endl; // test to see if values are being passed
}

private:

};

int main(int argc, const char * argv[])
{

return 0;
}```
I changed it up a little. I am just testing at this point to see is my values are being passed correctly and they are not. What am I doing wrong?

8. > What am I doing wrong?
You're writing code by trial and error rather than sitting down with pen and paper to come up with some kind of plan.

Big issues you have at the moment.
1. Your class has no member data, so there is no information being passed from say.
to

2. You're returning results, but ignoring those results.

Stop hacking code by guessing and come up with a plan. At this stage, any plan is better than what you're doing at the moment.

9. On an unrelated note:
Code:
```#ifndef ___2B__Homework__LetterGrade__
C++ forbids identifiers that contain double underscore in a row; those are reserved fro the implementation. Also, FYI, identifiers that start with an underscore followed by a capital letter are similarly forbidden.

10. on line 21 you are creating vector INSIDE the loop.

So you create vector for each iteration and then initialize only one item of the vector.

If you need only one int - use one int and not vector.

11. Thank you Vart. I way so focused on using vectors that I missed the simple solution. All the functions are working but the letterGrade(). I am still testing it right now but the values wont print. This function will eventually convert the average to a letter grade. Another thing about my classes. Is it ok if I do not put any variables in private, since I am not returning a values in main() to the functions?

Code:
``` #include <iostream>
#include <vector>

using namespace std;

public:
{

double total=0.0;
int amount = 0;

cout<<"Enter the students grades and -1 to stop entering"<<endl;

while (grades != -1) {

++amount;

}
}
double averageGrades(double total , int num)
{

double average = 0.0;

average = total / num;
return average;
}

{
int number=0;
double average;

cout<<average<<endl; // test to see if values are being passed
}

private:

};

int main(int argc, const char * argv[])
{

return 0;
}```

12. when you get to line 18 for the first time grades is not initialized - so behavior is undefined.

You need to increase warning level and pay attention to the compiler warnings

Your letterGrade function calls avareageGrades with amount 0 which results in division by 0...

You need somehow store the total and amount calculated by GetGrades in the member variable...

13. Originally Posted by jocdrew21
Is it ok if I do not put any variables in private, since I am not returning a values in main() to the functions?
You can do that, but that's missing the point of classes. A class is an object and an object encapsulates state.
If it does not encapsulate state, then it isn't distinguishable from global functions, so what is the point?
Think of it as a book: the book contains writing. The writing is a state in the book. So your GradeBook should contain grades as state.
It should not ask for such things because a) it reduces flexibility in using the object and b) it isn't natural (have you met a book that speaks?).
Instead, the normal approach is that you use setters and getters. The main program asks for a grade and then puts it in the grade book.
And again, your problem is basically that you remember no state. First you get the grades, then you forget them, then you try to average them and print them. How does that work? You need variables to maintain state such as grades.

Popular pages Recent additions