I am a teacher at an independent boarding school and am trying to to write a program in C++ that will randomly seat students at tables in our dining hall so that they sit with different students and with different staff members each week. Ideally, over a given period, they would not sit at the same table twice and with as many different students as possible. I have created this program in Python and it works great (well, pretty good). For a variety of reasons, I am trying to port this over to C++ (which I do not know well at all) so I can hand it off to the boarding staff. Both student and staff (as well as table capacities) are read from text files. I have created two custom classes, one for students and one for tables, to handle the data. Here are the two class header files:
Table.h
#pragma once
#include <iostream>
#include "Student.h"
#include <vector>
using namespace std;
class Table
{
// Private class variables
string staff;
int numSeats;
vector<Student> seating;
public:
Table(); // Default constructor
Table(string s, int n);
Table(const Table& that) : staff(that.staff), numSeats(that.numSeats)
{
}
// Copy Constructor
Table& operator=(const Table& that)
{
staff = that.staff;
numSeats = that.numSeats;
return *this;
}
int getNumSeats();
string getStaffName();
void addStudent(Student student);
void removeStudent(Student student);
void clearStudents();
vector<Student> getTableSeating();
int getRemainingSeats();
~Table(void);
};
Here is the student class file:
#pragma once
#include <iostream>
#include <vector>
using namespace std;
class Student
{
string name;
string country;
vector<int> tablesSatAt;
public:
Student(string n, string c);
Student();
Student(const Student& that) : name(that.name), country(that.country)
{
}
Student& operator=(const Student& that)
{
name = that.name;
country = that.country;
return *this;
}
string getName();
string getCountry();
void addTable(int tableNumber);
void removeTable(int tableNumber);
bool satAtTable(int tableNumber);
friend bool operator==(Student s1, Student s2);
friend bool operator!=(Student s1, Student s2);
~Student(void);
};
bool operator==(Student s1, Student s2);
bool operator!=(Student s1, Student s2);
Here is the recursion function that does the heavy lifting:
bool seatRecursive(vector<Student> &tempStudents, vector<Table> &tempTables)
{
if (tempStudents.size() == 0) return true; //base case
Student nextStudent = randomSelect(tempStudents);
for (vector<int>::size_type i=0; i<tempTables.size(); i++)
{
if (tempTables[i].getRemainingSeats() > 0 && !nextStudent.satAtTable(i))
{
addStudentToTable(nextStudent, tempTables, i);
if (seatRecursive(tempStudents, tempTables)) return true;
else
{
removeStudentFromTable(nextStudent, tempTables, i);
tempStudents.push_back(nextStudent);
}
}
}
return false;
}
Most of this works. When I run the program, I get a text file with 10 weeks of seating, but all the table seatings are the same. i.e. if I am a particular staff member, I have the same kids sitting at my table for all 10 weeks. I have a vector of ints that is supposed to be storing the table numbers that a student has sat at over time. When debugging, I notice that those table numbers are NOT being stored in that vector, it is always empty. My problem is that I can't figure out why this is happening. Is it because I am passing the vectors by reference? Does it have something to do with pointers, even though I am not explicitly declaring pointers?
Any suggestions are greatly appreciated and I can paste in the rest of the code if necessary.
Brian
Why make it difficult?
Just put all your students in a vector, then use the STL random_shuffle algorithm, and finally just put the resulting vector of students linearly at all your available tables.
You don't even need custom classes for students or tables really
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With