0% found this document useful (0 votes)
14 views21 pages

Detailed C Project

The document outlines a detailed C project for a Tour, Flight, and Hotel Booking System that includes file structures for storing booking data and user credentials. It provides a comprehensive code implementation with functions for adding, viewing, searching, editing, and deleting bookings, along with a user login system. Key components include file handling for data persistence, use of arrays and structures for managing bookings, and user interaction through a console menu.

Uploaded by

safiulbasar51
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views21 pages

Detailed C Project

The document outlines a detailed C project for a Tour, Flight, and Hotel Booking System that includes file structures for storing booking data and user credentials. It provides a comprehensive code implementation with functions for adding, viewing, searching, editing, and deleting bookings, along with a user login system. Key components include file handling for data persistence, use of arrays and structures for managing bookings, and user interaction through a console menu.

Uploaded by

safiulbasar51
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Detailed C Project: Tour, Flight, and Hotel Booking System

1. File Structure and Persistent Data:

We'll store the data in files to allow persistence. Each booking type (Flight, Hotel, Tour
Guide) will have its own file. Additionally, we'll store user login credentials in a file.

 flights.txt - Stores flight booking data.

 hotels.txt - Stores hotel booking data.

 tourguides.txt - Stores tour guide booking data.

 users.txt - Stores user credentials (user ID and password).

2. Extended C Project Code:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_BOOKINGS 100


#define MAX_USERS 10

// Define structures for Flight, Hotel, and Tour Guide


typedef struct {
int id;
char destination[50];
float price;
char departure_time[20];
} Flight;

typedef struct {
int id;
char name[50];
float price;
char location[50];
} Hotel;

typedef struct {
int id;
char name[50];
int rating;
char availability[20];
} TourGuide;

typedef struct {
int user_id;
char password[20];
} User;

// Function Declarations
void menu();
void addFlight();
void addHotel();
void addTourGuide();
void viewBookings();
void searchBooking();
void editBooking();
void deleteBooking();
void loadData();
void saveData();
int login();
void addUser();

// Global arrays to store bookings and users


Flight flights[MAX_BOOKINGS];
Hotel hotels[MAX_BOOKINGS];
TourGuide tourGuides[MAX_BOOKINGS];
User users[MAX_USERS];

// Bookings counters and user count


int flightCount = 0, hotelCount = 0, tourGuideCount = 0, userCount = 0;
int loggedInUserId = -1;

int main() {
loadData(); // Load saved data from files
if (login()) {
menu(); // Display menu and interact with the user
}
return 0;
}

void menu() {
int choice;
while(1) {
printf("\n-- Booking System --\n");
printf("1. Add Flight\n");
printf("2. Add Hotel\n");
printf("3. Add Tour Guide\n");
printf("4. View Bookings\n");
printf("5. Search Booking\n");
printf("6. Edit Booking\n");
printf("7. Delete Booking\n");
printf("8. Logout\n");
printf("9. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch(choice) {
case 1: addFlight(); break;
case 2: addHotel(); break;
case 3: addTourGuide(); break;
case 4: viewBookings(); break;
case 5: searchBooking(); break;
case 6: editBooking(); break;
case 7: deleteBooking(); break;
case 8: loggedInUserId = -1; printf("Logged out successfully!\
n"); break;
case 9: saveData(); exit(0);
default: printf("Invalid choice! Please try again.\n");
}
}
}
// Function to load data from files
void loadData() {
FILE *file;

// Load users
file = fopen("users.txt", "r");
if (file != NULL) {
while (fscanf(file, "%d %s", &users[userCount].user_id,
users[userCount].password) != EOF) {
userCount++;
}
fclose(file);
}

// Load flights
file = fopen("flights.txt", "r");
if (file != NULL) {
while (fscanf(file, "%d %s %f %s", &flights[flightCount].id,
flights[flightCount].destination, &flights[flightCount].price,
flights[flightCount].departure_time) != EOF) {
flightCount++;
}
fclose(file);
}

// Load hotels
file = fopen("hotels.txt", "r");
if (file != NULL) {
while (fscanf(file, "%d %s %f %s", &hotels[hotelCount].id,
hotels[hotelCount].name, &hotels[hotelCount].price,
hotels[hotelCount].location) != EOF) {
hotelCount++;
}
fclose(file);
}

// Load tour guides


file = fopen("tourguides.txt", "r");
if (file != NULL) {
while (fscanf(file, "%d %s %d %s", &tourGuides[tourGuideCount].id,
tourGuides[tourGuideCount].name, &tourGuides[tourGuideCount].rating,
tourGuides[tourGuideCount].availability) != EOF) {
tourGuideCount++;
}
fclose(file);
}
}

