Dashboard Temp Share Shortlinks Frames API

HTMLify

Travel Booking
Views: 109 | Author: guest
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct
{
    char *name;
    int age;
    char gender;
    char *destination;
    char seatid[4];
} Passenger;

Passenger *passengers = NULL;
int length = 0;

void bookTicket(const char *name, const int age, char Gender, const char *destination, const char seatID[4])
{
    Passenger *temp = (Passenger *)realloc(passengers, (length + 1) * sizeof(Passenger));

    if (!temp)
    {
        printf("Memory reallocation failed!!");
    }
    else
    {
        passengers = temp;
    }
    passengers[length].name = (char *)malloc(strlen(name) + 1);
    strcpy(passengers[length].name, name);
    passengers[length].destination = (char *)malloc(strlen(destination) + 1);
    strcpy(passengers[length].destination, destination);

    strncpy(passengers[length].seatid, seatID, sizeof(passengers[length].seatid) - 1);
    passengers[length].seatid[sizeof(passengers[length].seatid) - 1] = '\0';

    passengers[length].gender = Gender;
    passengers[length].age = age;

    length++;
}

void CancelTicket()
{
}

void viewAvailseats()
{
}

void viewPassangers()
{
    int i;
    for (i = 0; i < length; i++)
    {
        printf("\n1.\t%s\t%d[%c]\t%s\t%s", passengers[i].name, passengers[i].age, passengers[i].gender, passengers[i].destination, passengers[i].seatid);
    }
}

int main()
{
    char nameInput[1024], destinationInput[1024], genderinput, seadIdInput[4];
    int ageInput;
    int chs;
    int running = 1;

    while (running)
    {
        printf("\nOptions\n");
        printf("1.Book Ticket\n");
        printf("2.Cancel Ticket\n");
        printf("3.View Avalible seats\n");
        printf("4.view Passengers Info\n");
        printf("5.Exit\n");
        printf("\n");

        printf("What task would you like to do(1-6) : ");
        scanf("%d", &chs);
        getchar();

        switch (chs)
        {
        case 1:
            printf("Enter Passenger's  name : ");
            fgets(nameInput, sizeof(nameInput), stdin);
            nameInput[strcspn(nameInput, "\n")] = 0;
            printf("Enter age : ");
            scanf(" %d", &ageInput);
            while (getchar() != '\n')
                ;
            printf("Enter Passenger's Gender : ");
            scanf(" %c", &genderinput);
            while (getchar() != '\n')
                ;
            printf("Enter the destination :");
            fgets(destinationInput, sizeof(destinationInput), stdin);
            destinationInput[strcspn(destinationInput, "\n")] = 0;
            printf("Enter the seadID to allot to the Passenger : ");
            fgets(seadIdInput, 4, stdin);
            seadIdInput[strcspn(seadIdInput, "\n")] = 0;

            bookTicket(nameInput, ageInput, genderinput, destinationInput, seadIdInput);
            printf("Ticket booked Succesfully\n");
            while (getchar() != '\n')
                ;
            break;

        case 4:
            viewPassangers();
            break;
        case 5:
            running = 0;
            break;
        default:
            printf("Error : Incorrect Choice selection , Please enter between 1-6");
            continue;
        }
    }
}