0% found this document useful (0 votes)
4 views4 pages

Online Ticket Booking System Code

A code for movie ticket booking
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Online Ticket Booking System Code

A code for movie ticket booking
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

/******************************************************************************

Welcome to GDB Online.


GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite,
Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TICKETS 5
#define QUEUE 10

typedef struct {
int id;
char name[50];
int booked;
} Ticket;

typedef struct {
char names[QUEUE][50];
int front, rear, size;
} Queue;

void initTickets(Ticket t[]) {


for (int i = 0; i < TICKETS; i++) {
t[i].id = i + 1;
t[i].booked = 0;
strcpy(t[i].name, "");
}
}

void initQueue(Queue *q) {


q->front = 0;
q->rear = -1;
q->size = 0;
}

int isEmpty(Queue *q) {


return q->size == 0;
}

int isFull(Queue *q) {


return q->size == QUEUE;
}

void addToQueue(Queue *q, char name[]) {


if (isFull(q)) return;
q->rear = (q->rear + 1) % QUEUE;
strcpy(q->names[q->rear], name);
q->size++;
}

void removeFromQueue(Queue *q, char name[]) {


if (isEmpty(q)) return;
strcpy(name, q->names[q->front]);
q->front = (q->front + 1) % QUEUE;
q->size--;
}

void bookTicket(Ticket t[], Queue *q) {


char name[50];
printf("Enter name: ");
scanf("%s", name);

for (int i = 0; i < TICKETS; i++) {


if (!t[i].booked) {
t[i].booked = 1;
strcpy(t[i].name, name);
printf("Ticket %d booked for %s.\n", t[i].id, name);
return;
}
}
if (!isFull(q)) {
addToQueue(q, name);
printf("No tickets available. %s added to the queue.\n", name);
} else {
printf("Queue is full.\n");
}
}

void cancelTicket(Ticket t[], Queue *q) {


int id;
printf("Enter ticket ID to cancel: ");
scanf("%d", &id);

if (id < 1 || id > TICKETS || !t[id - 1].booked) {


printf("Invalid ticket ID.\n");
return;
}

printf("Ticket %d canceled (was for %s).\n", id, t[id - 1].name);


t[id - 1].booked = 0;

if (!isEmpty(q)) {
char next[50];
removeFromQueue(q, next);
t[id - 1].booked = 1;
strcpy(t[id - 1].name, next);
printf("Ticket %d reassigned to %s.\n", id, next);
}
}

void checkAvailable(Ticket t[]) {


int count = 0;
for (int i = 0; i < TICKETS; i++) {
if (!t[i].booked) count++;
}
printf("Tickets available: %d\n", count);
}
void displayBookedTickets(Ticket t[]) {
int found = 0;
printf("\n--- Booked Tickets ---\n");
for (int i = 0; i < TICKETS; i++) {
if (t[i].booked) {
printf("Ticket ID: %d, Name: %s\n", t[i].id, t[i].name);
found = 1;
}
}
if (!found) {
printf("No tickets are currently booked.\n");
}
}

void viewQueue(Queue *q) {


if (isEmpty(q)) {
printf("Queue is empty.\n");
return;
}
printf("Queue: ");
for (int i = 0; i < q->size; i++) {
printf("%s ", q->names[(q->front + i) % QUEUE]);
}
printf("\n");
}

void menu() {
printf("\n1. Book Ticket\n");
printf("2. Cancel Ticket\n");
printf("3. Check Availability\n");
printf("4. View Queue\n");
printf("5. Display Booked Tickets\n");
printf("6. Exit\n");
}

int main() {
Ticket t[TICKETS];
Queue q;
int choice;

initTickets(t);
initQueue(&q);

while (1) {
menu();
printf("Choice: ");
scanf("%d", &choice);

switch (choice) {
case 1: bookTicket(t, &q); break;
case 2: cancelTicket(t, &q); break;
case 3: checkAvailable(t); break;
case 4: viewQueue(&q); break;
case 5: displayBookedTickets(t); break;
case 6: return 0;
default: printf("Invalid choice.\n");
}
}
}

You might also like