H
Husyn Raj
class person
{ protected:
char name[80];
int age;
public:
void getdata()
{cout<<"Enter name";
cin>>name;
cout<<"Enter age";
cin>>age;
}
void showdata()
{cout<<"Name "<< name;
cout<<"Age "<< age;
} };
char ch;
person pers;
fstream file;
file.open("person.txt",ios::app|ios:ut|ios::in|ios::binary);
do{
cout<<"Enter person data\n";
pers.getdata();
file.write(reinterpret_cast<char*>(&pers),sizeof(pers));
cout<<"Another person (y/n)";
cin>>ch; }while(ch=='y');
file.seekg(0);
file.read(reinterpret_cast<char*>(&pers),sizeof(pers));
pers.showdata();
file.read(reinterpret_cast<char*>(&pers),sizeof(pers));
pers.showdata();
file.close();
cout<<endl;
system("pause");
return 0;}
this is my code and pers.showdata(); is only showing the last data
entered how to do this properly...
{ protected:
char name[80];
int age;
public:
void getdata()
{cout<<"Enter name";
cin>>name;
cout<<"Enter age";
cin>>age;
}
void showdata()
{cout<<"Name "<< name;
cout<<"Age "<< age;
} };
char ch;
person pers;
fstream file;
file.open("person.txt",ios::app|ios:ut|ios::in|ios::binary);
do{
cout<<"Enter person data\n";
pers.getdata();
file.write(reinterpret_cast<char*>(&pers),sizeof(pers));
cout<<"Another person (y/n)";
cin>>ch; }while(ch=='y');
file.seekg(0);
file.read(reinterpret_cast<char*>(&pers),sizeof(pers));
pers.showdata();
file.read(reinterpret_cast<char*>(&pers),sizeof(pers));
pers.showdata();
file.close();
cout<<endl;
system("pause");
return 0;}
this is my code and pers.showdata(); is only showing the last data
entered how to do this properly...