Dynamic arrays

i will implement a add a course function

description of function is :system will allow the instructor to add a new course indicating its
course id and course name. Since the course ids are unique, the system should check whether or not the
speci ed course id already exists (i.e., whether or not it is the id of another course), and if the course
exists, it should not allow the operation and display a warning message.

Here it will not be allowed to use vectors I must use dynamic arrays. My question is that how can I store int and string values in one array ? Should I use structure or anything else ?
Last edited on
Here it will not be allowed to use vectors I must use dynamic arrays. My question is that how can I store int and string values in one array ? Should I use structure or anything else ?


Yes, you should use an array of structures.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>

struct Course
{
    int id;
    std::string name;
};

// ...

Course *c = new Course[100]; 

c[0].id = 1;
c[0].name = "Math";

// ...

delete[] c; // important! 

Thank you i tried but it couldn't work. my code is :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct Course{
        int cId;
        string courseName;
        
    };

void StudentReviewSystem:: addCourse( const int courseId, const string courseName ){
    
    Course * ptr = new Course[100];
    for(int i = 0; i<count ; i++ ) // checks courseId
        if(ptr[count].cId==courseId)
            cout<< "Course "<< courseId<<" already exists"<<endl;
    
    ptr[count].cId = courseId;
    ptr[count].courseName=courseName;
    count++;
    cout<<"Course "<<courseId<<" has been added";
    

}
closed account (o1vk4iN6)
if(ptr[count].cId==courseId)
You are using count as the index so it checks for only one, and its out of bounds at that. You also aren't keeping any data... new allocated new memory each time so your old data won't be used...
You also forgot:

1
2
3
// ...

delete[] c; // important! 
Last edited on
thanks for your answer I have corrected the first part but for second part, How could I solve the part
You also aren't keeping any data... new allocated new memory each time so your old data won't be used...
How should I declare it ?
How should I declare it ?

If you need to write a StudentReviewSystem class, you should make ptr member data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class StudentReviewSystem
{
private:

    Courses *ptr;
    int how_many_courses; // TODO: use size_t instead of int

public:

    StudentReviewSystem(): ptr(NULL), how_many_courses(0)
    {
    }

    // TODO: write copy constructor and assignment operator=

    ~StudentReviewSystem()
    {
        delete[] ptr;
    }
};


A much simpler solution would be not writing a StudentReviewSystem class, make ptr a global variable, and then make StudentReviewSystem::addCourse() a standalone global function named addCourse().
using global variable is not allowed. I have tried but again I failed. What is missing ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public:
    
    StudentReviewSystem();
   ~StudentReviewSystem();
    void addCourse( const int courseId, const string courseName );

struct Course{
        int cId;
        string courseName;
        
    };
    
private:
    int count=0;
    Course *ptr;



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
StudentReviewSystem::StudentReviewSystem(): ptr(NULL)
{
}



void StudentReviewSystem:: addCourse( const int courseId, const string courseName ){
    
    
  
    for(int i = 0; i<count ; i++ ){ // checks courseId
        if(ptr[i].cId==courseId){
            cout<< "Course "<< courseId<<" already exists"<<endl;}
            else{
                ptr[count].cId = courseId;
                ptr[count].courseName=courseName;
                count++;
                cout<<"Course "<<courseId<<" has been added";}

        }


}

Last edited on
Topic archived. No new replies allowed.