Skip to content
Related Articles

Related Articles

Improve Article

Railway Reservation System in C

  • Difficulty Level : Hard
  • Last Updated : 04 Aug, 2021
Geek Week

Railway ticket booking system is implemented by C programming. It is as same as one can see while we are going for online ticket booking. The following series of steps are being followed while booking a railway ticket in this software-

  1. The first step is to provide the total number of passengers and submit all the necessary details of the passengers.
  2. The next step is to enter the source and destination.
  3. A list of available trains will appear. Among them, the user has to choose one.
  4. The ticket value will be evaluated. The system will ask to enter the seat choice by showing the seat matrix. At last, a receipt will be generated on the screen.

Approach:

  • The first step is to implement a structure for taking the details of the passengers, like name, gender, and age.
  • Five functions are defined void details(int), void add_node(char, char, int), int seat(int), int cal(int, int, int), void bill(int, int) to work smoothly.
  • There are three elements in the structure like two strings one for taking passenger name and gender and one integer for taking passenger age. Also, a structure pointer will be used which helps to link the next node of another passenger. It is similar to the linked list.
  • Character arrays are defined and some integer arrays are defined globally.
  • Take the number of passengers as input and these details are sent to the details() function.
  • Execute a for loop to take details of each passenger. The details inputted by the user will be sent to the add_node() function.
  • In the add_node function, every detail will store in a node for each passenger. These nodes will link each other. This is based on the linked list concept.
  • Take the input for source place, destination place and it will give some choice of trains available. Based on that user has to give a choice. Then call the cal() function.
  • In cal() function, the user has to give a choice for sleeper or a.c. class. If the user chooses a.c. class another three options will open where the user has to give another choice based on that the system will add 18% GST on the amount and make total amount.
  • Call the seat() function where a seat matrix will be given to the user and the user has to choose a seat same with the number of passengers.
  • At last, call the bill() function where the total bill amount with all the necessary details will be displayed.

Below is the implementation of the above approach:

C




// C program for the above approach
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
  
// Defining Structure
typedef struct mynode {
    char name[20];
    char gen[6];
    int age;
    struct mynode* link;
} Node;
  
Node* start = NULL;
  
void details(int);
int seat(int);
int cal(int, int, int);
void bill(int, int);
  
// Global variables
char source[20], des[20], train[40];
char station[40], cla[40];
int time1, time2, a[55];
  
// Driver Code
void main()
{
    int i, j, a1, a2, b, c, int x = 0, d, e, r;
    char o;
    printf("Enter Number Of Passengers: ");
    fflush(stdin);
    scanf("%d", &j);
  
    // Calling details() function with
    // argument number of passenger
    details(j);
    printf("Enter The Source Place: ");
    fflush(stdin);
    gets(source);
    printf("Enter The Destination Place: ");
    gets(des);
    printf("\t\tThe Following Trains "
           "Are Available.....\n");
    printf("\t\t1. Rajdhani Express.."
           ".......10:00 "
           "a.m........Sealdah Station\n");
    printf("\t\t2. Satabdi Express..."
           ".......05:00 "
           "p.m........Howrah Station\n");
    printf("\t\t3. Humsafar Express..."
           ".......11:00 "
           "p.m........Kolkata Chitpur"
           " Station\n");
    printf("\t\t4. Garib-Rath Express"
           ".........05:00 "
           "p.m........Sealdah Station\n");
    printf("\t\t5. Duronto Express..."
           ".........07:00 "
           "a.m.........Santraganchi"
           "Station\n");
    scanf("%d", &i);
    do {
        switch (i) {
        case 1: {
            strcpy(train,
                   "Rajdhani Express");
            strcpy(station,
                   "Sealdah Station");
            time1 = 10;
            time2 = 00;
            a1 = 2099;
            a2 = 1560;
  
            // Calling cal() function
            // with the three argument
            // and return value
            d = cal(a1, a2, j);
            printf("Total Bill Amount:"
                   " %d\n",
                   d);
        }; break;
        case 2: {
            strcpy(train,
                   "Satabdi Express");
            strcpy(station,
                   "Howrah Station");
            time1 = 05;
            time2 = 00;
            a1 = 1801;
            a2 = 981;
  
            // Calling cal() function with
            // three argument & return value
            d = cal(a1, a2, j);
            printf("Total Bill Amount:"
                   "%d\n",
                   d);
        }; break;
        case 3: {
            strcpy(train,
                   "Humsafar Express");
            strcpy(station,
                   "Kolkata Chitpur Express");
            time1 = 11;
            time2 = 00;
            a1 = 2199;
            a2 = 1780;
  
            // Calling cal() function with
            // three argument & return value
            d = cal(a1, a2, j);
            printf("Total Bill Amount: %d\n", d);
        }; break;
        case 4: {
            strcpy(train, "Garib-Rath Express");
            strcpy(station, "Sealdah Station");
            time1 = 05;
            time2 = 00;
            a1 = 1759;
            a2 = 1200;
  
            // Calling cal() function with
            // three argument & return value
            d = cal(a1, a2, j);
            printf("Total Bill Amount: %d\n", d);
        }; break;
        case 5: {
            strcpy(train, "Duronto Express");
            strcpy(station, "Santraganchi Station");
            time1 = 07;
            time2 = 00;
            a1 = 2205;
            a2 = 1905;
  
            // Calling cal() function with
            // three argument & return value
            d = cal(a1, a2, j);
            printf("Total Bill Amount: %d\n", d);
        }; break;
        default:
            printf("Enter Correct choice.....\n");
            x = 1;
            break;
        }
    } while (x);
    printf("Now Book Your Seats......\n");
  
    // Calling seat() function with number
    // of passenger
    seat(j);
  
    // Calling bill() function with
    // the number of passenger
    // and amount argument
    bill(d, j);
}
  
