Project Overview
This project models a small hotel with a fixed number of rooms. Each room is a struct holding its number, type, price, booking status, and guest name. The whole hotel is just an array of these structs โ everything else is functions that read or modify that array.
- Display All Rooms โ prints a formatted table of every room's status
- Book a Room โ assigns a guest and number of nights to a free room
- Check-out โ calculates the final bill (with GST) and frees the room
- Search Room โ looks up a single room by number
- Save to File โ writes all room records to a text file for persistence
| Concept used | Where |
|---|---|
| struct | Room record: number, type, price, status, guest, nights |
| Arrays | Room hotel[TOTAL_ROOMS] holds every room |
| Functions | One function per menu operation โ keeps main() clean |
| switch-case | Routes the user's menu choice to the right function |
| File I/O | fopen/fprintf to persist room data |
| do-while | Keeps showing the menu until the user chooses Exit |
Room Struct & Hotel Initialization
Every room is described by one Room struct. The whole hotel is a fixed-size array โ TOTAL_ROOMS makes it easy to resize later. initHotel() pre-loads 5 rooms with realistic types and prices so the program has data to work with immediately.
#include <stdio.h> #include <string.h> #include <stdlib.h> #define TOTAL_ROOMS 5 #define GST 0.12 typedef struct { int roomNo; char type[20]; float pricePerNight; int isBooked; char guestName[50]; int nights; } Room; Room hotel[TOTAL_ROOMS]; // the entire hotel lives in this array void initHotel() { char *types[] = {"Single", "Double", "Deluxe", "Suite", "Deluxe"}; float prices[] = {1200, 2000, 3500, 6000, 3500}; for (int i = 0; i < TOTAL_ROOMS; i++) { hotel[i].roomNo = 101 + i; strcpy(hotel[i].type, types[i]); hotel[i].pricePerNight = prices[i]; hotel[i].isBooked = 0; strcpy(hotel[i].guestName, "-"); hotel[i].nights = 0; } }
Room instead of struct Room everywhere else in the file โ purely a readability convenience.Display, Search & Booking
findRoomIndex() is the one helper everything else depends on โ it turns a room *number* the user types into an *array index* we can actually use. Both booking and searching call it first before touching any data.
void displayRooms() { printf("\n%-8s %-10s %-12s %-10s %-15s\n", "Room", "Type", "Price/Night", "Status", "Guest"); for (int i = 0; i < TOTAL_ROOMS; i++) { printf("%-8d %-10s %-12.2f %-10s %-15s\n", hotel[i].roomNo, hotel[i].type, hotel[i].pricePerNight, hotel[i].isBooked ? "Booked" : "Free", hotel[i].guestName); } } int findRoomIndex(int roomNo) { for (int i = 0; i < TOTAL_ROOMS; i++) if (hotel[i].roomNo == roomNo) return i; return -1; // not found } void bookRoom() { int roomNo, nights; char name[50]; printf("Enter room number to book: "); scanf("%d", &roomNo); int idx = findRoomIndex(roomNo); if (idx == -1) { printf("Room not found.\n"); return; } if (hotel[idx].isBooked) { printf("Room already booked.\n"); return; } printf("Enter guest name: "); scanf(" %[^\n]", name); // reads a full name with spaces printf("Enter number of nights: "); scanf("%d", &nights); hotel[idx].isBooked = 1; strcpy(hotel[idx].guestName, name); hotel[idx].nights = nights; printf("Room %d booked for %s (%d nights).\n", roomNo, name, nights); } void searchRoom() { int roomNo; printf("Enter room number to search: "); scanf("%d", &roomNo); int idx = findRoomIndex(roomNo); if (idx == -1) { printf("Room not found.\n"); return; } printf("Room %d | %s | Rs %.2f/night | %s | Guest: %s\n", hotel[idx].roomNo, hotel[idx].type, hotel[idx].pricePerNight, hotel[idx].isBooked ? "Booked" : "Free", hotel[idx].guestName); }
%s would cut off at the first space.Checkout Billing & File Saving
Checkout is where the project earns its keep: it computes nights ร price, adds GST, prints an itemized bill, then resets the room to free. saveToFile() writes every room as a comma-separated line โ simple, human-readable, and easy to re-load later.
void checkoutRoom() { int roomNo; printf("Enter room number to checkout: "); scanf("%d", &roomNo); int idx = findRoomIndex(roomNo); if (idx == -1) { printf("Room not found.\n"); return; } if (!hotel[idx].isBooked) { printf("Room already free.\n"); return; } float subtotal = hotel[idx].pricePerNight * hotel[idx].nights; float tax = subtotal * GST; float total = subtotal + tax; printf("\n----- Final Bill -----\n"); printf("Guest : %s\n", hotel[idx].guestName); printf("Room : %d (%s)\n", hotel[idx].roomNo, hotel[idx].type); printf("Nights: %d x Rs %.2f = Rs %.2f\n", hotel[idx].nights, hotel[idx].pricePerNight, subtotal); printf("GST(12%%): Rs %.2f\n", tax); printf("Total : Rs %.2f\n", total); // free the room for the next guest hotel[idx].isBooked = 0; strcpy(hotel[idx].guestName, "-"); hotel[idx].nights = 0; } void saveToFile() { FILE *fp = fopen("hotel_data.txt", "w"); if (!fp) { printf("Error saving file.\n"); return; } for (int i = 0; i < TOTAL_ROOMS; i++) { fprintf(fp, "%d,%s,%.2f,%d,%s,%d\n", hotel[i].roomNo, hotel[i].type, hotel[i].pricePerNight, hotel[i].isBooked, hotel[i].guestName, hotel[i].nights); } fclose(fp); printf("Data saved to hotel_data.txt\n"); }
isBooked = 0 and clearing the guest name/nights means the same array slot can be reused for the next guest โ no need to add or remove array elements.The Main Menu
The main() function stays tiny on purpose โ it just loops, shows the menu, and routes the choice to the right function. A do-while loop guarantees the menu shows at least once, and repeats until the user picks Exit.
int main() { initHotel(); int choice; do { printf("\n===== HOTEL MANAGEMENT SYSTEM =====\n"); printf("1. Display All Rooms\n"); printf("2. Book a Room\n"); printf("3. Check-out & Generate Bill\n"); printf("4. Search Room\n"); printf("5. Save Data to File\n"); printf("6. Exit\n"); printf("Enter choice: "); scanf("%d", &choice); switch (choice) { case 1: displayRooms(); break; case 2: bookRoom(); break; case 3: checkoutRoom(); break; case 4: searchRoom(); break; case 5: saveToFile(); break; case 6: printf("Exiting... Thank you!\n"); break; default: printf("Invalid choice.\n"); } } while (choice != 6); return 0; }
===== HOTEL MANAGEMENT SYSTEM ===== 1. Display All Rooms 2. Book a Room 3. Check-out & Generate Bill 4. Search Room 5. Save Data to File 6. Exit Enter choice: 1 Room Type Price/Night Status Guest 101 Single 1200.00 Free - 102 Double 2000.00 Free - 103 Deluxe 3500.00 Free - 104 Suite 6000.00 Free - 105 Deluxe 3500.00 Free - Enter choice: 2 Enter room number to book: 103 Enter guest name: Ravi Kumar Enter number of nights: 3 Room 103 booked for Ravi Kumar (3 nights). Enter choice: 3 Enter room number to checkout: 103 ----- Final Bill ----- Guest : Ravi Kumar Room : 103 (Deluxe) Nights: 3 x Rs 3500.00 = Rs 10500.00 GST(12%): Rs 1260.00 Total : Rs 11760.00 Enter choice: 6 Exiting... Thank you!
Ways to Extend This Project
This version keeps things simple to focus on the core logic. Once it's working, these are natural next steps โ each one maps to a concept from earlier lessons:
- Load from file on startup โ read
hotel_data.txtwithfscanfso data survives between runs - Dynamic room count โ replace the fixed array with
malloc-allocated memory sized from user input - Linked list of bookings โ track booking *history* per room, not just the current guest
- Search by guest name โ linear search through the array matching
strcmpon guest name - Sort rooms by price โ plug in any sorting algorithm from the Sorting lesson
- Admin login โ add a password check before allowing booking/checkout
| Extension | Concept it reuses |
|---|---|
| Persistent load/save | File I/O lesson |
| Dynamic rooms | Dynamic memory (malloc/realloc) lesson |
| Booking history | Linked Lists lesson |
| Sort by price | Sorting Algorithms lesson |
Project Checklist
- I understand the Room struct and what each field stores
- I can explain how findRoomIndex() connects room number to array index
- I can trace bookRoom() end to end
- I understand the GST billing calculation in checkoutRoom()
- I know why the room is reset after checkout
- I understand how saveToFile() writes each room as a CSV line
- I can trace the do-while + switch main menu loop
- I compiled and ran the full program myself
- I picked at least one extension to try next