YM_coding
YM_coding

Reputation: 95

Vector size returns 0 after being populated in a for loop

I have been working a small c++ app that has to do with lecturers and students getting matched together based on the class they teach/take.

Each Lecturer has a vector of students. When a student takes the same class as a Lecturer teaches, then we add that same student to that same lecturer.

I have 2 for loops that loop through all lecturers and students and then compares the classes of both and if they match, add that student to the lecturer.

After the loops, I am looping through all the lecturers and getting the size of each. But returns back 0 and it should give back 1. (Because one student matches the class for each lecturer).

Lecturer.h

#pragma once
#include <iostream>
#include "Person.h"
#include "Student.h"
#include <vector>
#include <string>

using namespace std;

class Lecturer : public Person {

public:
    // Lecturer(string department, string specialization, string name, int age, char gender)
    //     : department(department), specialization(specialization), name(name), age(age), gender(gender) {}

    Lecturer() { }

    Lecturer(string department, string specialization, string name, int age, char gender, string uniClass){
        this->department = department;
        this->specialization =specialization;
        this->name = name;
        this->age = age;
        this->gender = gender;
        this->uniClass = uniClass;
    }

    // Class Methods
    void addStudent(Student student);

    // Setter Methods
    void setDepartment(string dprt);
    void setSpecialization(string splz);
    void setName(string nme);
    void setAge(int ag);
    void setGender(char g);

    // Getter Methods
    string getDepartment();
    string getSpecialization();
    string getUniClass();

    int getStudentsSize();
    vector<Student> getStudents();

private:
    string department;
    string specialization;
    vector<Student> students;
    string uniClass;
};

void Lecturer::addStudent(Student student)
{
    cout << student.getName() << endl;
    students.push_back(student);
}

int Lecturer::getStudentsSize()
{
    return students.size();
}

Student.h

#pragma once
#include <iostream>
#include "Person.h"
#include <string>

using namespace std;

class Student : public Person {

public:
    // Student(string major, string minor, int id, string name, int age, char gender)
    //     : major(major), minor(minor), id(id), name(name), age(age), gender(gender) {}

    Student() { }

    Student(string major, string minor, int id, string name, int age, char gender, string uniClass){
        this->major = major;
        this->minor = minor;
        this->id = id;
        this->name = name;
        this->age = age;
        this->gender = gender;
        this->uniClass = uniClass;
    }
    // Setter Methods
    void setMajor(string mjr);
    void setMinor(string mnr);
    void setId(int _id);
    void setName(string nme);
    void setAge(int ag);
    void setGender(char g);

    // Getter Methods
    string getMajor();
    string getMinor();
    int getId();
    string getUniClass();
    string getName();

private:
    string major;
    string minor;
    int id;
    string uniClass;
};

string Student::getUniClass()
{
    return uniClass;
}

main.cpp

#include <iostream>
#include <string>
#include "Person.h"
#include "Lecturer.h"
#include "Student.h"

int main()
{

    vector<Lecturer> lecturers;
    lecturers.push_back(Lecturer("Computing", "Advanced Programming", "John", 40, 'm', "AB101"));
    lecturers.push_back(Lecturer("Business", "Finance", "Dave", 42, 'm', "AB102"));
    lecturers.push_back(Lecturer("Science", "Physics", "Bill", 46, 'm', "AB103"));

    vector<Student> students;
    students.push_back(Student("Computer Science", "Maths", 123, "Mike", 20, 'm', "AB101"));
    students.push_back(Student("Business", "Economics", 142, "Jane", 21, 'f', "AB102"));
    students.push_back(Student("Engineering", "Physics", 151, "Mary", 19, 'f', "AB103"));

    for(Lecturer lecturer : lecturers)
    {
        for(Student student : students)
        {
        //cout << "Name: " << student.getUniClass() << endl;
        if (lecturer.getUniClass().compare(student.getUniClass()) == 0)
            {
                // ADDING A STUDENT THAT MATCHES THE CLASS
                lecturer.addStudent(student);
            }
        }
    }

    for(Lecturer lecturer : lecturers)
    {
        // EACH LECTURER'S STUDENTS SIZE IS 0 HERE (SHOULD BE 1)
        cout << lecturer.getStudentsSize() << endl;
    }
}

Upvotes: 1

Views: 232

Answers (2)

einpoklum
einpoklum

Reputation: 132346

Other notes not involving what eventually was your actual bug:

Each Lecturer has a vector of students.

Why? Each lecturer should have a(n unordered) set of students. Students don't exist multiple times in parallel. Also, they don't have an inherent-and-relevant ordering. Actually, lecturers need such a set for each of the courses they teach.

Also, why are you assuming each student takes exactly one class? (looks at source) Ah! Now I get it.

  • Your "students" are not students, they're really student-course-taking records.
  • Your "lecturers" are not lecturers, they're really course records with lecturer information.

Extremely confusing. Please fix that. If you use the appropriate terms/names, and appropriate data structures, it's not unlikely you'll figure out your bug on your own.

I have 2 for loops that loop through all lecturers and students and then compares the classes of both and if they match, add that student to the lecturer.

Two loops? Too deep to be writing them yourself in this case - it's like you're re-inventing the wheel! Replace the inner loop with an invocation of std::copy_if. That might also help locate bugs.

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385405

You're using values everywhere. That means copies.

Your first change is to iterate using references. For example:

for (Lecturer& lecturer : lecturers)
//           ^

Upvotes: 4

Related Questions