I am trying to read full names that are separated by space from a file. In the example, name student
. Here is my code:
class student{
string name;
int degree;
int stage;
public:
void input(){
getline (my_file,name);
my_file>>degree;
my_file>>stage;
}
void display(){
cout<<name<<"\n"<<degree<<"\n"<<stage<<endl;
}
};
int main()
{
student S[3];
int len=0;
my_file.open("D:/h.txt", ios::in);
if (!my_file) {
cout << "No such file";
}
else
while (!my_file.eof()) {
S[len].input();
len++;
}
for(int i=0;i<3;i++)
S[i].display();
my_file.close();
return 0;
}
my "h.txt" file:
jack Donald
58
5
David William
82
4
Anthony Mark
89
3
My problem is, it don't show anything.