// Function to save data to files


void saveData() {
FILE *file;

// Save users
file = fopen("users.txt", "w");
for (int i = 0; i < userCount; i++) {
fprintf(file, "%d %s\n", users[i].user_id, users[i].password);
}
fclose(file);

// Save flights
file = fopen("flights.txt", "w");
for (int i = 0; i < flightCount; i++) {
fprintf(file, "%d %s %.2f %s\n", flights[i].id,
flights[i].destination, flights[i].price, flights[i].departure_time);
}
fclose(file);

// Save hotels
file = fopen("hotels.txt", "w");
for (int i = 0; i < hotelCount; i++) {
fprintf(file, "%d %s %.2f %s\n", hotels[i].id, hotels[i].name,
hotels[i].price, hotels[i].location);
}
fclose(file);

// Save tour guides


file = fopen("tourguides.txt", "w");
for (int i = 0; i < tourGuideCount; i++) {
fprintf(file, "%d %s %d %s\n", tourGuides[i].id,
tourGuides[i].name, tourGuides[i].rating, tourGuides[i].availability);
}
fclose(file);
}

// Function to handle user login


int login() {
int userId;
char password[20];

printf("Enter your User ID: ");


scanf("%d", &userId);

printf("Enter your password: ");


scanf("%s", password);

// Check if user exists and password is correct


for (int i = 0; i < userCount; i++) {
if (users[i].user_id == userId && strcmp(users[i].password,
password) == 0) {
loggedInUserId = userId;
printf("Login successful!\n");
return 1;
}
}
printf("Invalid User ID or Password. Try again.\n");
return 0;
}

// Function to add a new user (for testing purposes)


void addUser() {
User newUser;
printf("Enter new User ID: ");
scanf("%d", &newUser.user_id);
printf("Enter new User password: ");
scanf("%s", newUser.password);

users[userCount++] = newUser;
printf("User added successfully!\n");
}

// Function to add a flight booking


void addFlight() {
if (loggedInUserId == -1) {
printf("You must be logged in to add a booking.\n");
return;
}

Flight newFlight;
printf("Enter flight details:\n");
printf("ID: ");
scanf("%d", &newFlight.id);
printf("Destination: ");
scanf("%s", newFlight.destination);
printf("Price: ");
scanf("%f", &newFlight.price);
printf("Departure time: ");
scanf("%s", newFlight.departure_time);
flights[flightCount++] = newFlight;
printf("Flight added successfully!\n");
}

// Function to add a hotel booking


void addHotel() {
if (loggedInUserId == -1) {
printf("You must be logged in to add a booking.\n");
return;
}

Hotel newHotel;
printf("Enter hotel details:\n");
printf("ID: ");
scanf("%d", &newHotel.id);
printf("Hotel Name: ");
scanf("%s", newHotel.name);
printf("Price: ");
scanf("%f", &newHotel.price);
printf("Location: ");
scanf("%s", newHotel.location);
hotels[hotelCount++] = newHotel;
printf("Hotel added successfully!\n");
}

// Function to add a tour guide booking


void addTourGuide() {
if (loggedInUserId == -1) {
printf("You must be logged in to add a booking.\n");
return;
}

TourGuide newGuide;
printf("Enter tour guide details:\n");
printf("ID: ");
scanf("%d", &newGuide.id);
printf("Name: ");
scanf("%s", newGuide.name);
printf("Rating: ");
scanf("%d", &newGuide.rating);
printf("Availability: ");
scanf("%s", newGuide.availability);
tourGuides[tourGuideCount++] = newGuide;
printf("Tour Guide added successfully!\n");
}
// Function to view all bookings
void viewBookings() {
printf("\n-- Flight Bookings --\n");
for (int i = 0; i < flightCount; i++) {
printf("ID: %d, Destination: %s, Price: %.2f, Departure Time: %s\
n",
flights[i].id, flights[i].destination, flights[i].price,
flights[i].departure_time);
}
printf("\n-- Hotel Bookings --\n");
for (int i = 0; i < hotelCount; i++) {
printf("ID: %d, Name: %s, Price: %.2f, Location: %s\n",
hotels[i].id, hotels[i].name, hotels[i].price,
hotels[i].location);
}
printf("\n-- Tour Guide Bookings --\n");
for (int i = 0; i < tourGuideCount; i++) {
printf("ID: %d, Name: %s, Rating: %d, Availability: %s\n",
tourGuides[i].id, tourGuides[i].name, tourGuides[i].rating,
tourGuides[i].availability);
}
}

