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
|
#include <iostream> //allows use of cout and endl commands
#include <iomanip> // allows use of manipulation commands (setprecision, ect)
using namespace std; //This line allows you to use cin, cout and endl without the std:: prefix
// function declaration so function can be put after main
void menu();
void openSeats(bool seats[][6], int from, int to);
void reserveSeat(int fromRow, int toRow);
int aisleConversion(char aisle, int &c);
//static variables to avoid global variables
static int choice;
static int r;
static int c;
static char aisle;
bool seats[13][6] = { {0, 0, 1, 0, 1, 1}, //Row 1
{0, 1, 0, 1, 0, 1}, //Row 2
{0, 0, 1, 1, 0, 1}, //Row 3
{1, 0, 1, 0, 1, 1}, //Row 4
{0, 1, 0, 1, 0, 0}, //Row 5
{0, 1, 0, 0, 0, 1}, //Row 6
{1, 0, 0, 0, 1, 1}, //Row 7
{0, 1, 0, 1, 1, 0}, //Row 8
{1, 0, 1, 1, 0, 1}, //Row 9
{0, 1, 0, 1, 1, 1}, //Row 10
{0, 0, 1, 0, 1, 0}, //Row 11
{0, 0, 1, 1, 0, 1}, //Row 12
{0, 0, 0, 0, 1, 0}
}; //Row 13
//menu choices
static const int DISPLAYSEATS = 1, FIRSTCLASS = 2, BUSINESSCLASS = 3, ECONOMY = 4, QUIT = 9;
int
main() //heading of the function main, every program must have a main function
{
//info line
cout << "This program allows you to pick a seat for your upcoming flight. " << endl;
cout << endl;
//start of loop
do {
menu(); //show menu function
switch (choice) //switch start
{
//cases to execute functions
case DISPLAYSEATS:
openSeats(seats, 0, 12);
break;
case FIRSTCLASS:
reserveSeat(0, 1);
break;
case BUSINESSCLASS:
reserveSeat(2, 8);
break;
case ECONOMY:
reserveSeat(9, 12);
break;
}
} while (choice != QUIT); //if 9 is entered quits the program
system("pause"); //pauses the command window to see program running
return 0; //ends the program
}
void
menu() // sets function type as one that is not returning data to the program, only displays the menu choices, all text
{
cout << endl;
cout << "Menu Choices: " << endl;
cout << endl;
cout << "Press 1 to display all open seats: " << endl;
cout << "Press 2 to reserve a first class seat: " << endl;
cout << "Press 3 to reserve a business class seat: " << endl;
cout << "Press 4 to reserve an economy class seat: " << endl;
cout << "Press 9 to exit the program: " << endl;
cout << "Please enter a selection: ";
cin >> choice; //variable for selection from menu function
}
//shows seat menu for rows "from" to "to" (inclusive)
void
openSeats(bool seats[][6], int from, int to)
{
cout << endl << "The current status of the seats on this flight are: . " << endl;
cout << setw(11) << "A" << setw(5) << "B" << setw(5) << "C" << setw(5) << "D" <<
setw(5) << "E" << setw(5) << "F" << endl;
for (int row = from; row <= to; ++row) {
cout << "Row " << setw(2) << row + 1;
for (size_t col = 0; col < 6; ++col) {
cout << setw(5) << (seats[row][col] == false ? '*' : 'X');
}
cout << '\n';
}
cout << endl << "Seats available for occupancy are indicated by a *. " << endl;
}
void
reserveSeat(int fromRow, int toRow)
{
openSeats(seats, fromRow, toRow);
cout <<
"Please select your seat from those available, as indicated by a '*', please select your row : ";
cin >> r;
cout << "Please select your aisle : ";
cin >> aisle;
aisleConversion(aisle, c);
if (seats[r - 1][c] == true) {
cout << "That seat is occupied." << endl;
} else if (seats[r - 1][c] == false) {
cout << endl << "You have selected to sit in seat " << r << " in aisle " << aisle
<< "." << endl;
seats[r - 1][c] = true;
}
cout << endl;
}
int
aisleConversion(char aisle, int &c)
{
c = toupper(aisle) - 'A';
return c;
}
|