Write a program to assign seats to passengers in an airplane. The program asks t
ID: 3704791 • Letter: W
Question
Write a program to assign seats to passengers in an airplane. The program asks the user for the number of rows of seats and the number of seats per row, and creates a two-dimensional character array with that many rows and columns. It initializes every row with letters A, B, C, etc. For example if the plane has 7 rows of seats and each row has five seats, then the seats are numbered as follows: ABCDE 2 ABCDIE 3ABCDE 4 A BCDE 5ABCDE 6ABCD E 7ABCDE The program should display the seat pattern with an X marking the seats already assigned. For example, after seats 1A, 2E, and 6D are taken, the display should look like this: 1X BCD E 2 A B CDX 3A BCDE 4ABCDE 5 ABCD E 6 A BCXE 7ABCDE After displaying the seats available, the program asks the user for the seat desired. The user types in a seat, and then the display of available seats is updated. However, if the user enters a seat that is already taken, the program prints a message "seat is occupied" and asks the user for another seat. The above steps should continue until all seats are filled or until the user signals that the program should end by entering the word 'exit' instead of a seat number Sample Input and Output Programmer: Course: Labt: Due date: Name of the programmer (your name) cosc 246, Winter 2018 Take home project #2 4-12-2018 Enter number of rows: 7 Enter number of seats per row: 5 1AB CDE 2 A B C DE 3 ABCD E 4 AB C DEExplanation / Answer
#include <iostream>
using namespace std;
class TicketBooking
{
char **SeatOrder;
int rows;
int seats;
public:
TicketBooking();
void setSeatAllocation();
void showTicketBooking();
void chooseSeat();
};
TicketBooking :: TicketBooking()
{
setSeatAllocation();
}
// Takes No of Rows and Seats for Show
void TicketBooking :: setSeatAllocation()
{
cout << "Enter Number of Rows : ";
cin >> rows;
cout << "Enter Number of Seats : ";
cin >> seats;
SeatOrder = new char *[rows] ;
for( int i = 0 ; i < rows ; i++ )
SeatOrder[i] = new char[seats];
char startval;
for( int i = 0 ; i < rows ; i++ )
{
startval=65; // for seat name start with A,B,C
for( int j = 0 ; j < seats ; j++ )
{
SeatOrder[i][j]= startval++;
}
}
}
void TicketBooking ::showTicketBooking()
{
for( int i = 0 ; i < rows ; i++ )
{
cout << i+1 << " ";
for( int j = 0 ; j < seats ; j++ )
{
cout << SeatOrder[i][j] << " ";
}
cout <<endl;
}
}
//Choose Ticket booking
void TicketBooking ::chooseSeat()
{
int lrow;
char lseat;
cout << "Enter Number of Rows Number and Seat Number : ";
cin >> lrow;
cout << "Enter Number of Seats : ";
cin >> lseat;
if(lseat > 90)
{
cout << "Invalid Seat .. It should be BLOCK LETTERS(A,B,C)";
return;
}
lrow = lrow-1;
if(lrow > rows)
{
cout << "Invalid Row Number"<<endl;
return;
}
lseat = lseat-65;
if (SeatOrder[lrow][lseat] != 'X')
{
SeatOrder[lrow][lseat] = 'X';
}
else
cout << "Seat already Occupied" << endl;
}
int main()
{
TicketBooking obj;
cout << "====================================================== "<<endl;
cout << " Aeroplane TicketBooking "<<endl;
cout << "====================================================== "<<endl;
cout << "1. DISPLAY TICKET AVALIBILITY"<<endl;
cout << "2. BOOK TICKET (User can select one ticket at a time" <<endl;
cout << "3. EXIT"<<endl;
int option;
cout << "select your choice : ";
cin >> option;
switch(option)
{
case 1:
obj.showTicketBooking();
break;
case 2:
obj.chooseSeat();
obj.showTicketBooking();
char dummy;
while(1)
{
cout << "To Continue Press.... Y : ";
cin >> dummy;
if(dummy == 'Y')
{
obj.chooseSeat();
obj.showTicketBooking();
}
else
break;
}
break;
case 3:
cout << "Booking Exited...!" << endl;
break;
default:
cout << "Invalid Option "<<endl;
}
return 0;
}
//Output:
//Enter No rows : 7
//Enter No seats : 5
//1. DISPLAY TICKET AVALIBILITY"
//2. BOOK TICKET (User can select one ticket at a time)
//3. EXIT
//Select Option
//Press 1: show tickets display
//Press 2: Ask input Now of rows and Seat (A,B,C,E,D.E)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.