// Function to search for a booking


void searchBooking() {
char searchKey[50];
printf("Enter search keyword: ");
scanf("%s", searchKey);

// Search Flights
for (int i = 0; i < flightCount; i++) {
if (strstr(flights[i].destination, searchKey)) {
printf("Found Flight - ID: %d, Destination: %s, Price: %.2f,
Departure Time: %s\n",
flights[i].id, flights[i].destination, flights[i].price,
flights[i].departure_time);
}
}
// Search Hotels
for (int i = 0; i < hotelCount; i++) {
if (strstr(hotels[i].name, searchKey)) {
printf("Found Hotel - ID: %d, Name: %s, Price: %.2f, Location:
%s\n",
hotels[i].id, hotels[i].name, hotels[i].price,
hotels[i].location);
}
}
// Search Tour Guides
for (int i = 0; i < tourGuideCount; i++) {
if (strstr(tourGuides[i].name, searchKey)) {
printf("Found Tour Guide - ID: %d, Name: %s, Rating: %d,
Availability: %s\n",
tourGuides[i].id, tourGuides[i].name,
tourGuides[i].rating, tourGuides[i].availability);
}
}
}

// Function to edit a booking


void editBooking() {
int id, choice;
printf("Enter the ID of the booking you want to edit: ");
scanf("%d", &id);

// Search for booking by ID and edit


for (int i = 0; i < flightCount; i++) {
if (flights[i].id == id) {
printf("Editing Flight ID: %d\n", id);
printf("1. Change Destination\n");
printf("2. Change Price\n");
printf("3. Change Departure Time\n");
printf("Choose option: ");
scanf("%d", &choice);
switch(choice) {
case 1: printf("New Destination: "); scanf("%s",
flights[i].destination); break;
case 2: printf("New Price: "); scanf("%f",
&flights[i].price); break;
case 3: printf("New Departure Time: "); scanf("%s",
flights[i].departure_time); break;
default: printf("Invalid choice!\n");
}
return;
}
}
// Similar code for Hotel and TourGuide...
}

// Function to delete a booking


void deleteBooking() {
int id;
printf("Enter the ID of the booking you want to delete: ");
scanf("%d", &id);

// Delete from Flight, Hotel, or Tour Guide


for (int i = 0; i < flightCount; i++) {
if (flights[i].id == id) {
// Shift array elements
for (int j = i; j < flightCount - 1; j++) {
flights[j] = flights[j + 1];
}
flightCount--;
printf("Flight with ID %d deleted successfully!\n", id);
return;
}
}
// Similar code for Hotel and TourGuide...
}

3. Detailed Explanation of Key Components:

1. File Handling:

 Files: All data (user credentials, flights, hotels, and tour guides) is saved in respective
text files (users.txt, flights.txt, hotels.txt, and tourguides.txt).

 loadData() reads the data from these files when the program starts. The data is loaded
into appropriate arrays (flights, hotels, tour guides, and users).
 saveData() writes the updated data back into these files when the program exits.

2. Login System:

 Users: Each user is identified by a user_id, and their password is stored in the
users.txt file.

 Login Function: The login() function checks the entered user_id and password
against the stored users' credentials. If a match is found, the user is logged in and
granted access to the booking system.

3. Arrays and Structures:

 Arrays of Structures: Arrays like flights[], hotels[], tourGuides[] are used to


store multiple records of each booking type (flight, hotel, tour guide).

 Structure Fields: Each structure holds data relevant to a specific booking type (e.g.,
destination, price, departure_time for flights).

4. Conditional Statements and Loops:

 If-Else: Used for validating user input, checking if a user is logged in before allowing
bookings, and handling errors.

 Switch-Case: Used in the menu to handle the user's choice and call the corresponding
function.

 Loops: Used for iterating through arrays to display all records, search for specific
records, and manage booking data.

5. Memory Management (Dynamic Arrays):

 For this implementation, we use static arrays to hold bookings, but in a real-world
scenario, dynamic memory management (using pointers) could be implemented to
handle varying numbers of bookings efficiently.

