Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Flight Booking Ticket Program in C

here is a simple example of C code that could be used to book a flight ticket.

#include <stdio.h>

#include <string.h>

struct Flight {

int flightNumber;

char origin[50];

char destination[50];

char departureTime[50];

char arrivalTime[50];

int capacity;

int seatsTaken;

};

int main(void) {

struct Flight f;

printf(“Enter flight number: “);

scanf(“%d”, &f.flightNumber);

printf(“Enter origin: “);

scanf(“%s”, f.origin);

printf(“Enter destination: “);

scanf(“%s”, f.destination);

printf(“Enter departure time: “);

scanf(“%s”, f.departureTime);

printf(“Enter arrival time: “);

scanf(“%s”, f.arrivalTime);

printf(“Enter capacity: “);

scanf(“%d”, &f.capacity);

printf(“Enter seats taken: “);

scanf(“%d”, &f.seatsTaken);

if (f.seatsTaken < f.capacity) { printf(“Booking successful!\n”);

} else

{ printf(“Sorry, flight is full.\n”); }

return 0;

}
This code defines a Flight struct with several fields representing information about a flight, such as its flight number, origin and destination, departure and arrival times, and the number of seats on the plane.

The main function prompts the user to enter this information and then checks if there are any seats available on the flight. If there are, it prints “Booking successful!”, otherwise it prints “Sorry, flight is full.”




WhatsApp Group

Join Now



Telegram Group

Join Now
A function to book a ticket on a flight and another function to list all available flights:
#include <stdio.h>
#include <string.h>

#define MAX_FLIGHTS 100

struct Flight {
int flightNumber;
char origin[50];
char destination[50];
char departureTime[50];
char arrivalTime[50];
int capacity;
int seatsTaken;
};

struct Flight flights[MAX_FLIGHTS];
int numFlights = 0;

void bookTicket(void) {
int flightNumber;
printf(“Enter flight number: “);
scanf(“%d”, &flightNumber);

for (int i = 0; i < numFlights; i++) {
if (flights[i].flightNumber == flightNumber) {
if (flights[i].seatsTaken < flights[i].capacity) {
flights[i].seatsTaken++;
printf(“Booking successful!\n”);
} else {
printf(“Sorry, flight is full.\n”);
}
return;
}
}

printf(“Flight not found.\n”);
}

void listFlights(void) {
for (int i = 0; i < numFlights; i++) {
printf(“Flight %d: %s to %s, %s – %s, %d/%d seats taken\n”,
flights[i].flightNumber, flights[i].origin, flights[i].destination,
flights[i].departureTime, flights[i].arrivalTime,
flights[i].seatsTaken, flights[i].capacity);
}
}

int main(void) {
while (1) {
printf(“1. Book ticket\n2. List flights\n3. Quit\n”);
int choice;
scanf(“%d”, &choice);

if (choice == 1) {
bookTicket();
} else if (choice == 2) {
listFlights();
} else {
break;
}
}

return 0;
}
This program has a bookTicket function that prompts the user to enter a flight number and then searches for a flight with that number in the flights array.

If the flight is found and there are seats available, it books a ticket on the flight by incrementing the seatsTaken field.

The listFlights function simply iterates over the flights array and prints out the details of each flight.

The main function presents the user with a menu of options: they can book a ticket, list all flights, or quit the program. It calls the appropriate function based on the user’s choice.

Here is an example that uses a linked list to store the flights:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct Flight {
int flightNumber;
char origin[50];
char destination[50];
char departureTime[50];
char arrivalTime[50];
int capacity;
int seatsTaken;
struct Flight *next;
};

struct Flight *head = NULL;

void bookTicket(void) {
int flightNumber;
printf(“Enter flight number: “);
scanf(“%d”, &flightNumber);

struct Flight *current = head;
while (current != NULL) {
if (current->flightNumber == flightNumber) {
if (current->seatsTaken < current->capacity) {
current->seatsTaken++;
printf(“Booking successful!\n”);
} else {
printf(“Sorry, flight is full.\n”);
}
return;
}
current = current->next;
}

printf(“Flight not found.\n”);
}

