Vectors and Populating

I really don't know what to do and I need help with my homework if anybody could help.

The homework says I need to design 2 classes, a class called student that contains names and IDs and the second called course that contains course numbers and a pointer to student objects

These two vectors are declared

1
2
vector<student *> enrolled; 
vector<course *> offered; 


I need to populate enrolled with students names and ID's and populate offered with the students and the courses they are taking

The only thing I have so far is this and I'm not even sure if this is a good way to begin.

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
class Student {
private:
        string name;
        int id;
public:
        string name;
};

class Course {
private:
        string course_name;
        <Student *> student_ptr;
public:
        Course(string name, <Student * > s): course_name(name), student_ptr(s) {}


};

main()
{
        vector<Student *> enrolled;
        vector<Course *> offered;

        ifstream in1, in2;

        in1.open("students");

        in2.open("course");
        if (!in2.is_open()) {
                cout << "Error" << endl;
                return 2;
        }

        in2 >> student_name >> course_name;
        whiel (!in2.eof()) {
                Student * p = find(student_name, enrolled);
                offered.push_back(Course(course_name, p));
                in2 >> student_name >> course_name;
        }

}


If anybody could please help me, I'd be ever so thankful!
Last edited on
I question the use of the vector<Student*> within the Student class, and I also question the use of the vector<Course*> withing the Course class. I normally question the use of pointers within vectors as a general rule, while the pointers are useful in some occasions, such as polymorphic classes, having pointers in a vector are not a good idea, IMO. Usually you use a vector to avoid dynamic memory allocations and the pointers that go with the dynamic allocations.

I recommend that you have a vector<Student> in our Course class, then create a vector<Course> in your program to hold the course information.

So, I was able to get some advice on the spots that need editing but I still have no clue what to put into them. Here is what I have so far and the // represent where I need to add something

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
class Student {
private:
	string name;
        int id;
public:
	//
};

class Course {
private:
	string course_name;
	<Student *> student_ptr;
public:
	Course(string name, <Student * > s): course_name(name), student_ptr(s) {}
	//

};

main()
{
	vector<Student *> enrolled;
	vector<Course *> offered;

	ifstream in1, in2;

	in1.open("students");
	//

	in2.open("courses");
	if (!in2.is_open()) {
		cout << "Error" << endl;
		return 2;
	}

	in2 >> student_name >> course_name;
	while (!in2.eof()) {
		Student * p = find(student_name, enrolled);
		offered.push_back(Course(course_name, p));
		in2 >> student_name >> course_name;
	}
	//
}
Last edited on
Why the pointers in your vectors?

Where are you allocating memory for those pointers?

Where are you deleting the memory you need to allocate for those pointers?

You really don't seem to need those pointers so I recommend you just use "normal" instances instead of the pointers.

Also main() must be defined to return an int in a C++ program:

1
2
3
4
5
int main()
{

    return(0);
}


Last edited on
Topic archived. No new replies allowed.