Conclusion:

This Tour, Flight, and Hotel Booking System project is designed to manage bookings for
flights, hotels, and tour guides. It implements features like user login, adding new bookings,
viewing, searching, editing, and deleting bookings. It uses file handling for data persistence,
arrays of structures

to store booking records, and simple user interaction through the console.
users.txt (Stores User Credentials)

This file contains the user information, including the user_id and password for login. For
example:

101 password123
102 securepass
103 mypassword

Here, 101, 102, and 103 are user IDs, and the corresponding passwords are password123,
securepass, and mypassword.

flights.txt (Stores Flight Booking Data)

This file contains the flight booking details, with each line representing a flight record. It
includes the id, destination, price, and departure_time for each flight.

1 Paris 500.00 2025-05-15 10:00


2 Tokyo 700.00 2025-06-10 14:30
3 NewYork 300.00 2025-07-01 12:00
4 London 400.00 2025-06-20 08:00

Here:

 id is the unique flight identifier.

 destination is the destination city or country.

 price is the cost of the flight.

 departure_time is the scheduled departure time of the flight.

hotels.txt (Stores Hotel Booking Data)

This file contains the hotel booking details. Each line represents a hotel, with id, name,
price, and location.

1 Hilton 200.00 Paris


2 Marriott 250.00 NewYork
3 Hyatt 150.00 Tokyo
4 Sheraton 180.00 London

Here:

 id is the unique hotel identifier.

 name is the name of the hotel.


 price is the price per night at the hotel.

 location is the city where the hotel is located.

tourguides.txt (Stores Tour Guide Booking Data)

This file contains information about the tour guides. Each line represents a tour guide, with
id, name, rating, and availability.

1 JohnDoe 5 Available
2 JaneSmith 4 Available
3 MikeJohnson 3 Not Available
4 SarahBrown 5 Available

Here:

 id is the unique tour guide identifier.

 name is the name of the tour guide.

 rating is the rating of the tour guide (1-5 stars).

 availability indicates whether the guide is available or not.

How to Create These Files:

1. Open a text editor (e.g., Notepad on Windows, TextEdit on macOS, or any code
editor).

2. Copy the contents of each file provided above and paste them into separate text files.

3. Save the files as follows:

o users.txt for the user credentials file.

o flights.txt for the flight bookings.

o hotels.txt for the hotel bookings.

o tourguides.txt for the tour guide information.

Loading and Saving Data:

 When you run the program, it will read from these files using fopen() and fscanf().
 Any changes made (such as adding, editing, or deleting bookings) will be saved back
to the respective files when the program exits using the saveData() function.

Final Notes:

 These files are essential for the program to function correctly and retain data even
after the program is closed and reopened.

 Make sure the files are located in the same directory as your C program, or adjust the
file paths accordingly if you're using directories.

Let me know if you need more assistance setting up or running the project!

Sure! I'll break down the concepts you're asking about—if-else conditions, switch-case
statements, loops, arrays, files, structures, functions, and pointers—as they relate to the
provided C code. After that, I'll give you a detailed viva preparation guide for this project.

Explanation of Concepts Used in the Code

1. If-Else Conditions

The if-else condition is used to check certain conditions before executing a block of code.
It is used throughout the code to control flow based on specific conditions.

 Login Validation:
 if (loggedInUserId == -1) {
 printf("You must be logged in to add a booking.\n");
 return;
 }
o This checks if a user is logged in before allowing them to add a booking. If
loggedInUserId == -1, it means no user is logged in, so the function exits
with a message.

 User Exists Validation:


 if (users[i].user_id == userId && strcmp(users[i].password, password)
== 0) {
 loggedInUserId = userId;
 printf("Login successful!\n");
 return 1;
 }
o Here, if-else is used to validate if the user exists in the system by checking
the provided userId and password. If both match, the login is successful,
otherwise, it returns a failure message.

2. Switch-Case Statements
A switch-case is used for selecting among many options, making the code cleaner and more
manageable compared to multiple if-else statements.

 Menu Selection:
 switch(choice) {
 case 1: addFlight(); break;
 case 2: addHotel(); break;
 case 3: addTourGuide(); break;
 case 4: viewBookings(); break;
 case 5: searchBooking(); break;
 case 6: editBooking(); break;
 case 7: deleteBooking(); break;
 case 8: loggedInUserId = -1; printf("Logged out successfully!\
n"); break;
 case 9: saveData(); exit(0);
 default: printf("Invalid choice! Please try again.\n");
 }
