Okay so I have a class Student, which takes a number and a vector as a parameter for the constructor. Everything works well, until I output the values of the vector for every instance. The problem is that the same vector is being shared with EVERY instance I create, but I want it to be unique for every single one!

//Student.h
#ifndef __Grade_calculator__Student__
#define __Grade_calculator__Student__

#include <iostream>
#include <vector>
using namespace std;

class Student {
private:
    string name;
    int score;
    int class_count;
    int test_count;
    vector <Student> students;
    vector <int> Scores;
public:
    Student();
    Student(string n,vector<int> &s);
    ~Student();

    int getScore(){return score;}
    void display_specific_transcript(int student);
    void display_all_transcript();
    void set_students();
    void transcript();
    void calculator();


};

#endif /* defined(__Grade_calculator__Student__) */

//Student.cpp
#include "Student.h"
#include <vector>
Student::Student(){ //initialize all vairables
    //cout << "student created!\n";
    name = "";
    for(int i = 0; i< Scores.size();i++)
    {
        Scores[i] = 0;
    }

}

Student::Student(string n, vector<int> &s) {
    //set variables equal to parameters
    //cout << "Student created with different constructor\n";
    name = n;
    /*for(int i = 0; i< s.size();i++)
    {
        //Scores[i] = s[i];
        cout << s[i] << "from cin\n";
        //cout << Scores[i] << "from new vector\n";
    }*/
    Scores = s;
    cout << s.size();
}

Student::~Student() {
    //cout << "Student destroyed";
}

void Student::calculator()//MAIN PROGRAM
{
    this->set_students();//Enter students information
    this->display_all_transcript();//Displays everyones transcript
}

void Student::set_students()
{
    cout << "Enter the number of students in the class ";
    cin >> class_count;
    cout << "Enter the number of tests given during this course ";
    cin >> test_count;

    for (int i = 0; i < class_count; ++i)//goes through students in class
    {

        cout << "\nEnter student " << i+1 << " name: ";
        cin >> name;

        for(int j = 0; j < test_count; j++)
        {
            cout << "For test " << j+1;
            cout << ", Enter student " << j+1 << " score: ";
            cin >> score;
            Scores.push_back(score);
        }



        Student s(name,Scores);
        students.push_back(s);
    }
}

void Student::display_all_transcript()
{
    for (vector <Student>::iterator iter = students.begin(); iter != students.end(); iter++)
    {
    if(iter->name != "") iter->transcript();
    else cout << "Error! Student doesn't exist\n";
    }
}

void Student::transcript()
{
    cout << "\n******"<< name << "'s Transcript******\n";
    int i = 1;
    for (vector <int>::iterator iter = Scores.begin(); iter != Scores.end(); iter++)
    {
        cout << "Test score "<< i << ": " << *iter << endl;
        i++;
    }//HERE IS THE PROBLEM!!!
/*
If you test the input, you will see that for the first student everything is okay,
 and the vector displays only its scores, but if there is more than one,
 every other student will have their scores ADDED to the same vector!
*/

}

//Main.cpp
#include <iostream>
#include "Student.h"

int main()
{
    Student student;
    student.calculator();

    return 0;
}

Why is the vector of Student classes contained within the Student class? The vector should be declared in main(), not in the class itself

Then I'd take set_students() out of the class and make it a simple global function.

void set_students(vector<Student>& students);

int main()
{
    vector<Student> students;
    set_students(students);
}

what line 12 Score used for? Isn't the vector of scores the same thing?

Member Avatar for iamthwee

Don't you just instantiate ANOTHER student object

Eg Student a, Student b?

[edit please ignore]

Member Avatar for iamthwee

And to echo what AD said you shouldn't have a vector of student objects within the student class. It makes no sense.

To me it looks like your problem is trying to do too much inside one class. For something like this you'll probably find it much easier to use stronger organization of your classes. for example a StudentClass class that contains a vector of Student class which contains a vector of TestScore class. This way all your data is strongly linked, so that the vector in StudentClass will only have information for the Students in that Class, and each Student will only have the TestScores for that Student. Here's a prototype example which, while not complete, will give you an idea on how this can work:

class Student 
{
private:
    class TestScore
    {
    public:
        string test_name;
        int test_score;
        TestScore();
        TestScore(string _name, int _score);
    };
    string name;
    vector <TestScore> Scores;

public:

    Student();
    Student(string n,vector<int> &s);
    ~Student();
    void transcript();
};
class StudentClass
{
    int score;
    int class_count;
    int test_count;
    vector <Student> students;
    void display_all_transcript();
    void set_students();
    void calculator();
    void display_specific_transcript(int student);
}

The actual code causing the problem is in line 84 - 90 of your code. Note that the solutions suggested so far would fix this because they would force you to fix this code.

In these lines you get the students scores and store then in the vector of the student class declared in main. Since you never clear this vector the second time around (i.e. for the second student) you are adding more marks to the same vector before storing it in a new student class instance. Ultimately the vector used to store scores in this loop can be local to the code and does not need to be stored in any class instance, it is transfered to a class instance at line 94.

@ Ancient Dragon
I would suggest that the students vector be a static member of the Student class, although your suggestion is also valid. However, having it a static member of the Student class means that other classes and functions can access it, whereas if it is declared in main() it cannot be. I don't like global variables, unless absolutely there is no other way to make some data "globally" accessible, so I am not suggesting that. :-)

I actually like tinstaafl's suggestion better than mine.

, whereas if it is declared in main() it cannot be.

Of course it can -- just pass the vector as a parameter to other functions. You'd have to do that no matter how it's declared.

@AD & tinstaafl
:-) Goes to show - any three good programmers will generally come up with 3 valid approaches to any programming problem! :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.