// Function for calculation of amount
int cal(int y1, int y2, int h)
{
    int b, c, i, t, r, n;
    printf("\t\tEnter Your Choice......\n");
    printf("\t\t1. Slepper Class....\n");
    printf("\t\t2. A.C Class.......\n");
    scanf("%d", &i);
    switch (i) {
    case 1: {
        strcpy(cla, "Slepper Class");
        b = y2 * h;
        c = b + (b * 0.18);
    } break;
    case 2: {
        printf("\t\tEnter Your Choice....\n");
        printf("\t\t1. 3A Class....\n");
        printf("\t\t2. 2A Class....\n");
        printf("\t\t3. 1st Class A.C.....\n");
        scanf("%d", &n);
        switch (n) {
        case 1: {
            strcpy(cla, "3A Class");
            b = y1 * h;
            c = b + (b * 0.18);
        } break;
        case 2: {
            strcpy(cla, "2A Class");
            b = (y1 + 1000) * h;
            c = b + (b * 0.18);
        } break;
        case 3: {
            strcpy(cla, "1st Class A.C.");
            b = (y1 + 5000) * h;
            c = b + (b * 0.18);
        } break;
        default: {
            printf("\t\tEnter Right Choice......\n");
        }
        }
    } break;
    default: {
        printf("\t\tEnter Right Choice......\n");
    }
    }
    return c;
}
  
// Function for taking details
// of passengers
void details(int k)
{
    int i, a;
    char val[20], gen[6];
    for (i = 1; i <= k; i++) {
        printf("Enter The %dth Passenger Name: ", i);
        fflush(stdin);
        gets(val);
        printf("Enter The %dth Passenger Gender: ", i);
        fflush(stdin);
        gets(gen);
        printf("Enter The %dth Passenger Age: ", i);
        fflush(stdin);
        scanf("%d", &a);
  
        // Calling add_node() function
        add_node(val, gen, a);
    }
}
  
// Function to add details in node
// for each passengers
void add_node(char lol[20], char der[6], int b)
{
    Node *newptr = NULL, *ptr;
    newptr = (Node*)malloc(sizeof(Node));
    strcpy(newptr->name, lol);
    strcpy(newptr->gen, der);
    newptr->age = b;
    newptr->link = NULL;
    if (start == NULL)
        start = newptr;
    else {
        ptr = start;
        while (ptr->link != NULL)
            ptr = ptr->link;
        ptr->link = newptr;
    }
}
  
// Function for chosing seats
int seat(int p)
{
    int i;
    printf("\t           -:SEAT MATRIX:-        \n");
    printf("\t(U)    (M)        (L)    (L)    "
           "    (U)\n\n");
    printf("\t01    02        03\t04        "
           "05\n\n");
    printf("\t06    07        08\t09        "
           "10\n");
    printf("\t11    12        13\t14        "
           "15\n\n");
    printf("\t16    17        18\t19        "
           "20\n");
    printf("\t21    22        23\t24        "
           "25\n\n");
    printf("\t26    27        28\t29        "
           "30\n");
    printf("\t31    32        33\t34        "
           "35\n\n");
    printf("\t36    37        38\t39        "
           "40\n");
    printf("\t41    42        43\t44        "
           "45\n\n");
    printf("\t46    47        48\t49        "
           "50\n");
    printf("\t51    52        53\t54        "
           "55\n\n");
    printf("\t56    57        58\t59        "
           "60\n");
    printf("\tEnter Seat Numbers: \n");
    for (i = 0; i < p; i++)
        scanf("%d", &a[i]);
}
  
// Function for printing receipt
void bill(int y, int j)
{
    int i;
    Node* ptr = start;
    for (i = 1; i <= j; i++) {
        printf("\t\t\%dst Passenger Name: ", i);
        puts(ptr->name);
        printf("\t\t%dst Passenger Gender: ", i);
        puts(ptr->gen);
        printf("\t\t%dst Passenger Age: %d\n\n", i,
               ptr->age);
        ptr = ptr->link;
    }
    printf("\t\tSource Place: ");
    puts(source);
    printf("\t\tDestination Place: ");
    puts(des);
    printf("\t\tThe Boarding Station: ");
    puts(station);
    printf("\t\tTrain Is: ");
    puts(train);
    printf("\t\tAllocated Class: ");
    puts(cla);
    printf("\t\tBoarding Time: %d:%d\n", time1, time2);
    printf("\t\tTotal Bill Amount: %d\n", y);
    printf("\t\tAllocated Seats Are: \n");
    for (i = 0; i < j; i++) {
        printf("\t\t%d ", a[i]);
    }
    printf("\n");
    printf("\t\t\t\tThank You......\n");
}

Output:

Take a step-up from those “Hello World” programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.



My Personal Notes arrow_drop_up
Recommended Articles
Page :