fstream help

I got one version of C++ program that hotel booking or check in whatever you call. see the program below.
Now I want to modify this program with "Struct" and array for room number and customer name. Any other function should be working as original program above.
I post one more function for this program separately.please see
Please help me out

int main() {

HOTEL hotel;
char choice =' ';

initialize(hotel);
menu_Diaplay();

while(choice !='q' && choice !='Q')
{
get_Menu_input(choice,hotel);
}

return 0;
}

char return_first_char(string choose)
{
char first_Char;
first_Char=choose[0];
return first_Char;
}

void sorting_Customer(string sort[])
{
string buffer_sort[NUMBER_OF_ROOMS];
int room[NUMBER_OF_ROOMS];
int room_temp;
string temp;
initialize(sort);
for(int i=0; i<NUMBER_OF_ROOMS;i++)
{

buffer_sort[i]=sort[i];
room[i] = i+1;
//cout<<buffer_sort[i]<<endl;
}

for(int i=0; i< (NUMBER_OF_ROOMS-1); i++)
{
for(int j = (i+1); j < NUMBER_OF_ROOMS; j++)
{
if(buffer_sort[i]>buffer_sort[j])
{
temp= buffer_sort[i];
room_temp = room[i];
buffer_sort[i] = buffer_sort[j];
room[i] = room[j];
buffer_sort[j] = temp;
room[j] = room_temp;
}
}
}


cout<<"\t== SORTED CUSTOMER BY NAME =="<<endl;

cout<<"\n";
cout<<setw(16)<<"Room No."<<setw(29)<<"Customer's Name"<<endl;


for(int t=0;t<NUMBER_OF_ROOMS;t++)
{
cout<<setw(9)<<"|";
cout<<setw(3)<<room[t];
cout<<setw(4)<<"|";
cout<<setw(15)<<"|";
cout<<setw(10)<<buffer_sort[t];
cout<<setw(4)<<"|"<<endl;

}
to_Menu();
}

void background_Save(string sav[])
{
ofstream datain;
datain.open("Hotel.txt");
for (int i=0;i<NUMBER_OF_ROOMS;i++)
{
datain<<sav[i]<<endl;
}
datain.close();

cout<<"\t== Data has been saved to file =="<<endl;

cout<<"\t\n"<<endl;
}

void initialize(HOTEL h)
{
fstream dataout;
dataout.open("Hotel.txt");
if (dataout.is_open())
{
for (int i=0;i<=NUMBER_OF_ROOMS;i++)
{
dataout >> h.room_Num[i];
getline(dataout,h.customer_Name[i]);
}
}
else
cout<<"can't open file";
dataout.close();

}

void to_Menu()
{
string menu;
char firstChar=' ';
cout<<"\n";
while(firstChar != 'm'){
cout<<"\tPress 'm' to main menu ";
cin>>menu;
firstChar=return_first_char(menu);
firstChar=tolower(firstChar);
}

system("cls");
menu_Diaplay();
}

void search_Customer(string ser[])
{
string finding="";
int i=0;
bool found=false;
bool confirm=false;
char y_n=' ';
char menu=' ';

cout<<"\t== SEARCH CUSTOMER =="<<endl;

cout<<"\n\nPlease enter customer name you which to search: ";
cin>>finding;
//for(int i=1;i<=NUMBER_OF_ROOMS;i++)
while (found==false)
{
if (ser[i]==finding)
{
cout<<"\n\t"<<ser[i]<<" staying in room no. "<<i+1<<"\n\n";
found=true;
}
else
i++;
if (i>NUMBER_OF_ROOMS)
{
cout<<"\n\tCan't fine the customer!! "<<endl;
cout<<"\n\tDo you want to serach again? (y/n)";
cin>>y_n;
if (y_n=='y'){
i=0;
cout<<"\n\nPlease enter customer name you which to search: ";
cin>>finding;
}
else if (y_n=='n')
found= true;
}
}
to_Menu();
}

void makeBookings(string hotel[])
{
int roomNumber = 0;
string occupant = " ";
int check=0;
int roomIndex=0;
bool loop_end=true;


cout<<"\t== BOOK ROOM FOR CUSTOMER =="<<endl;

cout<<"\n";
cout<<setw(16)<<"Room No."<<setw(29)<<"Customer's Name"<<endl;


for (int i = 0; i < NUMBER_OF_ROOMS; i++)
{
if (hotel[i] == EMPTY_ROOM)
{
cout<<setw(9)<<"|";
cout<<setw(3)<<i+1;
cout<<setw(4)<<"|";
cout<<setw(15)<<"|";
cout<<setw(10)<<EMPTY_ROOM;
cout<<setw(4)<<"|"<<endl;

check++;
}
}

if (check==0){
cout<<"\n\tSORRY! ALL THE ROOM ARE FULL"<<endl;
cout<<"\tCANNOT BE BOOK"<<endl;
}else{
cout << "\tPlease follow the instruction below "<<endl;
do{
cout << "\n\n\tPlease enter a room number: ";
cin >> roomNumber;
int roomIndex=roomNumber - 1; //convert real room to an index
if (roomNumber>NUMBER_OF_ROOMS || roomNumber<1)
{
cout<<"\n\tInvalid room number!!\n"<<endl;
}else if (hotel[roomIndex] != EMPTY_ROOM){
cout << "\n\tSorry this room is occupied by "<<hotel[roomIndex]<<endl;
cout << "\tPlease chose another room"<<endl;
}
else{
cout << "\n\tEnter the name of the main occupant: ";
//getline(cin,occupant);
cin>>occupant;
hotel[roomIndex] = occupant;
background_Save(hotel);
loop_end = false;
}
}while (loop_end == true);
}
to_Menu();
}

