Create class Students

hi i need help in finishing the program code using a class called Student that will capture the following details;
1. registration number
2. student name
3. dob(date of birth)
4. course registered eg BSIT
5. year of admission
6. current year of study.
Program should read records for at least 5 student and display in table form

This is what i have so far. i am a student btw. would appreciate your help.
................................................................................
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
class Student
{
private:
string name, reg, study, course; int dob, yoa;
public:
Student(string, int);
void getdata(); void display();
};

Student::Student(string name, int dob)
{
name, reg, study, course; dob, yoa;
}

void Student::getdata()
{
cout<<"\n Enter Student Registration:"<<endl;
cin>>reg;
cout<<"\n Enter Student Name:"<<endl;
cin>>name;
cout<<"\n Enter Date of Birth:"<<endl;
cin>>dob;
cout<<"\n Enter Course:"<<endl;
cin>>course;
cout<<"\n Enter Year of Admission:"<<endl;
cin>>yoa;
cout<<"\n Enter Year of Study:"<<endl;
cin>>study;
}

void Student::display()
{
cout<< setw(68)<<"\n Student Registration summary.\n \t\t\tJKUAT MSA CBD CAMPUS";
cout<<"\n Reg number"<<setw(15) <<" Name"<<setw(5)<<" DoB"<<setw(5)<<" Course"<<setw(5)<<" YoA "<<setw(5)<<" YoS"<<setw(5);
cout<<"\n"<<reg<<setw(8)<< name<<setw(5)<< dob<<setw(5)<< course<<setw(5)<< yoa<<setw(5)<< study<<setw(5);
}

int main()
{
Student Student1("Jo", 10);
Student Student2("Ch", 8);
Student Student3("Pa", 4);
Student Student4("Be", 6);
Student Student5("Ze", 5);
cout<<"\n Student Details:"<<endl;
Student1.getdata(); Student1.display();
cout<<"\n Student Details:"<<endl;
Student2.getdata(); Student2.display();
cout<<"\n Student Details:"<<endl;
Student3.getdata(); Student3.display();
cout<<"\n Student Details:"<<endl;
Student4.getdata(); Student4.display();
cout<<"\n Student Details:"<<endl;
Student5.getdata(); Student5.display();

return 0;
}
................................................................................
This is wrong:
1
2
3
4
Student::Student(string name, int dob)
{
name, reg, study, course; dob, yoa;
}

Read the page on this site about constructors. you want to do something like this:

1
2
3
4
5
Student::Student(string name, int dob)
{
    this.name = name;
    this.dob = dob;
}

BUT, if you are giving setting this information through the constructor why are you then asking the user for it?

edit: why not read all the info from the user, and then pass it all in through the constructor?

there is a lot of repetition in your main function.
Consider using a for loop.
Last edited on
It would be better form to use the constructor init list.

1
2
3
Student::Student(string n, int d) : name(n), dob(s), yoa(0)
{
}


(reg, study, course are string members so can look after themselfves, when it comes to initializtion, but yoa is an int so need to be init.)

Andy

PS Also see the article "How to use code tags"
http://www.cplusplus.com/articles/jEywvCM9/
Topic archived. No new replies allowed.