0

I am working on an assignment where we have to create a "student" object with a "course" member array that is dynamic so the user can enter from one course to as many as they'd like in the array. I've been trying this every which way and just cannot get the array to resize and accept new elements. Here is my code as it stands now:

cout << "Enter student name > ";
cin >> name;

Student firstStudent(name);

cout << endl << "Enter \"done\" when you are complete";
cout << endl << "Enter a new course : ";
cin >> course;

while (course != "done") {
    numCourses++;
    firstStudent.addElement(numCourses, course);//makes the array bigger
    cout << endl << "Enter a new course : ";
    cin >> course;

}//end while

member variables and constructor:

class Student
{
private:
   string name;//name of student

   string *dynArray = new string[1];//array

public:
Student::Student(string name)
{
   this->name = name;
   this->numC = 1;//initialized to one to start with    
}

resizing and adding an element to the array:

void Student::addElement(int numCourses, string &course) {//DYNAMIC ARRAY
    if (numCourses > this->numC) {//checks if there are more courses than there is room in the array
       this->numC = numCourses; //this changes the member variable amount
       dynArray[numCourses] = course;//adds new course to array

       string *temp = new string[numC];//create bigger temp array

       temp = dynArray;//write original to temp

       delete[]dynArray; //this tells it that it's an array

       dynArray = temp;//make dynarray point to temp

    }
    else {
       dynArray[numCourses] = course;//adds new course to array
    }
}//end addElement

Even if I manage to get rid of compile errors, it will often come up with runtime errors. I'm sure it has to do with pointers/copying arrays but I'm just learning and haven't yet mastered these concepts.

Edit: dynArray[numCourses] = course; creates a char array. ie if course = "one", dynArray[0] = "o", dynArray[ 1] = "n", etc. Does anyone know how to keep this from happening?

screenshot

17
  • You can't copy arrays with the assignment temp = dynArray. You have to write a loop that copies each element of the array. Commented Oct 28, 2015 at 21:15
  • Where you declare numC? Commented Oct 28, 2015 at 21:19
  • 2
    @NeilKirk that would mean writing good code, which is not allowed in school Commented Oct 28, 2015 at 21:32
  • 1
    If std::vector is not available (to make you learn how it works under the hood) then the correct thing to do is implement your own vector-type class, and use that, rather than mixing the logic of how to handle a resizable array alongside other unrelated pieces of code. Commented Oct 28, 2015 at 21:34
  • 1
    @Barmar Implementing data structures is harder than using them. Beginner classes at school often require students to do both, and do not teach either well. Commented Oct 28, 2015 at 21:35

2 Answers 2

1

The first line that that doesn't look quite correct is this one:

dynArray[numCourses] = course;

Before it is checked that numCurses > numC, which means that dynArray[numCourses] accesses memory out of bounds (the bounds are 0 to numC - 1.

The other thing that strikes me as interesting is that the addElement method takes numCourses as an argument. I would expect that it should behave similar to std::vector<T>::push_back. Are you sure this argument is necessary?

The last thing I want to comment on is that the values from dynArray are not copied. Have a look at std::copy_n on how to do the copy.

Sign up to request clarification or add additional context in comments.

4 Comments

Actually, one last thing: I would expect that you start off with an empty array. Starting with string[1] is probably not what you want to do.
I had started with a one element array because I knew I needed one element at least anyway and to make sure that it was at least accepting a string as an element even if it isn't resizing. I'll make sure to change it when I get everything working. As for numCourses, that's a number that is increased every time an element is added which is how I base the size of the array. I'm not sure anymore why it is passed as an argument but I had a reason... at one point XD Does it pose a problem or just pointless?
Consider this code: s.addElement(0, "a"); s.addElement(2, "b"); What is the value of the second element?
Ahhhh, I see what you mean. rookie mistake.
0

I'm not sure if this would be considered cheating, but here's a break down of how std::vector is implemented. You could use that as a reference and just pick out the parts you need.

http://codefreakr.com/how-is-c-stl-implemented-internally/

1 Comment

I think it's not cheating so long as I understand the code and can implement it in the future :) thanks!

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.