o This switch-case structure is used to select an action based on the user's choice
in the menu. The break statement ensures that only the selected case is
executed. If no valid option is chosen, the default case handles it.

3. Loops (For Loops)

For loops are used to iterate through arrays or lists. This is essential for accessing or
processing multiple elements in the arrays, like users, flights, hotels, or tour guides.

 Loading Data from Files:


 for (int i = 0; i < userCount; i++) {
 if (users[i].user_id == userId && strcmp(users[i].password,
password) == 0) {
 loggedInUserId = userId;
 printf("Login successful!\n");
 return 1;
 }
 }
o This loop iterates through the users array to find a match for the provided
userId and password.

 Searching for Bookings:


 for (int i = 0; i < flightCount; i++) {
 if (strstr(flights[i].destination, searchKey)) {
 printf("Found Flight - ID: %d, Destination: %s, Price: %.2f,
Departure Time: %s\n",
 flights[i].id, flights[i].destination,
flights[i].price, flights[i].departure_time);
 }
 }
o Here, the loop searches for a booking by matching a search keyword in the
destination of each flight.

4. Arrays

Arrays are used to store multiple elements of the same type. In this code, arrays are used to
store flight bookings, hotel bookings, tour guides, and users.
 Flight, Hotel, and Tour Guide Arrays:
 Flight flights[MAX_BOOKINGS];
 Hotel hotels[MAX_BOOKINGS];
 TourGuide tourGuides[MAX_BOOKINGS];
 User users[MAX_USERS];
o These arrays store multiple bookings of flights, hotels, tour guides, and users,
respectively. Each array can hold a fixed number of elements (like 100
bookings for flights or hotels).

5. Files (Reading/Writing)

Files are used to store data persistently so that when the program runs again, the data isn't
lost. The fopen, fscanf, and fprintf functions are used for file I/O.

 Loading Data:
 file = fopen("users.txt", "r");
 if (file != NULL) {
 while (fscanf(file, "%d %s", &users[userCount].user_id,
users[userCount].password) != EOF) {
 userCount++;
 }
 fclose(file);
 }
o Data is loaded from text files (e.g., users.txt, flights.txt) into arrays. The
program reads each line from the file using fscanf and fills the corresponding
structures with values.

 Saving Data:
 file = fopen("users.txt", "w");
 for (int i = 0; i < userCount; i++) {
 fprintf(file, "%d %s\n", users[i].user_id, users[i].password);
 }
 fclose(file);
o After processing, the program saves updated data (such as users, flights,
hotels) back to their respective text files using fprintf.

6. Structures

Structures are used to group different data types together. In this case, structures represent
flights, hotels, tour guides, and users.

 Flight Structure:
 typedef struct {
 int id;
 char destination[50];
 float price;
 char departure_time[20];
 } Flight;
o The Flight structure holds details of a flight booking, such as id,
destination, price, and departure_time.

 Hotel Structure:
 typedef struct {
 int id;
 char name[50];
 float price;
 char location[50];
 } Hotel;
o Similarly, the Hotel structure holds details of a hotel booking.

7. Functions

Functions are used to break down the program into smaller, manageable chunks. Each
function typically handles a specific task.

 Login Function:
 int login() {
 // handles user login logic
 }
o This function prompts the user for login credentials and checks if the user
exists in the system.

 Add Flight Function:


 void addFlight() {
 // adds a new flight to the system
 }
o This function handles adding a new flight booking to the system.

8. Pointers

Pointers are not explicitly used in this code, but they are implicitly involved when using
arrays (since arrays are passed by reference to functions). For example, the scanf function
modifies the value of a variable directly.

Viva Preparation Questions and Answers

Here’s a guide to prepare for the viva based on your project:

1. What is the purpose of the program?

The program is a booking system that allows users to manage flight, hotel, and tour guide
bookings. It supports functionalities like adding, viewing, editing, and deleting bookings. It
also allows users to log in and view their respective bookings.

2. How does the login system work?

The login system works by validating a user's ID and password. The system reads user
credentials from a file (users.txt) and checks if the input matches any existing user's
details. If the credentials are valid, the user is logged in.

3. Explain the structure of the flight, hotel, and tour guide.


