Project 2a

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

#include <iostream>

#include <iomanip>
const int ROWS = 14;
const int SEATS = 6;
void air(char plane[ROWS][SEATS]);
void printAir(const char message[], char plane[ROWS][SEATS]);
void checkTicket( char &ticketType );
void checkType( char ticketType ,int &row, char &seat );
using namespace std;
int main()
{
int taken = 0;
int seats = (ROWS - 1) * (SEATS - 1);
char plane[ROWS][SEATS];
char option = 'y';
int row = 0;
char seat;
int row_index, seat_index;
char ticketType = '\0';
air(plane);
cout << "Welcome to Airline seat reservation program. Please pick the seat you
would like to reserve." << endl;
while (taken < seats && (option == 'y' || option=='Y'))
{
printAir("The seat layout is as follows: 'X' represents booked seats and '*'
represents available seats.", plane);
cout << endl;
cout << "\n" ;
ticketType = toupper(ticketType);
checkTicket ( ticketType);
checkType( ticketType,row,seat);
row_index = row;
seat_index = toupper(seat) - 'A' + 1;
if (plane[row_index][seat_index] == 'X')//if theres 'X'
cout << "Sorry, " << row << seat << " is already taken." << endl;
else//else if there is no 'X'
{
cout << "Your reservation for " << row << seat<<" is confirmed!" << endl;
plane[row_index][seat_index] = 'X';//take seat and row and increment taken
seat
taken++;
printAir("Congratulation, your seat is reserved!", plane);
}

if (taken < seats)//available seats then check if they want to keep going
{
cout << "Would you like to reserve another seat? Y/N ";
cin >> option;//if the seat is taken or if they finished with reserving a seat
}
else
cout << "All the seats have been reserved, goodbye!" << endl;//if all the
seats are taken
}
printAir("The current seating layout is as follows: ", plane);//prints seats with the
reserved ones
return 0;
}
void air(char plane[ROWS][SEATS])//prints seat layout
{
int r,c;
plane[0][0] = ' ';
for (r = 1; r < ROWS; r++)
{
plane[0][r] = 'A' + (r - 1);
}
for (r = 1; r < ROWS; r++)
{
for ( c = 0 ; c < 13 ; c++)
plane[r][c] = ' ';
}
for (r = 1; r < ROWS ; r++)
{
for ( c = 1 ; c < SEATS ; c++)//shows the astricks representing available seats
plane[r][c] = '*';
}
}
void printAir(const char message[], char plane[ROWS][SEATS])//prints seat layout
with message
{
cout << message << endl;
int row_r,seat_c;
for ( row_r = 0; row_r < ROWS ; row_r ++)
{
if (row_r == 0)
cout << "
";
else if (row_r != 0)
cout << "Row " << row_r;
if (row_r < 10 )
cout << " ";
for (seat_c = 0; seat_c < SEATS; seat_c++)
cout << setw(2) << plane [row_r][seat_c] << " ";

cout << endl;


}
}
void checkTicket( char &ticketType )//checks for first, economy or business class;
also rows and seats
{
bool input = true;
while(input)
{
cout << "Row 1 and 2 are FIRST CLASS. " << endl;
cout << "Rows 3 through 7 are BUSINESS CLASS. " << endl;
cout << "Rows 8 through 13 are ECONOMY CLASS: " << endl << "\n";
cout << "Enter your ticket type:" << endl;
cout<<"==> 'F/f' for FIRST CLASS " << endl;
cout<< "==> 'B/b' for BUSINESS CLASS" <<endl;
cout<<"==> 'E/e' ECONOMY CLASS:";
cin >> ticketType;
cout << "\n";
ticketType = toupper(ticketType);//takes ticket type
if (ticketType == 'F')
input = false;
else if (ticketType == 'B')
input = false;
else if (ticketType == 'E')
input = false;
else
{
input = true;
cout << "Invalid input. try again " << endl;//if no match of the ticket type
output this message
cout << "\n";
}
}
}
void checkType( char ticketType ,int &row, char &seat )
{//checks for seat letter for first class.
bool input;
if ( ticketType == 'F')
{ input = true;
while(input)
{
cout << "Enter the row (1-2) and seat between (A-F) in the format '1A': ";
cin >> row >> seat;
cout << "\n";
seat = toupper(seat);
if ((row >= 1) && (row <= 2))
{

if( seat == 'A')


input = false;
else if ( seat == 'B')
input = false;
else if ( seat == 'C')
input = false;
else if ( seat == 'D')
input = false;
else if (seat == 'E' )
input = false;
else if ( seat == 'F')
input = false;
else
{input = true;
cout << "Invalid input, please try again " << endl;}
cout << "\n";
}
else
{cout << "Invalid input, please try again " << endl;
cout << "\n";}
}
}
else if ( ticketType == 'B')//checks seat letter for business
{ input = true;
while(input)
{
cout << "Enter the row (3-7) and seat between (A-F) in the format 3A: ";
cin >> row >> seat;
cout << "\n";
seat = toupper(seat);
if ((row >= 3) && (row <= 7))
{
if( seat == 'A')
input = false;
else if ( seat == 'B')
input = false;
else if ( seat == 'C')
input = false;
else if ( seat == 'D')
input = false;
else if (seat == 'E' )
input = false;
else if ( seat == 'F')
input = false;
else
{input = true;
cout << "Invalid input, please try again " << endl;}
cout << "\n";}
else
{cout << "Invalid input, please try again " << endl;

cout << "\n";}


}
}
else if ( ticketType == 'E')//checks for economy class seat letter
{input = true;
while(input)
{
cout << "Enter the row (8-13) and seat between (A-F) in the format 8A: ";
cin >> row >> seat;
cout << "\n";
seat = toupper(seat);
if ((row >= 8) && (row <= 13))
{
if( seat == 'A')
input = false;
else if ( seat == 'B')
input = false;
else if ( seat == 'C')
input = false;
else if ( seat == 'D')
input = false;
else if (seat == 'E' )
input = false;
else if ( seat == 'F')
input = false;
else
{input = true;
cout << "Invalid input, please try again " << endl;}
cout << "\n";}
else
{cout << "Invalid input, please try again " << endl;
cout << "\n";}
}
}
}

You might also like