-
Vectors of class
im having a hard time trying to get this vector to work in the class. This is my second class of taking computer science and im really confuse can someone please help
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Course {
private:
int scores;
public:
Course();
Course(int s);
int minimum();
/* void print() {cout << scores; }
void countA();
void read();
int maximum();
int standard_deviation();
int average(); */
};
Course::Course()
{
scores = 0;
}
Course:: Course(int s)
{
s=scores;
}
int Course::minimum()
{
int minimum= numbers[0];
for (int i=0; i<numbers.size(); i++)
{
if (numbers[i] <minimum)
{
minimum = numbers[i];
}
}
return minimum;
}
int main()
{
Course cs202;
vector<int> numbers;
int grade;
cout<<"Enter set of scores for students until eof\n";
cin >> grade;
while (!cin.eof()) {
numbers.push_back(grade);
cin>>grade;
}
/*cs202.read();*/
cout << "Minimum = " << cs202.minimum() << endl;
/*cout << "Average = " << cs202.average() << endl;
cout << "Standard Deviation = " << cs202.standard_deviation() << endl;
cout << "Maximum = " << cs202.maximum() << endl;
cout << "A's Count = " << cs202.countA() << endl;
cout << "The scores again: \n";
cs202.print();*/
}
-
Re: Vectors of class
First, using an external global object within a class (like your vector<int> numbers within Course ) sounds like a bad design.
Second, what problem do you have with the code you have posted?
-
Re: Vectors of class
When posting code, please format your code before posting and use code tags (Go Advanced, select code and click '#').
Code:
int Course::minimum()
{
int minimum = numbers[0];
numbers is a vector that is local to the function main. Therefore it can't be accessed from class Course. If you want class Course to be able to use numbers, then numbers will need to be made global. However, a better way would be to have vector numbers as a private member of class Course and have additional Course methods to add a score to the vector etc.