dynamic pointer array

I am working on a project for class that I am having trouble with. The program creates a class object and sets the class size to 10. However, 13 students are added to the class. I am supposed to change the size of the array to accept 13 students instead of 10. No idea where to start. I have been sitting here for hours trying different things. I was able to get it to generate an error message when the capacity is exceeded and stop the program, but I can't seem to get any further than that. Any help would be greatly appreciated. The lesson today on arrays/pointers was very sub par.

The program runs just fine if I comment out three of the students I am trying to add. So I know my only problem at this point is the array issue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
  // Exercise.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

#ifndef COURSE_H
#define COURSE_H
class Course {
public: 
	Course(const string& courseName, int capacity);
	~Course();
	string getCourseName() const;
	void addStudent(const string& name);
	void dropStudent(const string& name);
	string* getStudents() const;
	int getNumberOfStudents() const;
	void clear();

private: 
	string courseName; 
	string* students; 
	int numberOfStudents; 
	int capacity; 

};
#endif

Course::Course(const string& courseName, int capacity) {
	numberOfStudents = 0; 
	this->courseName = courseName; 
	this->capacity = capacity; 
	students = new string[capacity];
}

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


string Course::getCourseName() const {
	return courseName;
}

void Course::addStudent(const string& name) {

	students[numberOfStudents] = name;

	numberOfStudents++;

//CANT FIGURE THIS OUT

}

void Course::dropStudent(const string& name) {
	//Find the students name, delete it and shift everything down one spot in the array
	for (int i = 0; i < numberOfStudents; i++) {
		if (students[i] == name) {
			numberOfStudents--;
			for (int j = i + 1; j < numberOfStudents + 1; j++) {
				students[i] = students[j];
			}
		}
	}
}

string* Course::getStudents() const {
	return students;
}

int Course::getNumberOfStudents() const {
	return numberOfStudents;
}

void Course::clear() {
	numberOfStudents = 0;
	
}



int main()
{
	Course course1("Data Structures", 10);
	Course course2("Database Systems", 2);

	course1.addStudent("Peter Jones");
	course1.addStudent("Brian Smith");
	course1.addStudent("Anne Kennedy");
	course1.addStudent("Susan Kennedy");
	course1.addStudent("John Kennedy");
	course1.addStudent("Kim Johnson");
	course1.addStudent("S1");
	course1.addStudent("S2");
	course1.addStudent("S3");
	course1.addStudent("S4");
	course1.addStudent("S5");
	course1.addStudent("S6");
	course1.addStudent("S7");

	course2.addStudent("Peter Jones");
	course2.addStudent("Steve Smith");

	cout << "Number of students in course1: "
		<< course1.getNumberOfStudents() << endl;

	string *students = course1.getStudents();

	for (int i = 0; i < course1.getNumberOfStudents(); i++)
		cout << students[i] << ", ";

	cout << endl;

	cout << "Number of students in course2: " << course2.getNumberOfStudents() << endl;

	course1.dropStudent("S1");
	cout << "Number of students in course1: " << course1.getNumberOfStudents() << endl;

	students = course1.getStudents();

	for (int i = 0; i < course1.getNumberOfStudents(); i++)
		cout << students[i] << ", ";

	cout << endl;
	course1.clear();
	cout << "Number of students in course1: " << course1.getNumberOfStudents() << endl;

	return 0;
}

Last edited on
Look at line 86. Change the 10 to 13. Does that work?
Please do not start multiple threads for the same problem.
http://www.cplusplus.com/forum/beginner/197645/

Please see my response in that thread.

It will make it work, but the idea of the exercise was to make the array so that it adjusts to a higher number.
Topic archived. No new replies allowed.