1

I want to make a library management system as an assignment

class student
{
          char ID_number[30];
          char Student_name[30];
      public:
          void create_student()
          {
                    cout<<"\nEnter The ID Number ";
                    cin>>ID_number;
                    cout<<"\n\nEnter The Name of The Student: ";
                    cin>>Student_name;

                    cout<<"\n\nStudent Created Successfully"<<endl;
          }
          void show_student()
          {
                    cout<<"\nID Number: "<<ID_number;
                    cout<<"\nStudent Name: ";
                    cin.getline(Student_name,30);


          }

How would i go about using dynamic allocation to make every new entry go into an array and use pointers to show a certain student?

I am really bad at this particular part, thanks in advance!

5
  • 1
    You may use std::vector<student>. Commented May 12, 2017 at 8:40
  • Well sadly for this assignment i can only use dynamic allocation I think i can do it but not without listing a specific array size Commented May 12, 2017 at 8:49
  • vector provide dynamic allocation. You don't need to specify the size of it Commented May 12, 2017 at 8:50
  • So write your own vector. Commented May 12, 2017 at 8:50
  • Sorry i haven't been very clear, i need to use new[] and delete[] not vectors Commented May 12, 2017 at 8:54

3 Answers 3

1

Since you are coding in C++, and not in C, I would suggest using a string class, like std::string, instead of raw char arrays, e.g.:

std::string student_name;

If you want to store a list of students in memory, I'd suggest using the std::vector container, e.g.:

std::vector<Student> students;

You can use the vector::push_back method to add new students to the container.


EDIT If you want to use new[] and delete[] as a learning exercise, you may allocate a big array of students, and then use an integer index inside the array, pointing to the first free slot for inserting new students. E.g.:

int capacity = 100;
Student* students = new Student[capacity];

int student_count = 0;

To add a new student, you copy it in the array index given by student_count, and then you increment this variable by one, pointing to the next free slot in the pre-allocated array.

You must pay attention to not overflow the capacity of the array. Once the pre-allocated array is full, if you want to add a new student you need to allocate a new array with bigger capacity. You can use a 2x or 1.5x scale factor to calculate the new capacity.

Dont't forget to release the memory previously allocated with delete[].

If you do that, you are basically kind of implementing std::vector from scratch (even if this standard container has additional advanced features, like move semantics, etc.).

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

4 Comments

But string doesn't let it read more than on word after the space no? I want it to read a full line of characters i.e "john doe", if i use string and cin>>student_name; it'll just get "john" and ignore the rest And i can only use dynamic allocation in this assignment, actually this is the whole point of the assignment sadly. Edit: to be clear i need to use new[] and delete[] no vectors
@00qq00: Good question. Use getline(cin, student_name);.
Thanks this works!, any idea how to use new[] delete[] instead of vectors?
Thanks, i'll go ahead and try
0

If by dynamic you mean you want to create "a few" stduent records, but aren't sure how many you don't need to worry about new or delete. Just use a vector.

std::vector<student> students;
while(!bored)
{
    student latest;
    latest.creat_student(); // this looks a bit odd, perhaps it should be a free function...
    vector.push_back(latest);
    //TODO - see if I am bored yet
}

Now we have students. You said you wanted to use pointers to display them. Or rather a certain student. You haven't described how you will choose a "certain student", so let's show them all first

for (auto item : students)
{
    item.show_student();
}

Disappointingly, your show_student method isn't declared const, which you should consider.

To index to a particular student, you could use students[17]. Or which ever index you want. You might be able to use std algorithms to find students with particular properies, if you provide a way to get the properties (and of course consider using std::string instead of char *)


If you really want to use a raw array of student, then something like this works, but you will get into reallocs etc if 5 or whatever you choose isn't enough. Which is a compelling reason for choosing a std::vector.

student * them = new student[5];
for (size_t i = 0; i < 5; ++i)
{
    student latest;
    latest.create_student();
    them[i] = latest;
}

for (size_t i = 0; i < 5; ++i)
{
    them[i].show_student();
}
delete[] them;

2 Comments

Okay so with new and delete i have to declare the size of my array every time?
Exactly - just added an edit. Vector deals with this for you, and reallocates when it needs to. If you use a raw array you need to do this yourself.
0

std::vector<student> or std::vector<student*> if you want a pointer.

Adding a new student :

Student* newStudent = new Student(name);

vector.append(newStudent);

Reading a student called "Victor" and then deleting it:

Student* victor;
for(int i = 0; i<vector.size();i++) {
    if(vector.at(i)->getName() == "Victor") {
        victor = vector.at(i);
        break;
    }
 }
//Then you can delete it
delete(victor);

std::vector

5 Comments

I can only use new[] and delete[] so any ideas how and where to insert them?
I am not sure of what do you mean with new and delete.
public: void * operator new[] (size_t) { return 0; } void operator delete[] (void*) { } }; int main() { MyClass *pMyClass = new MyClass[5]; delete [] pMyClass; } I need to use something like this
That would work if you insist on using a c-style array, sorta by why define your own?
Is it a student exercice ? If it is @Mr.C64 is better than mine.

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.