Detailed C Project
Detailed C Project
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.
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();
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);
}
// 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);
users[userCount++] = newUser;
printf("User added successfully!\n");
}
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");
}
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");
}
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);
}
}
// 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);
}
}
}
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.
Structure Fields: Each structure holds data relevant to a specific booking type (e.g.,
destination, price, departure_time for flights).
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.
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.
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.
Here:
This file contains the hotel booking details. Each line represents a hotel, with id, name,
price, and location.
Here:
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:
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Using dynamic memory allocation for arrays (instead of static sizes) for scalability.
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:
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:
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.
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).
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.
Macros
#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.
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;
TourGuide: Holds information about a tour guide.
typedef struct {
int id;
char name[50];
int rating;
char availability[20];
} TourGuide;
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.
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.
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.
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.