Answer to Question #41789 in C++ for samir

Question #41789
Write a program for an airline company that assigns seats on each flight of the airline’s only plane (capacity: 10 seats).
Your program should display the following menu of alternatives:
• Please type 1 for “First Class”
• Please type 2 for “Economy”
If the person types 1, your program should assign a seat in the first class section (seat 1 – 5).
If the person types 2, your program should assign a seat in the economy section (seat 6 – 10).
Your program should print a boarding pass indicating the person’s seat number and whether it’s in the first class or economy section of the plane.
Use a one-dimensional array to represent the seating chart of the plane. Initialize all the elements of the array to false to indicate that all seats are empty. As each seat is assigned, set the corresponding elements of the array to true to indicate that the seat is no longer available.
Your program should never assign a seat that is already been assigned. When the first class section is full, your program should ask the person if it’s acceptable to be placed in the economy section (and vice versa). If yes, then make the appropriate seat assignment. If no, then print the message text “Next flight leaves in 3 hours.”
1
Expert's answer
2014-05-05T11:02:13-0400
#include <iostream>
#include <cstdlib>
using namespace std;
const int capacity = 10;
const int firstClassCapacity = 5;
enum SECTION{SECTION_FIRST, SECTION_ECONOM};
void menu();
int main() {
menu();
return 0;
}
void printMenu() {
system("cls");
cout << "Please type 0 to EXIT" << endl;
cout << "Please type 1 for \"First class\"" << endl;
cout << "Please type 2 for \"Economy\"" << endl;
cout << "Choice: ";
}
void printBoardingPass(int seat, SECTION section) {
cout << endl << endl;
cout << "Your ticket:" << endl;
cout << "Seat number: " << seat + 1 << endl;
cout << "Section: " << (section == SECTION_FIRST ? "First Class" : "Economy") << endl;
cout << endl << endl;
}
int firstClassSeat(bool plane[capacity]) {
for (int i = 0; i < firstClassCapacity; ++i) {
if (plane[i] == false) {
return i;
}
}
return -1;
}
int economSeat(bool plane[capacity]) {
for (int i = firstClassCapacity; i < capacity; ++i) {
if (plane[i] == false) {
return i;
}
}
return -1;
}
void menu() {
bool plane[capacity];
for (int i = 0; i < capacity; ++i) {
plane[i] = false;
}
int choice = -1, seat = 0;
while (choice) {
printMenu();
cin >> choice;
switch(choice) {
case 0 :
break;
case 1 :
seat = firstClassSeat(plane);
if (seat == -1) {
int seatEc = economSeat(plane);
if (seatEc != -1) {
cout << "First Class is full. Would you like to be place in Economy? \n1.Yes\n2.No\n";
int c2 = -1;
cin >> c2;
if (c2 == 1) {
plane[seatEc] = true;
printBoardingPass(seatEc, SECTION_ECONOM);
}
else {
cout << "Next flight leaves in 3 hours." << endl;
}
}
else {
cout << "Next flight leaves in 3 hours." << endl;
}
}
else {
plane[seat] = true;
printBoardingPass(seat, SECTION_FIRST);
}
break;
case 2 :
seat = economSeat(plane);
if (seat == -1) {
int seatFirst = firstClassSeat(plane);
if (seatFirst != -1) {
cout << "Economy class is full. Would you like to be place in First Class? \n1.Yes\n2.No\n";
int c2 = -1;
cin >> c2;
if (c2 == 1) {
plane[seatFirst] = true;
printBoardingPass(seatFirst, SECTION_FIRST);
}
else {
cout << "Next flight leaves in 3 hours." << endl;
}
}
else {
cout << "Next flight leaves in 3 hours." << endl;
}
}
else {
plane[seat] = true;
printBoardingPass(seat, SECTION_ECONOM);
}
break;
default :
cout << "Wrong choice! Try again." << endl;
}
system("pause");
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS