2 Problems:
- I ran valgrind on this program an lost around 400 bytes, but I don't know what I did wrong.
- For some reason, upon the FIRST (and only the first) initialization of a student object (
roster[i]
whenint i = 0
), the loop is only allowing me to pass two parameters into my constructor. I know it sounds strange but the first loop invocation is different from the others.
HEADER
#include<iostream>
#include<string>
using namespace std;
class Student{
public:
Student();
Student(string ln, string fn, string cn) {setLast(ln);setFirst(fn);setCourse(cn);}
void showInfo(){cout << firstname << " " << lastname << " in " << coursename << endl;}
protected:
void setLast (string l){lastname = l;}
void setFirst (string f){firstname = f;}
void setCourse (string c){coursename = c;}
string firstname;
string lastname;
string coursename;
};
MAIN
int main(){
Student **roster;
int num;
cout << "how many?";
cin >> num;
roster = new Student*[num];
string l,f,c;
for (int i = 0; i < num; i++){
cout << "\nenter last, first, course: ";
getline(cin, l);
getline(cin, f);
getline(cin, c);
roster[i] = new Student(l, f, c);
roster[i]->showInfo();
}
delete [] roster;
};