0

Parsing a file and need to add students to a struct vector using an array for student names specific to that course line.

In my course.h file:

struct Course {
    std::string name;
    int enrollment;
    int maxEnrollment;

    std::string* students; ///< array of student names

    Course(std::string courseName, int maxEnrollmentPermitted);

    bool enroll(std::string studentName);

    void print(std::ostream& output);
};

In my course.cpp file:

bool Course::enroll(std::string studentName) {
    this->students = new std::string[studentName];
    if (this->enrollment < this->maxEnrollment) {
        this->enrollment++;
        return true;
    }
    else {
        return false;

In my source file:

void processEnrollmentRequests(istream& enrollmentRequestsFile, vector<Course>& courses) {
    // Read the requests, one at a time, serving each one
    string courseName;
    enrollmentRequestsFile >> courseName;

    while (enrollmentRequestsFile) {
        enrollmentRequestsFile >> ws;
        string studentName;
        getline(enrollmentRequestsFile, studentName);

        int pos = search(courses, courseName);
        if (pos >= 0) {
            // Found a matching course
            bool enrolled = courses[pos].enroll(studentName);
            if (enrolled) {
                cout << studentName << " has enrolled in " << courseName << endl;
            }
            else {
                // course is full
                cout << studentName << " cannot enroll in " << courseName << endl;
            }
        }
        else {
            // course does not exist
            cout << studentName << " cannot enroll in " << courseName << endl;
        }
        enrollmentRequestsFile >> courseName;
    }
}
        }
    }

I cant seem to add the gathered studentName to the array using this->students = new std::string[studentName]. Getting an error that says must have integral or enumeration type.

15
  • 5
    Offish-topic: may I suggest another std::vector here: std::string* students;-> std::vector<std::string> students; Commented Mar 30, 2017 at 21:34
  • 1
    When you do new std::string[...] the contents of [...] should be a number, the size of the array to allocate. It makes no sense to write new std::string[studentName] Commented Mar 30, 2017 at 21:36
  • 2
    vector<Course> -- You used this, so why didn't you simply use std::vector<std::string> instead of all of the pointer manipulation? Commented Mar 30, 2017 at 21:39
  • We have to use pointers and arrays unfortunately. Otherwise I would concur. Commented Mar 30, 2017 at 21:55
  • 1
    @pstatix -- Are you reading the code you wrote? You are already using vector<Course>. Do you know what vector is? It is a class that represents a dynamic array, thus you're going against your own rules. You need to pointer-up that Course also, just like you're pointering-up that std::string. Commented Mar 30, 2017 at 22:07

2 Answers 2

1

new SomeThing[size] is used to declare array. It makes no sense to use a string as the size.

Assuming the size of students is limited to maxEnrollment, you can use this:

if (this->enrollment < this->maxEnrollment) {
    this->students[this->enrollment++] = studentName;
    return true;
}
else {
    return false;
Sign up to request clarification or add additional context in comments.

1 Comment

Better! But now I am getting a stackdump error. Only goes through the first student.
0

For the sake of completeness, the allocation of students is not the only problem. Given that the code you posted also uses std::vector<Course>, and Course does not follow the rule of 3, using it in a std::vector is highly likely to cause memory corruption, leaks, etc.

Given that you state that students must remain a pointer, the complete fix is to write Course in this manner:

#include <string>
#include <algorithm>

struct Course {
    std::string name;
    int enrollment;
    int maxEnrollment;
    std::string* students; ///< array of student names
    Course(std::string courseName, int maxEnrollmentPermitted);
    bool enroll(std::string studentName);
    void print(std::ostream& output);
    Course(const Course& rhs);
    Course& operator =(const Course& rhs);
    ~Course();
};

Course::Course(const Course& rhs) : name(rhs.name),  
                                    enrollment(rhs.enrollment),  
                                    maxEnrollment(rhs.maxEnrollment),
                                    students(new std::string[rhs.maxEnrollment])
{
   for (int i = 0; i < maxEnrollment; ++i)
      students[i] = rhs.students[i];
}

Course& Course::operator= (const Course& rhs)
{
   Course temp(rhs);
   std::swap(temp.students, students);
   std::swap(temp.maxEnrollment, maxEnrollment);
   std::swap(temp.enrollment, enrollment);
   std::swap(temp.name, name);
   return *this;
}

Course::~Course() { delete [] students; }

Course::Course(std::string courseName, int maxEnrollmentPermitted) : 
               name(courseName), 
               enrollment(0),
               maxEnrollment(maxEnrollmentPermitted), 
               students(new std::string[maxEnrollmentPermitted])    
{}

Why all of this code? Well, in the code you posted in your question, you are using a std::vector<Course>. The Course class as written could not be safely used in a vector, due to Course having incorrect copy semantics. Thus your error you're getting may have a lot to do with code you stated wasn't yours (the vector<Course>).

The adjustments to Course above now makes Course safe to be used in a vector since the copy semantics (copy constructor, assignment operator, and destructor) have now been implemented to handle the dynamically allocated students member.

Note that absolutely none of this code would be necessary if students were simply a std::vector<std::string> instead of std::string *.

For more reading:

What is the rule of 3?

What is the copy / swap idiom?

2 Comments

I am not to alter the main source file. Only the course.cpp. Thanks for the reads though.
Well, you may never get to actually rid yourself of the error until these changes are implemented. You were given a broken class and expect it to work without issues when placed in a vector. The teacher is doing you a disservice by providing such a mess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.