Hi there,
I am implementing simple student class when i run the program it crashes after getting input for char* name. Program is as follows.
#include <iostream>
using namespace std;
class Student
{
private:
char* name;
int rollNo;
float cgpa;
public:
Student()
{
name = "";
rollNo = 0;
cgpa = 0.0;
}
void setName()
{
cout<<"Enter name : "<<flush; //after input my program crashes
cin >> name;
}
void setName(char* newName)
{
name = newName;
}
char* getName()
{
return name;
}
void setRollNo()
{
cout<<"Enter roll no : "<<flush;
cin >> rollNo;
}
void setRollNo(int newRollNo)
{
rollNo = newRollNo;
}
int getRollNo()
{
return rollNo;
}
void setCgpa()
{
cout<<"Enter cgpa : "<<flush;
cin >> cgpa;
}
void setCgpa(float newCgpa)
{
cgpa = newCgpa;
}
float getCgpa()
{
return cgpa;
}
};
int main()
{
Student s1;
//s1.setName("Ahmad");
s1.setName();
cout<<"Name :\t\t"<<s1.getName() <<endl;
//s1.setRollNo(23);
s1.setRollNo();
cout<<"Roll no :\t" <<s1.getRollNo() <<endl;
//s1.setCgpa(3.45);
s1.setCgpa();
cout<<"Cgpa :\t\t" <<s1.getCgpa() <<endl;
return 0;
}
Please see the comment where my program crashes, I don't know why ? Please help and explain, I am using Code Blocks. Thanks.