Can anyone tell me what I did wrong in this code? When I enter data in the object of car class, it enters correctly, but it is not getting stored in the "CARS.dat" file.
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
class Car{
public:
string Model_NO, manufacturer, car_no;
Car(){ manufacturer="DEY MOTORS"; }
void Disp(){
cout<<"Model_Number:-"<<Model_NO<<endl;
cout<<"Car Number:-"<<car_no<<endl;
cout<<"Manufacturer:-"<<manufacturer<<endl;
}
};
Car Enter(){
Car c1;
cout<<"\nEnter Model_Number:-"; cin>>c1.Model_NO;
cout<<"Enter Car Number:-"; cin>>c1.car_no; return c1;
}
int main(){
Car *c; int n; char d;
fstream file;
cout<<"What do you want:-"<<endl<<"1. Enter"<<endl<<"2. Show"<<endl;
d=getch();
if(d=='1'){
cout<<"Enter Number:-"; cin>>n;
for(int i=0;i<n;i++){
c=new Car(); (*c)=Enter(); c->Disp();
file.open("CARS.dat",ios::app|ios::binary);
file.write((char*)c , sizeof(*c)); delete c;
}
}else if(d=='2'){
file.open("CARS.dat",ios::in|ios::binary);
while(!file.eof()){
c=new Car();
file.read((char*)c , sizeof(*c));
c->Disp(); delete c;
}
}
return 0;
}