Im having trouble with my classList variable. It's a dynamic array of strings used to store the names of the classes(my program ask for students name, number of classes, then a list of the classes).
Everythinng works till I type in a class. If I type one letter, such as "a", everything is fine. Yet, if I type say "Math" then hit enter. It will print "Enter name of class" and then "press any key to continue". It wont let me type another class. I figured my problem is in the input() finction. I mean I think the problem is that each letter takes up an index space, but I'm just now learning about strings and dynamic arrays so I dont know what a good solution could be.
Thanks.
Code:
typedef char* CharPtr;
class Student
{
public:
Student& operator =(const Student& rightside);
~Student();
void input();
void output();
void setNumClasses(int);
int getNumClasses();
void setClassList(char []);
char getClassList();
void setName(string);
char getName();
void reset();
private:
string _name;
int _numClasses;
char *_classList;
};
void main()
{
Student classInfo;
classInfo.input();
}
void Student::input()
{
CharPtr classList;
classList = new char[];
int numberClasses;
string name;
cout<<"Enter students name ";
cin>>name;
setName(name);
cout<<"Enter the number of classes student is taking ";
cin>>numberClasses;
setNumClasses(numberClasses);
for(int index = 0; index < numberClasses; index++)
{
cout<<"Enter name of class ";
cin>>classList[index];
}
setClassList(classList);
}
Thanks.
Comment