need help with C++ homework, can\'t use pointers or global variables for this as
ID: 3860562 • Letter: N
Question
need help with C++ homework, can't use pointers or global variables for this assignment.
Write a program that can be used to assign seats for a commercial airline. The airplane has 13
rows, with 6 seats in each row. Rows 1-3 are first-class seats, while rows 5-13 are economy
seats.
Create a menu-driven program. Display a “map” of all of the seats on the airplane (display a star
(*) to indicate the seat is available; display an X if the seat is occupied). Ask the user which class
of ticket they are interested in (first-class or economy) and to select the specific seat(s) they
wish to reserve.
You must create this program by writing the following functions: initialize will fill the array with '*', displayMap will display the current seating map of the entire airplane, makeReservation will let the user select the specific seat(s) they wish to reserve, and validateFunction will make sure that a) the user is not trying to reserve seats that are already taken and b) the user’s seat-class choice is appropriate for thespecific seats they’ve selected.
A B C D E F
Row 1 * * X * X X
Row 2 * X * X * X
Row 3 X * X X * X
Explanation / Answer
Program to assign seats for a commercial airline ::
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <cstring>
using namespace std;
const int NUMROWS = 14;
const int NUMSEATS = 7;
void initPlane(char plane[NUMROWS][NUMSEATS])
{
int x,y;
plane[0][0] = ' ';
plane[0][1] = 'A';
plane[0][2] = 'B';
plane[0][3] = 'C';
plane[0][4] = 'D';
plane[0][5] = 'E';
plane[0][6] = 'F';
plane[1][0] = '1';
plane[2][0] = '2';
plane[3][0] = '3';
plane[4][0] = '4';
plane[5][0] = '5';
plane[6][0] = '6';
plane[7][0] = '7';
plane[8][0] = '8';
plane[9][0] = '9';
plane[10][0] = 'W';
plane[11][0] = 'Y';
plane[12][0] = 'Z';
plane[13][0] = 'G';
for (x = 1; x < NUMROWS ; x++)
{
for ( y = 1 ; y < NUMSEATS ; y++)
plane[x][y] = '*';
}
}
void printPlane(char msg[], char plane[NUMROWS][NUMSEATS])
{
cout << msg << endl;
int Row,col;
for ( Row = 0; Row < NUMROWS ; Row ++)
{
for (col = 0; col < NUMSEATS; col++)
cout << setw(5) << plane [Row][col] << " ";
cout << endl;
}
}
int main()
{
int seatsTaken = 0;
int seats = NUMROWS * NUMSEATS;
char plane[NUMROWS][NUMSEATS];
char keepGoing = 'y';
int row = 0;
char seat;
int rowIndex, seatIndex;
char ticketType[15];
initPlane(plane);
cout << "Choose your seat!" << endl;
while (seatsTaken < seats && keepGoing == 'y')
{
printPlane("Plane layout; X designates taken seats", plane);
cout << "Row 1 and 2 are first clss. " << endl;
cout << "Rows 3 through 7 are business class. " << endl;
cout << "Rows 8 through 13 are economy class. " << endl;
cout << "Eneter ticket type (first class,business class,or economy class) :" ;
cin >> row >> seat;
rowIndex = row;
if ( seat == 'A' || seat == 'a' )
seatIndex = 1;
else if ( seat == 'B' || seat == 'b' )
seatIndex = 2;
else if ( seat == 'C' || seat == 'c' )
seatIndex = 3;
else if ( seat == 'D' || seat == 'd' )
seatIndex = 4;
else if ( seat == 'E' || seat == 'e' )
seatIndex = 5;
else if ( seat == 'F' || seat == 'f' )
seatIndex = 6;
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 (seatsTaken < seats)
{
cout << "Choose another seat? (y/n) ";
cin >> keepGoing;
}
else
cout << "Plane is now full!" << endl;
}
printPlane("Final seating chart", plane);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.