Create Airline Reservation System in C

Faraz

By Faraz -

Learn to create an Airline Reservation System in C Programming with a detailed step-by-step guide.


create-airline-reservation-system-in-c.webp

Introduction

Creating an Airline Reservation System in C programming is a great way to enhance your coding skills and understand the fundamentals of programming. This project will guide you through developing a simple yet functional system for booking flights. The system will have a user-friendly interface to make it easy for users to navigate through the available options. By the end of this tutorial, you'll be able to create a basic airline reservation system with a beautiful user interface.

Set Up Your Development Environment

  1. Install a C Compiler: Make sure you have a C compiler installed on your computer. Popular choices include GCC and MinGW for Windows, or Xcode for macOS.
  2. Choose an IDE: While you can use any text editor, an Integrated Development Environment (IDE) like Code::Blocks or Visual Studio Code can make coding easier.

Step-by-Step Guide for Creating an Airline Reservation System

Step 1: Include Necessary Libraries

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
  • stdio.h: Provides input and output functions.
  • stdlib.h: Provides functions for memory allocation and program control.
  • string.h: Provides functions for string handling.

Step 2: Define Constants and Data Structures

#define MAX_FLIGHTS 100
#define MAX_SEATS 200

typedef struct {
    int flightNumber;
    char destination[50];
    int totalSeats;
    int availableSeats;
    float ticketPrice;
} Flight;

Flight flights[MAX_FLIGHTS];
int flightCount = 0;
  • MAX_FLIGHTS and MAX_SEATS define the maximum number of flights and seats.
  • Flight structure stores details about each flight.
  • flights array stores all flights.
  • flightCount keeps track of the number of flights.

Step 3: Display Menu Function

void displayMenu() {
    printf("\n------------------------------------\n");
    printf("  Airline Reservation System\n");
    printf("------------------------------------\n");
    printf("1. View Available Flights\n");
    printf("2. Book a Flight\n");
    printf("3. Cancel a Flight\n");
    printf("4. View Bookings\n");
    printf("5. Exit\n");
    printf("------------------------------------\n");
    printf("Enter your choice: ");
}
  • Displays the main menu with options for the user to choose from.

Step 4: Initialize Flights Function

void initializeFlights() {
    flights[0] = (Flight){101, "New York", 100, 50, 200.0};
    flights[1] = (Flight){102, "Los Angeles", 100, 30, 150.0};
    flights[2] = (Flight){103, "Chicago", 100, 70, 180.0};
    flightCount = 3;
}
  • Initializes sample flight data for demonstration purposes.

Step 5: View Available Flights Function

void viewAvailableFlights() {
    printf("\nAvailable Flights:\n");
    printf("Flight Number | Destination    | Available Seats | Ticket Price\n");
    printf("--------------------------------------------------------------\n");
    for (int i = 0; i < flightCount; i++) {
        printf("%-14d | %-15s | %-15d | $%.2f\n",
               flights[i].flightNumber,
               flights[i].destination,
               flights[i].availableSeats,
               flights[i].ticketPrice);
    }
}
  • Displays a list of available flights with their details.

Step 6: Book a Flight Function

void bookFlight() {
    int flightNumber;
    printf("Enter flight number to book: ");
    scanf("%d", &flightNumber);

    for (int i = 0; i < flightCount; i++) {
        if (flights[i].flightNumber == flightNumber) {
            if (flights[i].availableSeats > 0) {
                flights[i].availableSeats--;
                printf("Booking successful! Flight Number %d\n", flightNumber);
                return;
            } else {
                printf("Sorry, no available seats on Flight Number %d.\n", flightNumber);
                return;
            }
        }
    }
    printf("Flight Number %d not found.\n", flightNumber);
}
  • Books a seat on the specified flight if available.

Step 7: Cancel a Flight Function

void cancelFlight() {
    int flightNumber;
    printf("Enter flight number to cancel: ");
    scanf("%d", &flightNumber);

    for (int i = 0; i < flightCount; i++) {
        if (flights[i].flightNumber == flightNumber) {
            if (flights[i].totalSeats > flights[i].availableSeats) {
                flights[i].availableSeats++;
                printf("Cancellation successful! Flight Number %d\n", flightNumber);
                return;
            } else {
                printf("No bookings found for Flight Number %d.\n", flightNumber);
                return;
            }
        }
    }
    printf("Flight Number %d not found.\n", flightNumber);
}
  • Cancels a booking if there are existing bookings.

Step 8: View Bookings Function

void viewBookings() {
    printf("\nCurrent Bookings:\n");
    printf("Flight Number | Destination    | Booked Seats | Available Seats\n");
    printf("--------------------------------------------------------------\n");
    for (int i = 0; i < flightCount; i++) {
        int bookedSeats = flights[i].totalSeats - flights[i].availableSeats;
        printf("%-14d | %-15s | %-12d | %-15d\n",
               flights[i].flightNumber,
               flights[i].destination,
               bookedSeats,
               flights[i].availableSeats);
    }
}
  • Shows the current number of booked and available seats for each flight.

Step 9: Main Function

int main() {
    int choice;
    initializeFlights();

    do {
        displayMenu();
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                viewAvailableFlights();
                break;
            case 2:
                bookFlight();
                break;
            case 3:
                cancelFlight();
                break;
            case 4:
                viewBookings();
                break;
            case 5:
                printf("Exiting the system.\n");
                break;
            default:
                printf("Invalid choice. Please try again.\n");
        }
    } while (choice != 5);

    return 0;
}
  • Initializes flight data.
  • Displays the menu and processes user choices in a loop until the user chooses to exit.

Compiling and Running the Program

  1. Save Your Code: Save your code in a file named airline_reservation.c.
  2. Compile the Program: Open your terminal or command prompt and navigate to the directory where your file is saved. Run the following command:
    gcc -o airline_reservation airline_reservation.c
  3. Run the Program: Execute the compiled program with:
    ./airline_reservation

Conclusion

Creating an Airline Reservation System in C programming not only sharpens your coding skills but also gives you practical experience with system design and user interface development. By following this guide, you have learned how to implement a basic yet functional booking system with a user-friendly menu. Keep experimenting and enhancing your system by adding more features, such as saving data to files or databases, to make it even better.

That’s a wrap!

I hope you enjoyed this article

Did you like it? Let me know in the comments below 🔥 and you can support me by buying me a coffee.

And don’t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!

Thanks!
Faraz 😊

End of the article

Subscribe to my Newsletter

Get the latest posts delivered right to your inbox


Latest Post

Please allow ads on our site🥺