0

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!

2
  • 2
    This is not valid C++. C++ arrays must have a constant-expression size. You should use a std::vector<Course>. Commented Nov 19, 2013 at 9:31
  • How have you declared your courses array? Commented Nov 19, 2013 at 9:32

4 Answers 4

1

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

(+1) there is at least a typo in your answer cobstabt second line
first this code didn't work. I've deleted delete [] courses and it started to work. is there any problem with doing that?
1
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;
    }

5 Comments

Cources is numberOfCourses and new cources is numberOfCourses+1
I can't use vectors (our teacher doesn't allow). However, I used for loop to copy arrays. Still doesn't work.
Thanks for help! But, I'm getting a runtime error in line delete [] courses;
private: Course* courses; int numberOfCourses; in my header.
@EfeGürkan without delete []-ing the old courses array, you'll have a memory leak.
0
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.

Comments

0

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;

5 Comments

In my original code, I update numberOfcourses, but while trying to find the problem I accidentally deleted that line a guess ^^
@EfeGürkan did you try one of the dynamic versions meanwhile?
Yes. delete [] courses gives me runtime error. I'm trying to figure it out why.
courses should be initialized with NULL or with a dynamic array, this may be currently not the case. Would you please add the globals courses and numberOfCourses to your snippet as well.
I've added. I'm initliazing courses to size 0 array at first.

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.