Ok, thanx for tha previous help but i have a problem with viewing something after i save it to a file, i cant seem to get the syntax for it. Say I have the function for adding data to a file like the one am gonna post below how would i access that file from a different function to view specific records.
Code:
void HomeIns:: Add(ofstream &PolicyFile)
{
//ofstream PolicyFile;
PolicyFile.open("Policy.txt",ios::out | ios::app| ios::binary );
HomeIns *nHome;
Date XDate, EDate;
int day, month, year;
int xday, xmonth, xyear;
float home_val;
string fladdr;
string sladdr;
string Roof_Type;
int hnum;
string person;
float premium_cost;
cout<<"Enter The Name Of The Policy Holder: ";
cin>>person;
cout<<"\n\nPlease Enter The Value Of The House: ";
cin>>home_val;
cout<<"\n\nPlease Enter The First Line Of Address For The House: ";
cin>>fladdr;
cout<<"\n\nPlease Enter The Second Line Of Address For The House: ";
cin>>sladdr;
cout<<"\n\nPlease Enter The Roof Type of The House: ";
cin>>Roof_Type;
cout<<"\n\nEnter The Day The Policy begins: ";
cin>>day;
cout<<"\n\nEnter The Month The Policy begins: ";
cin>>month;
cout<<"\n\nEnter The Day The Year begins: ";
cin>>year;
cout<<"\n\nEnter The Day The Policy finishes: ";
cin>>xday;
cout<<"\n\nEnter The Month The Policy finishes: ";
cin>>xmonth;
cout<<"\n\nEnter The Year The Policy finishes: ";
cin>>xyear;
system("CLS");
Home home(home_val, fladdr, sladdr, Roof_Type);
setEffDate(day, month, year);
setExpDate(xday, xmonth, xyear);
EDate=getEffDate();
XDate= getExpDate();
hnum=policynumber();
premium_cost= Premium(home_val);
nHome= new HomeIns(home, premium_cost,EDate, XDate, person, hnum );
if
(!PolicyFile){
cout<<"Error Creating FIle!!!\nSORRY!!!";
return;
}
PolicyFile<<"\n";
PolicyFile.write(reinterpret_cast<char*>(&nHome),sizeof(HomeIns));
PolicyFile<<"\n";
PolicyFile.close();
//nHome= new HomeIns[sizeof(HomeIns)];
//nHome= &HomeIns(home, premium_cost,EDate, XDate, person );
}
the coding above works like a charm. this is what i have for the viewing function say for instance i would like to view the third record what would the syntax be?
Code:
void HomeIns:: View(ifstream &Policyfile){
HomeIns *vHome;
int number;
Policyfile.open("Policy.txt", ios::in | ios::binary);
if(!Policyfile){
cout<<"Error Creating File!!!\nSORRY!!!";
return;
}
cout<<"Enter The Policy Number Of For The Home You Would Like To View: ";
cin>>number;
vHome->pnum=number;
while(!Policyfile.eof() && vHome->pnum>0){
Policyfile.seekg( (vHome->pnum-1)* sizeof(HomeIns));
Policyfile.read(reinterpret_cast<char *>(&vHome), sizeof(HomeIns));
if(vHome->pnum!=0){
cout<<vHome->home.getFAddress();
}
else {
cout<<"Enter Another Record Number";
cin>>vHome->pnum;
}
}
Policyfile.close();
}
what i get after running the program and i try to view i get an exception handling error or something like that.
I KNow programming can be a dick sometimes.