1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
// Program displays the seat diagram for an airplane and allows the user
// to select a seat. If the seat is available, the program marks it as taken
// and displays a new diagram. If the seat is not available, the program says
// so and asks for another selection. The program terminates when the user
// asks to stop or all seats are taken.
#include <iostream>
using namespace std;
const int NUMROWS = 7;
const int NUMSEATS = 4;
void initPlane(char plane[NUMROWS][NUMSEATS]);
void printPlane(char msg[], char plane[NUMROWS][NUMSEATS]);
void getSeat(char &row, char &seat);
int main()
{
int seatsTaken = 0;
int seats = NUMROWS * NUMSEATS;
char plane[NUMROWS][NUMSEATS];
char keepGoing = 'y';
char row, seat;
int rowIndex, seatIndex;
initPlane(plane);
cout << "Choose your seat!" << endl;
while (seatsTaken < seats && keepGoing == 'y')
{
// Show layout and get seat choice
printPlane("Plane layout; X designates taken seats", plane);
getSeat(row, seat);
// Adjust input to use as indices
rowIndex = row - '1';
seatIndex = seat - 'A';
// Check to see if seat is taken
if (plane[rowIndex][seatIndex] == 'X')
cout << "Sorry, " << row << seat << " is already taken." << endl;
else
{
cout << "OK, you've got " << row << seat << endl;
plane[rowIndex][seatIndex] = 'X';
seatsTaken++;
}
// If there are seats left, see if we should keep going
if (seatsTaken < seats)
{
cout << "Choose another seat? (y/n) ";
cin >> keepGoing;
}
else
cout << "Plane is now full!" << endl;
}
printPlane("Final seating chart", plane);
return 0; // successful completion
}
void initPlane(char plane[NUMROWS][NUMSEATS])
{
// POSTCONDITION:
// plane[x][0] == 'A', 0<=x<=6
// plane[x][1] == 'B', 0<=x<=6
// plane[x][2] == 'C', 0<=x<=6
// plane[x][3] == 'D', 0<=x<=6
int i, j;
for(i=0; i<NUMROWS; i++)
for(j=0; j<NUMSEATS; j++)
plane[i][j] = 'A' + j;
return;
}
void printPlane(char msg[], char plane[NUMROWS][NUMSEATS])
{
// POSTCONDITION: The seating layout of the plane has been printed
// to the standard output with an X displayed for each taken seat.
// print the header message
cout << msg << endl;
// print the seating layout of the plane
// put an extra blank for an aisle in the middle when printing each row
int i,j;
for( i = 0; i< NUMROWS; i++)
{
cout << i+1 <<" ";
for(j=0;j<NUMSEATS;j++)
{
cout << plane[i][j]<<" " ;
cout << endl;
}
}
return;
}
void getSeat(char &row, char &seat)
{
int i,j;
// POSTCONDITION: '1' <= row <= '7', 'A' <= seat <= 'D'
// Get row and seat from user.
// row must be between '1' and '7'. seat must be between 'A' and 'D'.
// Note that getSeat does not check to see if the seat has been taken.
cout << "Enter the row(1-7) you would like: ";
// validate the row is a character between '1' and '7'
cin>>row;
cout << "Enter the seat(A-D) you would like: ";
// validate the seat is a character between 'A' and 'D'
cin>>seat;
i=row-'0';
j=toupper(seat)-'A';
while(i<1||i>NUMROWS||j>NUMSEATS||j<0)
{
cout<<"invalid seat, reenter\n";
cin>>row;
cin>>seat;
i=row-'0';
j=toupper(seat)-'A';
}
seat=toupper(seat);
return;
}
/*
Example seating chart with 2A and 3D taken:
Plane layout; X designates taken seats
1 A B C D
2 X B C D
3 A B C X
4 A B C D
5 A B C D
6 A B C D
7 A B C D
*/
|