-2

I am new to programming and working on a basic student management system. The system first asks the user for data input. 6 things; the id, name, university, department, semester and gpa. The data input is working perfectly. After the data input is complete, a menu appears with three options. one is to create a new student profile. In that case, when the user enters the name of the student, the program is terminated abruptly. I've used cin.ignore every single place where it makes sense but the same problem occurs. Any help would be appreciated. Thank you. Code is below. NOTE: I was not allowed to use structures and/or vectors.

#include<iostream>
#include<string>
using namespace std;

int main()
{
cout<<"------------------------------------- Welcome to student management system! ----------------------------------------------"<<endl;
cout<<"------------------------------------------------DATA INPUT----------------------------------------------"<<endl;
cout<<"Enter the number of students you want to enroll: ";
int size;
int n=0;
int counter=0;
cin>>size;

int *student_ids=new int[size];
int *semester=new int[size];
float *cgpa=new float[size];
string *names=new string[size];
string *uni=new string[size];
string *dept=new string[size];

int *ptr_id,*ptr_semester;
string *ptr_names,*ptr_uni,*ptr_dept;
float *ptr_cgpa;

cout<<"Enter their ids: "<<endl;
ptr_id=student_ids;
for(int i=0;i<size;i++)
{
    cin>>*ptr_id;
    ptr_id++;
}

ptr_names=names;
cout<<"Enter their names: "<<endl;
cin.ignore();
for(int i=0;i<size;i++)
{

    getline(cin,*ptr_names);
    ptr_names++;
}

ptr_uni=uni;
cout<<"Enter their Universities: "<<endl;
for(int i=0;i<size;i++)
{
    getline(cin,*ptr_uni);
    ptr_uni++;
}

ptr_dept=dept;
cout<<"Enter their departments: "<<endl;
for(int i=0;i<size;i++)
{

    getline(cin,*ptr_dept);
    ptr_dept++;
}

ptr_semester=semester;
cout<<"Enter their semester: "<<endl;
for(int i=0;i<size;i++)
{
    cin>>*ptr_semester;
    ptr_semester++;
}

ptr_cgpa=cgpa;
cout<<"Enter the GPA: "<<endl;
for(int i=0;i<size;i++)
{
    cin>>*ptr_cgpa;
    ptr_cgpa++;
}
cout<<"-----------------------------------------------DATA INPUT COMPLETE----------------------------------------------------------- "<<endl;

int choice;
char again='y';
while(again=='y'||again=='Y')
{

cout<<"Press 1 to Create a Student Profile"<<endl;
cout<<"Press 2 to Search a Student"<<endl;
cout<<"Press 3 to Delete a Student record"<<endl;
cin>>choice;

switch(choice)
{
    case 1: //CREATING STUDENT PROFILE

        cout<<"Enter the number of profiles you want to create: ";
        cin>>n;
        cout<<"Enter ID(s): ";
        for(int i=size;i<size+n;i++)
        {
            cin>>*ptr_id;
            ptr_id++; 
        }
        cout<<student_ids[1];
        cout<<student_ids[0];

        cout<<"Enter name(s): ";
        cin.ignore();
        for(int i=size;i<size+n;i++)
        {

            getline(cin,*ptr_names);
            cin.ignore();
            ptr_names++;
        }
        cout<<names[1]<<endl;

        cout<<"Enter University: ";
        for(int i=size;i<size+n;i++)
        {

            getline(cin,*ptr_uni);
            ptr_uni++;
        }

        cout<<"Enter Department(s): ";
        for(int i=size;i<size+n;i++)
        {
            getline(cin,*ptr_dept);
            ptr_dept++;
        }
        cout<<dept[2]<<endl;

        cout<<"Enter semester(s): ";
        for(int i=size;i<size+n;i++)
        {
            cin>>*ptr_semester;
            ptr_semester++;
        }

        cout<<"Enter CGPA: ";
        for(int i=size;i<size+n;i++)
        {
            cin>>*ptr_cgpa;
            ptr_cgpa++;
        }

    cout<<"------------------------------------------------SUCCESS-------------------------------------------------------------";
    cout<<endl;
    break;

    case 2: //SEARCHING
        int searching_method;
        cout<<"Press 1 to search by ID."<<endl;
        cout<<"Press 2 to list all students."<<endl;
        cin>>searching_method;

        switch(searching_method)
        {
            case 1: //SEARCHING BY ID
                cout<<"Enter the student ID: ";
                int search;
                cin>>search;
                for(int i=0;i<size+n;i++)
                {
                    if(student_ids[i]==search)
                    {
                        counter++;
                        cout<<"Name: "<<names[i]<<endl;
                        cout<<"University: "<<uni[i]<<endl;
                        cout<<"Department: "<<dept[i]<<endl;
                        cout<<"Semester: "<<semester[i]<<endl;
                        cout<<"CGPA: "<<cgpa[i]<<endl;
                        break;
                    }
                }
                if(counter==0)
                {
                    cout<<"No student found!"<<endl;
                }
                break;

            case 2: //LIST OF ALL STUDENTS
                for(int i=0;i<size+n;i++)
                {
                    cout<<"Student ID: "<<student_ids[i]<<endl;
                    cout<<"Name: "<<names[i]<<endl;
                    cout<<"University: "<<uni[i]<<endl;
                    cout<<"Department: "<<dept[i]<<endl;
                    cout<<"Semester: "<<semester[i]<<endl;
                    cout<<"CGPA: "<<cgpa[i]<<endl; 
                    cout<<endl;
                    cout<<endl;                       
                }
                break;
        }

    //case 3: //DELETEING A STUDENT ID
}
}
system("pause");
return 0;
}
Ibrahim Farooq
  • 408
  • 1
  • 3
  • 14
  • What does the pointer `ptr_names` point to? Why do you need a pointer, in the first place? – Algirdas Preidžius Nov 22 '19 at 13:54
  • 4
    Please try to learn how to create a [mcve], with emphasis on the minimal part. – Some programmer dude Nov 22 '19 at 13:55
  • I am not going to read this example, give a minimal example. – Tanveer Badar Nov 22 '19 at 13:56
  • @AlgirdasPreidžius It points to the string *names=new string[size]; – Ibrahim Farooq Nov 22 '19 at 13:56
  • 1
    And please learn about *structures* and *classes* and [*vectors*](https://en.cppreference.com/w/cpp/container/vector). And instead of pointers and pointer arithmetic and dereference, use array indexing as in e.g. `student_ids[i]`. – Some programmer dude Nov 22 '19 at 13:56
  • @Someprogrammerdude I know about structures but our professor did not want us to use them yet. – Ibrahim Farooq Nov 22 '19 at 13:58
  • Look at the pointer arithmetic in `case 1: //CREATING STUDENT PROFILE`, this can't be right. I suggest making a `student` class and use an `std::vector` to manage your memory instead of juggling a bunch of arrays with pointer arithmetic. Also please read [Why is using namespace std; considered bad practice?](https://stackoverflow.com/q/1452721/10411602). – Blaze Nov 22 '19 at 14:00
  • @IbrahimFarooq OK, I see that now. Now, what does your `ptr_id`, `ptr_names`, etc., point after your data input? Don't you write past the bounds of the allocated array? Array has only `size` elements, while you write until `size+n` elements (`for(int i=size;i – Algirdas Preidžius Nov 22 '19 at 14:03
  • @AlgirdasPreidžius Pointers weren't necessary but i'm still learning and my professor wanted us to understand pointers thoroughly which is why i had to use them – Ibrahim Farooq Nov 22 '19 at 14:03
  • @AlgirdasPreidžius size+n was used because in case 1 as you can see program asks the user to create another student profile. It asks how many student profiles to create which is stored in "n". So, the size of the array is changed to size+n. – Ibrahim Farooq Nov 22 '19 at 14:05
  • @IbrahimFarooq "_So, the size of the array is changed to size+n._" Where do you change the array size to `size+n`? Yes, you ask the user how he wants to increase the size. You just never increase it. The size of allocated array can't change after allocation, nor does it change dynamically, if you start writing past the bounds of it. The computer does what you tell it to do, not what you want it to do. – Algirdas Preidžius Nov 22 '19 at 14:08
  • @Blaze I'm not allowed to used structures or classes by our professor. Well regarding the pointer arithemetic, my logic is that in the DATA INPUT section, the respective pointers point at the last index of the array and in the case 1 they point onwards. – Ibrahim Farooq Nov 22 '19 at 14:09
  • @IbrahimFarooq *and my professor wanted us to understand pointers thoroughly which is why i had to use them* -- This way of teaching C++ is why so many students drop C++ and pick up other languages, where they are actually creating useful applications. That entire program you're attempting to write could be done in half the code you've written, with little to no bugs, if given a chance to use what C++ has to offer. – PaulMcKenzie Nov 22 '19 at 14:10
  • You never reset your pointers. After `"Enter their ids: "` the `ptr_id` will be pointing out of bounds of the memory you have allocated. – Some programmer dude Nov 22 '19 at 14:11
  • @AlgirdasPreidžius These are all dynamic arrays. – Ibrahim Farooq Nov 22 '19 at 14:29
  • @Someprogrammerdude I did that on purpose because i wanted to input at the next index to where the pointer is pointing respectively – Ibrahim Farooq Nov 22 '19 at 14:30
  • But when you're coming to the `switch` the pointers will not be pointing to elements in the "arrays" you allocate, dereferencing them *will* lead to *undefined behavior*. And this is very likely the reasons for the "crash" you probably experience. – Some programmer dude Nov 22 '19 at 14:33
  • What is the purpose of `ptr_id` and the rest of those `ptr_...` variables anyway? They are unnecessary. You can simply iterate for example, `student_ids[x]`, where `x` is the index of the student. I think you went too far with the "you must use pointers" orders from your teacher. – PaulMcKenzie Nov 22 '19 at 14:39
  • @Someprogrammerdude They actually are pointing at the next index. I printed the 3rd value after selecting the initial size to 2, and it gave the right value. – Ibrahim Farooq Nov 22 '19 at 14:40
  • @IbrahimFarooq "_These are all dynamic arrays._" Dynamic in a sense that the size can be not defined at compilation time. Not in the sense, that they will resize automatically. Arrays in C++ does not do that. Please review the [C++ book of your choice](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) if you think otherwise. Accessing the arrays out of bounds is undefined behavior. Which means: You may get correct behavior at this time, but it will fail, when you try to show it to your professor. The behavior is, literally, undefined. – Algirdas Preidžius Nov 22 '19 at 14:46
  • After the `"Enter their ids: "` loop, `ptr_id` will point to `student_ids[size]`. This is out of bounds. You continue to go even further out of bounds. Just because something *seems* to work doesn't mean it's correct. Just think a little more about the logic. Draw it up using pen and paper if you want, drawing arrows as you iterate in your loops. And use your [rubber duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging) (or roommate, family member or other suitable substitute). – Some programmer dude Nov 22 '19 at 15:04
  • Also, if this is about dynamic arrays, your code is not going to be able to accomplish this. You basically have to create your own home-made `vector` class to add / delete, etc. – PaulMcKenzie Nov 22 '19 at 15:11

1 Answers1

0

So, in the beginning of your program you ask the user to enter a size for the number of students. When you finish initializing the students in your 'database'. Now, with the next student you try to add, you don't have any more space in the ptr_names array, as you have filled it in in the initializing step.

What you can do:

-If you know the maximum number of students that can be created, set the size to that number

-The better solution would be using std::vector for a dynamic sized container.

To make your code even better, you could make a student structure or class like this

struct student{
    int id;
    int semester;
    float cgpa;
    std::string name;
    std::string uni;
    std::string dept;
}

This way, you can store all your students using a vector that looks like this:

std::vector<student> all_students;

More about vectors here https://en.cppreference.com/w/cpp/container/vector

kookie
  • 49
  • 3