I am currently learning to program in c++. I am making my way through a programming project I found online, and try and recreate it line by line looking up why certain things work the way they do. The project is a simple hotel booking system that has a menu system and saves users input i.e name, address, phone number etc.
I have been looking about trying understand what certain parts of this code do. I want to take a users input and save it to a .dat file, however it doesnt seem to work and im not sure why. Is there a better way to read and write to a text file.
This is the function that deals with checking if a room is free or reserved:
#include <fstream>
#include "Hotel.h"
int Hotel::check_availabilty(int room_type){
int flag = 0;
std::ifstream room_check("Room_Bookings.dat",std::ios::in);
while(!room_check.eof()){
room_check.read((char*)this, sizeof(Hotel));
//if room is already taken
if(room_no == room_type){
flag = 1;
break;
}
}
room_check.close();//close the ifstream
return(flag);//return result
}
This is the code that books a room:
#include "Hotel.h"
#include "check_availability.cpp"
void Hotel::book_a_room()
{
system("CLS");//this clears the screen
int flag;
int room_type;
std::ofstream room_Booking("Room_Bookings.dat");
std::cout << "\t\t" << "***********************" << "\n";
std::cout << "\t\t " << "THE GREATEST HOTEL" << "\n";
std::cout << "\t\t" << "***********************" << "\n";
std::cout << "\t\t " <<"Type of Rooms "<< "\t\t Room Number" "\n";
std::cout << "\t\t" << " Standard" << "\t\t 1 - 30" "\n";
std::cout << "\t\t" << " Luxury" << "\t\t\t 31 - 45" "\n";
std::cout << "\t\t" << " Royal" << "\t\t\t 46 - 50" "\n";
std::cout << "Please enter room number: ";
std::cin >> room_type;
flag = check_availabilty(room_type);
if(flag){
std::cout << "\n Sorry, that room isn't available";
}
else{
room_no = room_type;
std::cout<<" Name: ";
std::cin>>name;
std::cout<<" Address: ";
std::cin>>address;
std::cout<<" Phone No: ";
std::cin>>phone;
room_Booking.write((char*)this,sizeof(Hotel));
std::cout << "Your room is booked!\n";
}
std::cout << "Press any key to continue...";
getch();
room_Booking.close();
}
And this is the Hotel.h file
class Hotel
{
int room_no;
char name[30];
char address[50];
char phone[10];
public:
void main_menu();
void book_a_room();
int check_availabilty(int);
void display_details();
};
I dont fully understand what this part of the while loop does:
room_check.read((char*)this, sizeof(Hotel));
If you need any more info, please ask. Any hints and tips towards making this better would be welcomed.