The way that I have to submit files for schoolwork is all in one file. I cannot have separate header files or separate source files. The teacher did not really explain how to properly do this so could someone tell me what I am doing wrong and how to get the below code working? I still need to add a few features, but I figured there was no point in going any further until I can figure out how I am supposed to put all of this on one cpp file for submission.
Your #endif at line 62 should be at line 28. Line 28 is the end of your class declaration. Lines 8-28 would go in a Course.h file, if you were allowed to have separate files.
Lines 30-60 would go in your Course.cpp file, if you were allowed.
Your program is going to experience an out of bounds condition at line 79. You're trying to add 13 students to course1, but course1 has a capacity of 10. Course::add_student has no check to ensure that you don't add too many students. I strongly recommend using a std::vector instead of allocating your own array of students.
As part of the assignment we are supposed to increase the size of the array if the number of students assigned is greater than the capacity. So that is part of what I need to do.
Is there a way to increase the size of the array or would I need to create a new array to fit with the size of the students that are added?
I have gotten as far as giving an error message and stopping the program if the number of elements in addStudent exceeds the capacity. However, I can't seem to nail down anything to add 13 students to a class when the capacity is 10.
In addStudent:
- Allocate a new students array (don't overwrite the students pointer yet) with the new desired capacity.
- Copy the the old students array to the new students array.
- Delete the old students array.
- Set the students pointer to the new array.
- Update capacity to the new capacity.
- Continue with addStudent.
void Course::addStudent(const string& name) {
string* studentsNew;
students[numberOfStudents] = name;
numberOfStudents++;
studentsNew = new string[numberOfStudents];
for(int i = 0; i < numberOfStudents; i++){
students[i] = studentsNew[i];
}
delete[] students;
students = new string[number of students];
for(int i = 0; i < numberOfStudents; i++){
studentsNew[i] = students[i];
}
delete[] studentsNew;
}
This does not seem to work, but am I on the right track? I think what I am having trouble with is that the count is increased and a name is added each time the function is called.
That seemed to work as I needed! Thanks for the help! This forum is great for when I get stuck on one part of a small program. So far I am enjoying learning about cpp