0% found this document useful (0 votes)
11 views3 pages

Varun C Proj

The document presents a C program for a bus reservation system that allows users to view available buses, book tickets, view bookings, and cancel tickets. It defines structures for buses and seats, manages a maximum of three buses with ten seats each, and provides a menu-driven interface for user interactions. The program handles input validation and displays appropriate messages for various operations.

Uploaded by

ourname9227
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Varun C Proj

The document presents a C program for a bus reservation system that allows users to view available buses, book tickets, view bookings, and cancel tickets. It defines structures for buses and seats, manages a maximum of three buses with ten seats each, and provides a menu-driven interface for user interactions. The program handles input validation and displays appropriate messages for various operations.

Uploaded by

ourname9227
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>

#define MAX_SEATS 10
#define MAX_BUSES 3

typedef struct {
int seat_number;
char passenger_name[50];
} Seat;

typedef struct {
int bus_id;
char bus_name[50];
Seat seats[MAX_SEATS];
int booked_seats;
} Bus;

Bus buses[MAX_BUSES] = {
{1, "Express Bus", {}, 0},
{2, "Luxury Bus", {}, 0},
{3, "Sleeper Bus", {}, 0}
};

void displayBuses() {
printf("\nAvailable Buses:\n");
for (int i = 0; i < MAX_BUSES; i++) {
printf("Bus ID: %d - %s (Seats Available: %d)\n",
buses[i].bus_id, buses[i].bus_name, MAX_SEATS -
buses[i].booked_seats);
}
}

void bookTicket() {
int bus_id, seat_num;
char name[50];

displayBuses();
printf("\nEnter Bus ID to book a seat: ");
scanf("%d", &bus_id);

if (bus_id < 1 || bus_id > MAX_BUSES) {


printf("Invalid Bus ID!\n");
return;
}

Bus *bus = &buses[bus_id - 1];

if (bus->booked_seats >= MAX_SEATS) {


printf("No seats available on this bus.\n");
return;
}

printf("Enter Passenger Name: ");


scanf(" %[^\n]", name);

seat_num = bus->booked_seats + 1;
strcpy(bus->seats[bus->booked_seats].passenger_name, name);
bus->seats[bus->booked_seats].seat_number = seat_num;
bus->booked_seats++;

printf("Ticket booked successfully! Seat Number: %d\n", seat_num);


}

void viewBookings() {
int bus_id;
displayBuses();
printf("\nEnter Bus ID to view bookings: ");
scanf("%d", &bus_id);

if (bus_id < 1 || bus_id > MAX_BUSES) {


printf("Invalid Bus ID!\n");
return;
}

Bus *bus = &buses[bus_id - 1];

if (bus->booked_seats == 0) {
printf("No bookings for this bus.\n");
return;
}

printf("\nBooked Seats for %s:\n", bus->bus_name);


for (int i = 0; i < bus->booked_seats; i++) {
printf("Seat %d: %s\n", bus->seats[i].seat_number, bus-
>seats[i].passenger_name);
}
}

void cancelTicket() {
int bus_id, seat_num;

displayBuses();
printf("\nEnter Bus ID to cancel a ticket: ");
scanf("%d", &bus_id);

if (bus_id < 1 || bus_id > MAX_BUSES) {


printf("Invalid Bus ID!\n");
return;
}

Bus *bus = &buses[bus_id - 1];

if (bus->booked_seats == 0) {
printf("No bookings to cancel.\n");
return;
}

printf("Enter Seat Number to cancel: ");


scanf("%d", &seat_num);

int found = 0;
for (int i = 0; i < bus->booked_seats; i++) {
if (bus->seats[i].seat_number == seat_num) {
found = 1;
for (int j = i; j < bus->booked_seats - 1; j++) {
bus->seats[j] = bus->seats[j + 1];
}
bus->booked_seats--;
printf("Ticket for seat %d canceled successfully.\n", seat_num);
break;
}
}

if (!found) {
printf("Seat number not found!\n");
}
}

int main() {
int choice;

while (1) {
printf("\n--- Bus Reservation System ---\n");
printf("1. View Buses\n");
printf("2. Book a Ticket\n");
printf("3. View Bookings\n");
printf("4. Cancel Ticket\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1: displayBuses(); break;
case 2: bookTicket(); break;
case 3: viewBookings(); break;
case 4: cancelTicket(); break;
case 5: printf("Exiting...\n"); exit(0);
default: printf("Invalid choice! Try again.\n");
}
}

return 0;
}

You might also like