Efe Gürkan
Efe Gürkan

Reputation: 23

Copying a static array to a dynamic array in c++

I am trying to write a method that creates a course, and adds it to the static array of the SRS. however, my code gives no errors but doesn't add the course at all. What am i doing wrong? By the way courses is dynamic array.

This code is correct now:

#include "StudentReviewSystem.h"

StudentReviewSystem::StudentReviewSystem()
{
    numberOfCourses = 0;
    courses = new Course[0];
}

void StudentReviewSystem::addCourse( const int courseId, const string courseName )
{
int i = findCourse( courseId );

 if ( i == -1 )
  {
  int newNum = numberOfCourses + 1;
  Course *newCourses = new Course[newNum];

  for ( int j = 0; j < numberOfCourses; j++ )
  {
     newCourses[j] = courses[j];
  }

   Course aCourse(courseId, courseName);

   newCourses[numberOfCourses] = aCourse;

   delete []courses;
   courses = newCourses;
   numberOfCourses = newNum; 

   cout<< "Course added"<< endl;
   }

   i = findCourse(courseId);
   cout<< i;
}

Thanks for help everyone!

Upvotes: 0

Views: 718

Answers (4)

Wolf
Wolf

Reputation: 10238

You may create the array on heap using new []:

 Course* newCourses = new Course[newNum];

then copy as you already do and add the new course. Don't forget to delete the old courses array

 delete [] courses;
 courses = newCourses;

and to update numberOfCourses (I think here is your error)

 numberOfCourses = newNum;

Upvotes: 0

Dmitry Ledentsov
Dmitry Ledentsov

Reputation: 3660

int newNum = numberOfCourses + 1;
Course newCourses [newNum];

is not legal c++ and there's no need for copying from whatever array to another in your case. Just use a std::vector as a member of your class and use its push_back method.

Did your teacher explain you a reason for not using std::vector? If not, you'd better of thinking for yourself and trying your best to produce good software and not hacks. Implementing a dynamically sized container is possible. It it's the homework task, then there's no need to write the StudentReviewSystem class, but concentrate on implementing a dynamically sized array. If not, then you shouldn't reinvent two wheels at a time, but concentrate on one - the StudentReviewSystem, and use std::vector.

Upvotes: 0

Abhishek Bansal
Abhishek Bansal

Reputation: 12705

Course newCourses [newNum];

This is not valid C++. newNum should be a compile time constant.

Also, I don't think you can directly equate two static arrays.

courses = newCourses;

Instead assign individual elements using a for loop, or better still, use std::vector.

Try the following code:

if(i == -1) {
        int newNum = numberOfCourses + 1;
        Course* newCourses = new Course[newNum];
        for(int j = 0; j < numberOfCourses; j++) {
            newCourses[j] = courses[j];
        }

        Course aCourse(courseId, courseName);

        newCourses[numberOfCourses] = aCourse;
        delete [] courses;
        courses = new Course[newNum];
        for(int j = 0; j < newNum; j++) {
            courses[j] = newCourses[j];
        }
        delete [] newCourses;
        cout<< "Course added"<< endl;
    }

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 311068

There are several errors in the code. in C++ when a variable is declared as an array the size of the array shall be a cobstabt expression. So this statement is invalid

Course newCourses [newNum];

In C/C++ there is no assignment operator for arrays. So this code is also invalid

courses = newCourses;

Also you forgot to change value of varaibale numberOfCourses. Shpuld be

numberOfCourses = newNum;

Also you may not enlarge arrays in C/C++.

Instead of using an array you should dynamically allocate memory. So courses should be defined as

Course *courses;

Taking into account other necessary changes the function would look the following way

void StudentReviewSystem::addCourse( const int courseId, const string courseName )
{
   int i = findCourse( courseId );

   if ( i == -1 )
   {
      int newNum = numberOfCourses + 1;
      Course *newCourses = new Course[newNum];

      for ( int j = 0; j < numberOfCourses; j++ )
      {
         newCourses[j] = courses[j];
      }

      Course aCourse(courseId, courseName);

      newCourses[numberOfCourses] = aCourse;

      delete []courses;
      courses = newCourses;
      numberOfCourses = newNum; 

      cout<< "Course added"<< endl;
   }

   i = findCourse(courseId);
   cout<< i;
}

Upvotes: 1

Related Questions