Help with header/cpp files

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.

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


#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; 

};


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++;
}

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

}

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

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

#endif

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();

	return 0;
}

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.
Last edited on
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.

So it would be something like the following?

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
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.
Last edited on
Almost.

Lines 5-7 belong at the end (after you have possibly increased the size).

Line 8: You want to allocate studentsNew according to the desired new capacity.

Lines 8-14: You only want to do this IF the existing array is full.

Line 11: The assignment is the wrong direction

Lines 16-20 are not needed.

You're missing assigning studentsNew to students.

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
void Course::Resize (int newcap)
{   string* studentsNew;
    
    //  Allocate a new array
    studentsNew = new string[newcap];
    //  Copy existing array to new array
    for (int i=0; i<numberOfStudents; i++)
        students[i] = studentsNew[i];
    //  Delete old array        
    delete[] students;
    //  Assign pointer to new array
    students = studentsNew;
    //  Update capacity
    capacity = newcap;                
}   
 
void Course::addStudent(const string& name) 
{   //  Check if arrray is full
    if (numberOfStudents == capacity)
    {   //  Array is full
        Resize (capacity * 2);
    }
    students[numberOfStudents] = name;
	numberOfStudents++;
}


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
I just noticed the assignment in line 8 is reversed.
It should be:
 
  studentsNew[i] = students[i];

Last edited on
Topic archived. No new replies allowed.