C++ newbie here and this is my first post. I am working on a project at school and I'm somewhat stuck. My task is to create a roster of courses. Each roster will contain a course name, course code, course credits, and professor name. No problem, I have a Roster class. The problem is I'm unsure how to make an array of objects dynamic, as it must be able to grow and shrink when requested by the user. I am somewhat familiar with dynamic arrays in general, but unsure of the syntax of dynamic arrays of objects. And, as per the professor's instructions, vectors are not an option. I've searched this forum as well as other areas on the internet and haven't found an answer, or I'm not understanding the answers I've found, and thought I'd post here. The following is my code for a non-dynamic array of objects. Help in converting to a dynamic array would be much appreciated. Thanks!
StudentEnrollment.h:
#ifndef STUDENTENROLLMENT_H
#define STUDENTENROLLMENT_H
# include <iostream>
# include <string>
using namespace std;
class Roster {
private:
string courseName;
string courseCode;
string courseCredits;
string professorName;
public:
void setCourseName ( string );
void setCourseCode ( string );
void setCourseCredits ( string );
void setProfessorName ( string );
string getCourseName();
string getCourseCode();
string getCourseCredits();
string getProfessorName();
Roster ();
};
#endif;
StudentEnrollment.cpp:
#include <iostream>
#include <string>
#include "StudentEnrollment.h"
using namespace std;
// Roster class implementation
Roster::Roster () {
courseName = "";
courseCode = "";
courseCredits = "";
professorName = "";
}
void Roster::setCourseName ( string cn ) {
courseName = cn;
}
void Roster::setCourseCode ( string c ) {
courseCode = c;
}
void Roster::setCourseCredits ( string cc ) {
courseCredits = cc;
}
void Roster::setProfessorName ( string pn ) {
professorName = pn;
}
string Roster::getCourseName() {
return courseName;
}
string Roster::getCourseCode() {
return courseCode;
}
string Roster::getCourseCredits() {
return courseCredits;
}
string Roster::getProfessorName() {
return professorName;
}
main.cpp:
#include <iostream>
#include <string>
#include "StudentEnrollment.h"
using namespace std;
int main (int argc, char * const argv[]) {
int number_of_rosters = 0;
string course, code, credits, name;
cout << "Enter the number of rosters you would like to create: ";
cin >> number_of_rosters;
cin.ignore(100, '\n');
Roster roster[number_of_rosters];
for ( int i = 0; i < number_of_rosters; i++){
cout << "Enter course name: ";
getline(cin,course);
roster[i].setCourseName(course);
cout << "Enter course code; ";
getline(cin, code);
roster[i].setCourseCode(code);
cout << "Enter course credits: ";
getline(cin, credits);
roster[i].setCourseCredits(credits);
cout << "Enter professor name: ";
getline(cin, name);
roster[i].setProfessorName(name);
cout << "Next course..." << endl;
}
cout << endl;
for ( int i = 0; i < number_of_rosters; i++){
cout << roster[i].getCourseName() << endl;
cout << roster[i].getCourseCode() << endl;
cout << roster[i].getCourseCredits() << endl;
cout << roster[i].getProfessorName() << endl;
cout << endl;
}
return 0;
}
Forgive me if this didn't format correctly. This is my first post.
Arthur