Professional Documents
Culture Documents
Microproject Code
Microproject Code
Microproject Code
#include <iostream>
using namespace std;
struct Room {
int roomNumber;
bool booked;
string guestName;
};
class Hotel {
private:
Room rooms[MAX_ROOMS];
public:
Hotel() {
for(int i = 0; i < MAX_ROOMS; i++) {
rooms[i].roomNumber = i + 1;
rooms[i].booked = false;
rooms[i].guestName = "";
}
}
void displayRooms() {
cout << "Room Number\tBooked\tGuest Name\n";
for(int i = 0; i < MAX_ROOMS; i++) {
cout << rooms[i].roomNumber << "\t\t" << (rooms[i].booked ? "Yes" :
"No") << "\t" << rooms[i].guestName << "\n";
}
}
if(rooms[roomNumber - 1].booked) {
cout << "Room " << roomNumber << " is already booked.\n";
} else {
rooms[roomNumber - 1].booked = true;
rooms[roomNumber - 1].guestName = guestName;
cout << "Room " << roomNumber << " booked for " << guestName << ".\n";
}
}
};
int main() {
Hotel hotel;
int choice;
do {
cout << "\nHotel Booking System\n";
cout << "1. Display Rooms\n";
cout << "2. Book Room\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch(choice) {
case 1:
hotel.displayRooms();
break;
case 2: {
int roomNumber;
string guestName;
cout << "Enter room number: ";
cin >> roomNumber;
cin.ignore(); // Clear the input buffer
cout << "Enter guest name: ";
getline(cin, guestName); // Read the entire line, including spaces
hotel.bookRoom(roomNumber, guestName);
break;
}
case 3:
cout << "Exiting program.\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while(choice != 3);
return 0;
}
Output:-
Explanation:-
3. **Hotel Class**
- `class Hotel { ... };`: This is a class definition for the Hotel. It contains private
data members and public member functions.
4. **Main Function**
- `int main() { ... }`: The main function is where the program starts its execution.
- **Menu-Driven Interface**:
- The program presents a menu to the user using a `do-while` loop, where the
user can choose from three options:
1. Display Rooms
2. Book Room
3. Exit
- Depending on the user's choice, the program calls the corresponding member
function of the `Hotel` class.
- For booking a room, it prompts the user for a room number and guest name.
It uses `cin.ignore()` to clear the input buffer and `getline()` to read the entire line
for the guest's name, including spaces.
6. **Return Statement**
- `return 0;`: This indicates that the program has terminated successfully.