Each structure (Flight, Hotel, and TourGuide) contains details relevant to that type of
booking. For example, a flight contains id, destination, price, and departure_time,
while a hotel contains id, name, price, and location.

4. Why are arrays used in this program?

Arrays are used to store collections of data, such as multiple bookings for flights, hotels, and
tour guides. Arrays provide an easy way to manage and access large sets of similar data.

5. How does the program handle data persistence?

The program uses file I/O functions (fopen, fscanf, fprintf) to load and save data from/to
text files. This ensures that the data is preserved between different runs of the program.

6. What happens if a user tries to add a booking without logging in?

If a user is not logged in, the system will print a message indicating that the user must log in
to add a booking and the function will return without allowing any changes.

7. How does the switch-case statement help in the program?

The switch-case statement is used to manage user choices in the main menu. It makes the
code cleaner and more efficient by handling different user options based on their input.

8. Can you explain how the viewBookings() function works?

The viewBookings() function loops through all the bookings (flights, hotels, and tour
guides) and prints out their details. It uses for loops to iterate over each booking array and
display information.

9. How do you ensure data consistency in the program?

Data consistency is ensured by saving the data to files whenever there are changes. The
program reads from the files during startup and writes back when exiting or making updates
to the bookings.

10. What improvements can be made to the program?

 Implementing better error handling (e.g., invalid input).

 Using dynamic memory allocation for arrays (instead of static sizes) for scalability.

 Implementing a more secure password storage method (e.g., hashing).

Absolutely! Let’s dive deeper into some more complicated questions regarding loops, files,
conditions, arrays, and structures. Then, I’ll give you a detailed breakdown of the entire code
with beginner-friendly explanations.
Complicated Questions About Loops, Files, Conditions, Arrays, and
Structures

1. Loops:

Question: In your code, you use a for loop to iterate through the arrays of flights, hotels,
and tour guides. Can you explain why you use for loops instead of other types of loops
(e.g., while or do-while)? How do you ensure the loop will stop when the array is full?

Answer:
The for loop is ideal in this case because we know exactly how many iterations we need to
make—based on the current count of items in the array (like flightCount, hotelCount,
etc.). It is a perfect match for cases where we know how many items we are dealing with, and
we can easily control the starting point, condition, and increment (or step) in a single line.

For example:

for (int i = 0; i < flightCount; i++) {


// Do something with flights[i]
}

Here, i is the index that goes from 0 to flightCount - 1. The condition i < flightCount
ensures that the loop runs only until we have processed all the bookings in the flights array.
If you were to use while or do-while loops, you'd need an explicit condition inside the loop
to stop it, which could complicate things.

2. Files:

Question: The program reads and writes data to files (users.txt, flights.txt, etc.). Can
you explain how the file operations are done, and what might go wrong if the file doesn’t
exist or isn’t accessible?

Answer:
The program opens files using fopen with a mode (r for read, w for write, etc.). For instance:

file = fopen("users.txt", "r");

This line tries to open the file users.txt in read mode. If the file exists, the program will
continue to read its contents using fscanf. If the file doesn’t exist or is not accessible (due to
permission issues), fopen returns NULL, and we handle it by checking the returned value:

if (file != NULL) {
// Process the file
} else {
printf("Error opening file\n");
}

If a file is opened in write mode ("w"), the existing file will be overwritten, and if it doesn't
exist, a new file will be created.
To ensure data integrity, files should always be closed using fclose(file) to flush changes
to disk and release resources.

3. Conditions:

Question: You have used multiple if and switch statements in your code. What is the
difference between using if-else and switch-case? Which one is more efficient for
checking multiple conditions?

Answer:

 if-else:This structure is flexible and can handle complex conditions. You can
combine multiple logical expressions (e.g., using && or ||), which makes it useful
when you have complex conditions that involve ranges or multiple variables.

 switch-case: A switch-case is generally more efficient when you have many


discrete options based on a single variable. It's often faster because the compiler can
optimize it into a jump table, allowing direct access to the corresponding case.
However, switch-case only works with a single expression that evaluates to an
integer or character, unlike if-else, which can handle a wider variety of conditions.

For example, the menu selection is more suited for a switch-case because it's checking
different possible menu choices:

switch(choice) {
case 1: addFlight(); break;
case 2: addHotel(); break;
...
}

This is cleaner and more efficient than using multiple if-else statements for each choice.

4. Arrays:

