Given the following code, please include as many comments necessary to explain w
ID: 3764125 • Letter: G
Question
Given the following code, please include as many comments necessary to explain what is happening throughout.
#include <iostream>
#include <string>
#include <iomanip>
#define NUMBER_ROWS 13 // similar to stating global variables, wherever NUMBER_ROWS is stated in the code it will give a value of 13
#define NUMBER_SEATS 6 // similar to stating global variables, wherever NUMBER_SEATS is stated in the code it will give a value of 6
using namespace std;
void init(char[][NUMBER_SEATS]);
void printPlane(char[][NUMBER_SEATS]);
void getSeatNum(char[][NUMBER_SEATS], int&, int&);
void checkSeat(char[][NUMBER_SEATS], int, int);
char getSeatYN(); // function prototypes
int main()
{
int row;
int col;
int i;
int j;
bool goodseat;
char choice;
char plane[NUMBER_ROWS][NUMBER_SEATS];
init(plane);
cout << "Hello! This program will help you reserve a plane seat and show you available plane seats." << endl;
cout << endl;
printPlane(plane);
choice = getSeatYN();
while (choice != 'N')
{
getSeatNum(plane, row, col);
checkSeat(plane, row, col);
printPlane(plane);
choice = getSeatYN();
}
return 0;
}
void checkSeat(char plane[][NUMBER_SEATS], int row, int col)
{
if (plane[row - 1][col] != 'X')
{
plane[row - 1][col] = 'X';
cout << "The seat is reserved for you ";
}
else
{
cout << "Seat taken!" << endl;
}
}
void init(char plane[][NUMBER_SEATS])
{
int i, j;
for (i = 0; i < NUMBER_ROWS; i++)
for (j = 0; j < NUMBER_SEATS; j++)
plane[i][j] = '*';
}
void getSeatNum(char[][NUMBER_SEATS], int& row, int& col)
{
char ticket;
char seat;
int min, max;
bool good = true;
do
{
good = true;
cout << "Please choose the type of seat you would like from the menu below: " << endl;
cout << "F = First Class " << ' ' << "B = Business Class "
<< ' ' << "E = Economy Class" << endl;
cin >> ticket;
ticket = toupper(ticket);
switch (ticket)
{
case 'F':min = 1;
max = 2;
break;
case 'E':
min = 8;
max = 13;
break;
case 'B': min = 3;
max = 7;
break;
default: good = false;
cout << "invalid seat type ";
}
} while (!good);
good = false;
do
{
cout << "Please enter in the desired Row Number (" << min << "-" << max << "): " << endl;
cin >> row;
} while (row<min || row>max);
do
{
cout << "Please enter in the desired Seat (A-F): " << endl;
cin >> seat;
col = tolower(seat) - 'a';
if (col<0 || col>5)
cout << "Invalid Seat ";
else if ((ticket == 'F') && (row > 2) || row<1)
cout << "Invalid Selction for First Class" << endl;
else if ((ticket == 'B') && ((row <= 2) || (row > 7)))
cout << "Invalid Selection for Business Class" << endl;
else if ((ticket == 'E') && ((row < 8) || (row > 13)))
cout << "Invalid Selection for Economy Class" << endl;
else
good = true;
} while (!good);
}
char getSeatYN()
{
char choice;
cout << "Would you like to make a selection? (Y/N): ";
cin >> choice;
choice = static_cast<char>(toupper(choice));
return choice;
}
void printPlane(char plane[][NUMBER_SEATS])
{
int i, j;
cout << setw(11) << "A" << setw(5) << "B"
<< setw(5) << "C" << setw(5) << "D"
<< setw(5) << "E" << setw(5) << "F" << endl;
cout << " " << endl;
for (i = 0; i < NUMBER_ROWS; i++)
{
cout << "Row " << setw(2) << i + 1;
for (j = 0; j < NUMBER_SEATS; j++)
cout << setw(5) << plane[i][j];
cout << endl;
}
cout << " " << endl;
cout << "* -- available seat ";
cout << "X -- occupied seat ";
cout << "Rows 1 and 2 are for first class customers ";
cout << "Rows 3 through 7 are for business class passengers ";
cout << "Rows 8 through 13 are for economy class passengers ";
}
Run the program to help clarify.
Explanation / Answer
#include <iostream>
#include <string>
#include <iomanip>
// declare constants for number of seats per row and number of rows
#define NUMBER_ROWS 13 // similar to stating global variables, wherever NUMBER_ROWS is stated in the code it will give a value of 13
#define NUMBER_SEATS 6 // similar to stating global variables, wherever NUMBER_SEATS is stated in the code it will give a value of 6
using namespace std;
// declare the functions
void init(char[][NUMBER_SEATS]);
void printPlane(char[][NUMBER_SEATS]);
void getSeatNum(char[][NUMBER_SEATS], int&, int&);
void checkSeat(char[][NUMBER_SEATS], int, int);
char getSeatYN(); // function prototypes
// main function starts here.
int main()
{
// declare the variables needed in the program.
int row;
int col;
int i;
int j;
bool goodseat;
char choice;
char plane[NUMBER_ROWS][NUMBER_SEATS];
init(plane);
// print a welcome message
cout << "Hello! This program will help you reserve a plane seat and show you available plane seats." << endl;
cout << endl;
// call the function to show the seats left
printPlane(plane);
// ask the user whether he/she wants to select a seat('N' for no and any other letter for yes).
choice = getSeatYN();
// if user presses any other letter than 'N' or 'n'
while (choice != 'N')
{
// call the function to get the seat selection from the user
getSeatNum(plane, row, col);
// check whether the seat is available or not. If available, book it
checkSeat(plane, row, col);
// print the seat map again
printPlane(plane);
// ask the user whether he/she wants to select a seat again('N' for no and any other letter for yes).
choice = getSeatYN();
}
return 0;
}
// function to check whether seat is available or not.
void checkSeat(char plane[][NUMBER_SEATS], int row, int col)
{
if (plane[row - 1][col] != 'X')
{
plane[row - 1][col] = 'X';
cout << "The seat is reserved for you ";
}
else
{
cout << "Seat taken!" << endl;
}
}
// function to initialize all the seats by '*'
void init(char plane[][NUMBER_SEATS])
{
int i, j;
for (i = 0; i < NUMBER_ROWS; i++)
for (j = 0; j < NUMBER_SEATS; j++)
plane[i][j] = '*';
}
// function to input seat number from user
void getSeatNum(char[][NUMBER_SEATS], int& row, int& col)
{
char ticket;
char seat;
int min, max;
bool good = true;
do
{
good = true;
cout << "Please choose the type of seat you would like from the menu below: " << endl;
cout << "F = First Class " << ' ' << "B = Business Class "
<< ' ' << "E = Economy Class" << endl;
cin >> ticket;
ticket = toupper(ticket);
switch (ticket)
{
case 'F':min = 1;
max = 2;
break;
case 'E':
min = 8;
max = 13;
break;
case 'B': min = 3;
max = 7;
break;
default: good = false;
cout << "invalid seat type ";
}
} while (!good);
good = false;
do
{
cout << "Please enter in the desired Row Number (" << min << "-" << max << "): " << endl;
cin >> row;
} while (row<min || row>max);
do
{
cout << "Please enter in the desired Seat (A-F): " << endl;
cin >> seat;
col = tolower(seat) - 'a';
if (col<0 || col>5)
cout << "Invalid Seat ";
else if ((ticket == 'F') && (row > 2) || row<1)
cout << "Invalid Selction for First Class" << endl;
else if ((ticket == 'B') && ((row <= 2) || (row > 7)))
cout << "Invalid Selection for Business Class" << endl;
else if ((ticket == 'E') && ((row < 8) || (row > 13)))
cout << "Invalid Selection for Economy Class" << endl;
else
good = true;
} while (!good);
}
// function to ask the user whether he wants to select seats or not
char getSeatYN()
{
char choice;
cout << "Would you like to make a selection? (Y/N): ";
cin >> choice;
choice = static_cast<char>(toupper(choice));
return choice;
}
// print the current seat map
void printPlane(char plane[][NUMBER_SEATS])
{
int i, j;
cout << setw(11) << "A" << setw(5) << "B"
<< setw(5) << "C" << setw(5) << "D"
<< setw(5) << "E" << setw(5) << "F" << endl;
cout << " " << endl;
for (i = 0; i < NUMBER_ROWS; i++)
{
cout << "Row " << setw(2) << i + 1;
for (j = 0; j < NUMBER_SEATS; j++)
cout << setw(5) << plane[i][j];
cout << endl;
}
cout << " " << endl;
cout << "* -- available seat ";
cout << "X -- occupied seat ";
cout << "Rows 1 and 2 are for first class customers ";
cout << "Rows 3 through 7 are for business class passengers ";
cout << "Rows 8 through 13 are for economy class passengers ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.