I'm having a problem with retrieving records from my file just started programming over a month a go please help this is for my class project.
I need to do delete,update,query,and add to binary file and prompt the user to enter which record they need to retrieve and i started adding.please if you could give me an example of each i would be very gratefully.my project needs to be submmitted by 28/7/08.
here is my code:
#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<windows.h>
#include<conio.h>
using namespace std;
class houses
{
int house_number;
int number_of_rooms;
string furnished;
double cost;
public:
/* houses()
{
house_number = 0;
number_of_rooms = 0;
furnished = "";
cost = 0;
occupied = "";
}*/
void get_house()
{
cout<<"Please enter the house number(integer data only): ";
cin>>house_number;
cout<<"Please enter the number of rooms(integer data only): ";
cin>>number_of_rooms;
cout<<"Please enter the cost of the house: ";
cin>>cost;
cout<<"Please indicate if the house is furnished(yes,no): ";
cin>>furnished;
}
void show_house()
{
cout<<"house number: "<<house_number<<endl;
cout<<"number of rooms: "<<number_of_rooms<<endl;
cout<<"cost of the house: "<<cost<<endl;
cout<<"Is house the furnished: "<<furnished<<endl;
}
void housewrite()
{
houses h;
ofstream out("house.bin",ios::app);
if(out == NULL)
{
for(int i = 0; i < 15;i++)
{
cout<<"..";
Sleep(200);
}
cout<<"......FILE COULD NOT BE OPENED";
}
else
{
get_house();
out.write((char *)&h,sizeof(h));
out.close();
for(int i = 0; i < 15;i++)
{
cout<<"..";
Sleep(200);
}
cout<<".....FILE WRITTEN SUCESSFULLY";
}
}
void display_house()
{
int house_temp ;
houses h;
ifstream in("house.bin",ios::in|ios::binary);
if(in == NULL)
{
for(int i = 0; i < 15; i++)
{
cout<<"..";
Sleep(200);
}
cout<<"..........FILE COULD NOT BE OPENED";
}
else
{
do
{
cout<<"ENTER HOUSE NUMBER: ";
cin>>house_temp;
in.seekg(house_temp*sizeof(h),ios::beg);
in.read((char *)&h,sizeof(h));
if(house_temp == h.house_number)
{
h.show_house();
break;
}
else
{
cout<<"HOUSE NUMBER DOES NOT EXIST.......";
break;
}
}while(!in.eof());
}in.close();
}
};
int main()
{
houses obj;
obj.housewrite();
getch();
system("cls");
obj.display_house();
return 0;
}