Question: You use arrays to store the bookings of flights, hotels, and tour guides. How do
you ensure that the program doesn’t access invalid elements in the array (e.g., out-of-bounds
access)? What would happen if the array is full?

Answer:
Arrays in C have a fixed size, and accessing an element outside of their bounds (e.g.,
accessing flights[101] in an array of size 100) is undefined behavior. To avoid this, you
use counters (like flightCount, hotelCount, etc.) to keep track of how many elements are
currently in the array.

For example:

flights[flightCount++] = newFlight;

This ensures that the array is only accessed up to the current count (flightCount),
preventing out-of-bounds errors. If the array is full (i.e., if flightCount >= MAX_BOOKINGS),
the program won’t add more flights unless you increase the array size or implement dynamic
memory allocation (using malloc).

Detailed Breakdown of the Entire Code for Beginners

Let’s now go through the entire code, explaining everything in the most beginner-friendly
way.

Header Files

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

 stdio.h allows us to use functions for input/output, like printf and scanf.

 stdlib.h includes functions like exit(), which helps us quit the program.

 string.h gives us string functions like strcmp() for comparing strings.

Macros

#define MAX_BOOKINGS 100


#define MAX_USERS 10

 #define is used to set constant values. Here, we define the maximum number of
bookings (100) and the maximum number of users (10).

Structures

Structures group related variables together under one name. They are used to represent
complex data.

 Flight: This structure holds information about a flight booking.

typedef struct {
int id;
char destination[50];
float price;
char departure_time[20];
} Flight;

 Hotel: Holds information about a hotel booking.

typedef struct {
int id;
char name[50];
float price;
char location[50];
} Hotel;
 TourGuide: Holds information about a tour guide.

typedef struct {
int id;
char name[50];
int rating;
char availability[20];
} TourGuide;

 User: Holds the user's login credentials.

typedef struct {
int user_id;
char password[20];
} User;

Global Variables

Flight flights[MAX_BOOKINGS];
Hotel hotels[MAX_BOOKINGS];
TourGuide tourGuides[MAX_BOOKINGS];
User users[MAX_USERS];

 These are arrays that will hold the flight, hotel, tour guide, and user data. The
MAX_BOOKINGS and MAX_USERS limits how many entries we can have.

Function Declarations

Functions are declared at the top to indicate their prototypes, so the compiler knows what
functions exist before they are used in the code.

Main Function

int main() {
loadData(); // Load saved data from files
if (login()) {
menu(); // Display menu if login is successful
}
return 0;
}

1. loadData() loads data from files into the arrays at the beginning.

2. login() asks the user for credentials and checks if they are correct.

3. If the login is successful, menu() is called to display options to the user.

Menu Function

The menu() function handles the main interface of the program, where the user can choose
what to do (add a flight, add a hotel, etc.).

void menu() {
int choice;
while(1) {
printf("\n-- Booking System --\n");
// Display choices
scanf("%d", &choice);

switch(choice) {
case 1: addFlight(); break;
case 2: addHotel(); break;
case 3: addTourGuide(); break;
case 4: viewBookings(); break;
case 5: searchBooking(); break;
case 6: editBooking(); break;
case 7: deleteBooking(); break;
case 8: loggedInUserId = -1; printf("Logged out successfully!\
n"); break;
case 9: saveData(); exit(0);
default: printf("Invalid choice! Please try again.\n");
}
}
}

The program displays options and uses a switch-case to decide which function to call based
on the user's input.

File Operations

The loadData() and saveData() functions are used to read from and write to files.

 loadData() reads data from users.txt, flights.txt, hotels.txt, and


tourguides.txt into the corresponding arrays.

 saveData() writes the current data back into those files so that changes are preserved
for the next time the program is run.

Booking Functions

Functions like addFlight(), addHotel(), and addTourGuide() allow the user to add new
bookings to the system. Each function checks if the user is logged in before proceeding.

Login Function

The login() function asks for a user ID and password, then checks if the entered credentials
match any existing user. If they do, the user is logged in and the system proceeds to show the
menu.

Summary

The program is a simple booking system where users can manage flight, hotel, and tour guide
bookings. It uses basic concepts like structures, arrays, loops, conditions, and file I/O. The
main operations are:
1. Logging in with a user ID and password.

2. Adding, viewing, editing, and deleting bookings.

3. Storing and retrieving data from text files.

The code is structured to be modular, using functions for specific tasks (like adding a flight or
logging in), which makes it easier to manage and extend.

You might also like