Hello everyone I'm currently working on a program that takes a student name and number of classes. Then asks the user to enter the his classes. I have most of the program done but I'm having trouble with my dynamic array for some reason it wont let me type in more than one class before it crashes. Can someone please help me out with this.

#include <iostream>

using namespace std;




class Student
{
    string name;
    int numClasses, key;
    string *classList;

        public:
                Student();
                Student(int , string);
                void user_Input();
                void class_Output();
};

Student::Student()
{
}

Student::Student(int classes, string student)
{
    numClasses = classes;
    name = student;
    classList = new string [numClasses];

}

void Student::user_Input()
{
    cout<<"Please enter your name"<<endl;
    cin>>name;

    cout<<"Please enter the number of classes you have"<<endl;
    cin>>numClasses;

    cout<<"Please enter your classes: "<<endl;

    for(int i=0; i<numClasses; i++)
    {
            cin>>classList[i];
    }


}

void Student::class_Output()
{

cout<<"\n"<<name<<endl;

}


int main()
{
    Student obj;
    obj.user_Input();
    obj.class_Output();




    return 0;
}

The default construtor doesn't do anything with the classList pointer, so it's still uninitialized when you call user_Input(). The ideal would be to eschew a dynamic array in favor of a standard vector object, but if you must use a dynamic array, it might be a good idea to ditch the default constructor entirely, or move your initialization steps to user_Input().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.