void displayHotel(string h[])
{
int check=0;

cout<<"\t== HOTEL EMPTY ROOMS =="<<endl;

cout<<"\n";
cout<<setw(16)<<"Room No."<<setw(29)<<"Customer's Name"<<endl;
for (int i = 0; i < NUMBER_OF_ROOMS; i++)
{
if (h[i] == EMPTY_ROOM)
{
cout<<setw(9)<<"|";
cout<<setw(3)<<i+1;
cout<<setw(4)<<"|";
cout<<setw(15)<<"|";
cout<<setw(10)<<EMPTY_ROOM;
cout<<setw(4)<<"|"<<endl;
check++;
}

}
if(check==0)
cout<<"\n\tSORRY !! THIS HOTEL IS FULL AT THE MOMENT "<<endl;
to_Menu();
}

void display_All_room(string r[])
{

cout<<"\t== HOTEL ROOMS =="<<endl;
int in=1;
cout<<"\n";
cout<<setw(16)<<"Room No."<<setw(29)<<"Customer's Name"<<endl;
for(int i=0;i<NUMBER_OF_ROOMS;i++)
{
cout<<setw(9)<<"|";
cout<<setw(3)<<in++;
cout<<setw(4)<<"|";
cout<<setw(15)<<"|";
cout<<setw(10)<<r[i];
cout<<setw(4)<<"|"<<endl;
}
to_Menu();

}

void delete_Customer(string h[])
{
int room_num = 0;
char y_n= ' ';
string Y_N=" ";
bool pass_Room=false;


cout<<"\t== Customer Leaving from room =="<<endl;
cout<<"\n";
cout<<setw(16)<<"Room No."<<setw(29)<<"Customer's Name"<<endl;
for(int i=0;i<NUMBER_OF_ROOMS;i++)
{
cout<<setw(9)<<"|";
cout<<setw(3)<<i+1;
cout<<setw(4)<<"|";
cout<<setw(15)<<"|";
cout<<setw(10)<<h[i];
cout<<setw(4)<<"|"<<endl;
}
while(pass_Room==false)
{
cout<<"\n\tPlease select the room number: ";
cin>> room_num;
if (room_num>NUMBER_OF_ROOMS || room_num<1)
cout<<"\n\t!! Invalid room number"<<endl<<"\n\tPlease select the room number again "<<endl;
else pass_Room= true ;
}

cout<<"\tAre you sure to delete customer from the room!!"<<endl;
while(y_n!='N')
{
cout<< "\tY/N? ";
cin>>Y_N;
y_n=return_first_char(Y_N);
y_n=toupper(y_n);
switch(y_n){
case 'Y':
room_num = room_num - 1;
if (h[room_num]=="empty")
cout<<"\tNo one in the room no. "<<room_num+1<<"\n\n"<<endl;
else
{
h[room_num]="empty";
cout<<"\tRoom no. "<<room_num+1<<" is empty now\n\n"<<endl;
}
background_Save(h);
y_n='N';
break;

case 'N':
y_n='N';
break;

default: cout<<"\n\tInvalid choice! ";
}
}
to_Menu();
}
void menu_Diaplay()
{
cout<<"\n\n";
cout<<"\t== Main Menu =="<<endl;
cout<<"\n\n";
cout<<"\tPress A:\tAdd Customer to room"<<endl;
cout<<"\tPress V:\tView All Room"<<endl;
cout<<"\tPress E:\tDisplay Empty rooms"<<endl;
cout<<"\tPress D:\tDelete customer from room"<<endl;
cout<<"\tPress F:\tFind room from customer name"<<endl;
cout<<"\tPress S:\tSave program data in to file"<<endl;
cout<<"\tPress L:\tLoad program data from file"<<endl;
cout<<"\tPress O:\tView rooms Ordered alphabetically by name"<<endl;
cout<<"\tPress Q:\tTo Quit"<<endl;
cout<<""<<endl;

}

Last edited on
continued function

void get_Menu_input(char &choose,string h[])
{
char c;
string first_Char;
cout<<"Select from the menu above ";
cin>>first_Char;
choose=return_first_char(first_Char);
c=tolower(choose);
system("cls");
switch (c){
case 'a':
makeBookings(h); //A: add customer to room
break;

case 'v':
display_All_room(h); //V: view all room
break;

case 'e':
displayHotel(h); //E: display empty room
break;

case 'd':
delete_Customer(h); //D: delete customer from room and make empty
break;

case 'f':
search_Customer(h); //F: Fine room from customer name
break;

case 'l':
initialize(h); //L: Load program data from file

cout<<"\t== Data has been loaded =="<<endl;

to_Menu();
break;

case 'o':
sorting_Customer(h); //O: View rooms Ordered alphabetically by name
break;

case 's':
background_Save(h); //S: Store program data in to file
to_Menu();
break;

case 'q':
cout<<"\t==Data has been saved to file & quit=="<<endl;

break;


default:
{
cout<<"\t\t!! Warning !! Wrong input!!"<<endl;
cout<<"\n\t\tPlease Check your input again!!\n\n"<<endl;
menu_Diaplay();}
}
}
Topic archived. No new replies allowed.