void listFlights(void) {
struct Flight *current = head;
while (current != NULL) {
printf(“Flight %d: %s to %s, %s – %s, %d/%d seats taken\n”,
current->flightNumber, current->origin, current->destination,
current->departureTime, current->arrivalTime,
current->seatsTaken, current->capacity);
current = current->next;
}
}

void addFlight(void) {
struct Flight *newFlight = malloc(sizeof(struct Flight));
if (newFlight == NULL) {
printf(“Error allocating memory.\n”);
return;
}

printf(“Enter flight number: “);
scanf(“%d”, &newFlight->flightNumber);

printf(“Enter origin: “);
scanf(“%s”, newFlight->origin);

printf(“Enter destination: “);
scanf(“%s”, newFlight->destination);

printf(“Enter departure time: “);
scanf(“%s”, newFlight->departureTime);

printf(“Enter arrival time: “);
scanf(“%s”, newFlight->arrivalTime);

printf(“Enter capacity: “);
scanf(“%d”, &newFlight->capacity);

printf(“Enter seats taken: “);
scanf(“%d”, &newFlight->seatsTaken);

newFlight->next = head;
head = newFlight;
}

int main(void) {
while (1) {
printf(“1. Book ticket\n2. List flights\n3. Add flight\n4. Quit\n”);
int choice;
scanf(“%d”, &choice);

if (choice == 1) {
bookTicket();
} else if (choice == 2) {
listFlights();
} else if (choice == 3) {
addFlight();
} else {
break;
}
}

return 0;
}
We think this code will help you in school and college to make project and smart.

some asked question by user is same answer

Airline Booking In C With Source Code
what is airline booking in c programming

what is simple aireline booking system with source code

How do I write a C program to book a flight ticket?
What are some tips for designing an airline reservation system in C?
How do I store flight information in a C program (e.g. using an array, struct, or linked list)?
How can I implement functions in C to search for flights, book tickets, and display flight information?
How do I handle errors and edge cases in an airline reservation system written in C (e.g. when a flight is full or a user enters invalid input)?
example that stores the flights in a binary search tree:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct Flight {
int flightNumber;
char origin[50];
char destination[50];
char departureTime[50];
char arrivalTime[50];
int capacity;
int seatsTaken;
struct Flight *left;
struct Flight *right;
};

struct Flight *root = NULL;

struct Flight *createFlight(int flightNumber, char *origin, char *destination,
char *departureTime, char *arrivalTime,
int capacity, int seatsTaken) {
struct Flight *newFlight = malloc(sizeof(struct Flight));
if (newFlight == NULL) {
printf(“Error allocating memory.\n”);
return NULL;
}

newFlight->flightNumber = flightNumber;
strcpy(newFlight->origin, origin);
strcpy(newFlight->destination, destination);
strcpy(newFlight->departureTime, departureTime);
strcpy(newFlight->arrivalTime, arrivalTime);
newFlight->capacity = capacity;
newFlight->seatsTaken = seatsTaken;
newFlight->left = NULL;
newFlight->right = NULL;

return newFlight;
}

void insertFlight(struct Flight *flight) {
if (root == NULL) {
root = flight;
} else {
struct Flight *current = root;
while (1) {
if (flight->flightNumber < current->flightNumber) {
if (current->left == NULL) {
current->left = flight;
break;
}
current = current->left;
} else {
if (current->right == NULL) {
current->right = flight;
break;
}
current = current->right;
}
}
}
}

struct Flight *searchFlight(int flightNumber) {
struct Flight *current = root;
while (current != NULL) {
if (current->flightNumber == flightNumber) {
return current;
} else if (flightNumber < current->flightNumber) {
current = current->left;
} else {
current = current->right;
}
}
return NULL;
}

void bookTicket(void) {
int flightNumber;
printf(“Enter flight number: “);
scanf(“%d”, &flightNumber);

struct Flight *flight = searchFlight(flightNumber);
if (flight == NULL) {
printf(“Flight not found.\n”);
} else {
if (flight->seatsTaken < flight->capacity) {
flight->seatsTaken++;
printf(“Booking successful!\n”);
} else {
printf(“Sorry, flight is full.\n”);
}
}
}

void listFlights(struct Flight *flight) {
if (flight == NULL) {
return;
}

listFlights(flight->left);
printf(“Flight

Leave a Reply

Your email address will not be published. Required fields are marked *