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
|
#include <iostream>
using namespace std;
char ** CreateArrayOfSeats (int NumberRows, int seats);
void InitializeSeats (char **ArrayOfSeats, int NumberRows, int seats);
void DisplaySeats (char **ArrayOfSeats, int NumberRows, int seats);
void MemoryCleanup (char **ArrayOfSeats, int NumberRows, int seats);
int main(int argc, char * argv[]) {
char **ArrayOfSeats;
char Select[3];
char answer;
int NumberRows;
int NumberSeats;
int Seats_Available;
int row;
int seat;
cout << "Welcome to your personalized seating reservation system!" << endl;
cout << "Please enter the number of rows: ";
cin >> NumberRows;
cout << "Please enter the number of seats in each row: ";
cin >> NumberSeats;
Seats_Available = NumberRows*NumberSeats;
ArrayOfSeats = CreateArrayOfSeats(NumberRows, NumberSeats);
InitializeSeats(ArrayOfSeats, NumberRows, NumberSeats);
DisplaySeats(ArrayOfSeats, NumberRows, NumberSeats);
do {
do
{
cout << endl;
cout << "There are " << Seats_Available << " seats available, to choose from."<<endl;
cout << "Please enter a seat selection (Example 5F): " << endl;
cin >> Select;
row = Select[0] - '1';
if(Select[1] >= '0' && Select[1] <= '9') // In case there are more than 9 rows
{
row += 1;
row *= 10;
row += (Select[1] - '1');
Select[1] = Select[2];
}
Select[1] = toupper(Select[1]);
seat = Select[1] - 'A';
if(row < 0 || row > NumberRows-1)
cout << "Row selections go from 1 to " << NumberRows <<". Try a different row.." << endl;
if(seat < 0 || seat > NumberSeats-1)
cout << "Seat selections go from A to " << char('A'+ (NumberSeats-1)) << ". Try a different seat.." << endl;
}while ( row < 0 || row > NumberRows-1 || seat < 0 || seat > NumberSeats-1);
if(row >= 0)
{
if (ArrayOfSeats[row][seat] == '-')
cout << "The seat you selected has already been taken. Please try another seat." <<endl;
else
{
ArrayOfSeats[row][seat] = '-';
Seats_Available--;
}
if(Seats_Available == 0)
cout << "There are no more seats available, so the program is ending." << endl;
}
DisplaySeats(ArrayOfSeats, NumberRows, NumberSeats);
do
{
cout << endl << "Reserve another seat? (Y/N)" << endl;
cin >> answer;
answer = toupper(answer);
if(answer != 'Y' && answer != 'N')
cout << "I check only for a 'Y' or an 'N' response!" << endl;
}while(answer != 'Y' && answer != 'N');
if(answer == 'N')
row = -1;
} while (Seats_Available > 0 && row >= 0 );
MemoryCleanup(ArrayOfSeats, NumberRows, NumberSeats);
cout << endl << "Program has ended." << endl;
cout << endl << "Press the ENTER key to continue." << endl << endl;
char buff[100];
cin.getline (buff, 100);
return 0;
}
//// CreatArrayOfSeats Function
char **CreateArrayOfSeats (int NumberRows, int seats) {
char **ArrayOfSeats;
ArrayOfSeats = new char*[NumberRows];
for (int r=0; r<NumberRows; r++)
ArrayOfSeats[r] = new char[seats];
return ArrayOfSeats;
}
///// InitializeSeats Function
void InitializeSeats (char **ArrayOfSeats, int NumberRows, int seats) {
for (int r=0; r<NumberRows; r++) {
for (int s=0; s<seats; s++)
ArrayOfSeats[r][s] = 'A' + s;
}
}
///// DisplaySeats Function
void DisplaySeats (char **ArrayOfSeats, int NumberRows, int NumberSeats) {
for (int r=0; r<NumberRows; r++) {
cout.width(2);
cout << r+1 << ' ';
for (int s=0; s<NumberSeats; s++)
cout << ArrayOfSeats[r][s] << ' ';
cout << endl;
}
}
///// MemoryCleanup Function
void MemoryCleanup (char **ArrayOfSeats, int NumberRows, int NumberSeats) {
for (int r=0; r<NumberRows; r++)
delete [] ArrayOfSeats[r];
delete [] ArrayOfSeats;
}
|