Reputation: 25
int reserveSeating(char seatingPlan[][COLS]) {
char ticketClass, choice, letter;
int column = 0, row = 0, rowStart, rowEnd;
bool isValidInput = true;
while (isValidInput) {
cout << "\nPlease choose class (first class (F/f), business class (B/b), or economy class (E/e)): ";
cin >> ticketClass;
cin.clear(); //this line takes the input stream out of fail state if they entered anything other than a number.
cin.ignore(100, '\n'); //this empties the input stream and gets it ready for the new input.
ticketClass = toupper(ticketClass);
switch (ticketClass) {
case 'F':
rowStart = 1;
rowEnd = 2;
isValidInput = false;
break;
case 'B':
rowStart = 3;
rowEnd = 7;
isValidInput = false;
break;
case 'E':
rowStart = 8;
rowEnd = 13;
isValidInput = false;
break;
default:
cout << "Ticket class is invalid." << endl << "Please try again.\n";
break;
}
}
isValidInput = true;
while (isValidInput) {
cout << "\nPlease enter desired seat : ";
cin >> row >> letter;
letter = toupper(letter);
if ((row > 0 && row < 14) && (letter >= 65 && letter <= 70)) {
if ((row < rowStart || row > rowEnd)) {
cout << "\nThe desired seat is not in the selected class. \nReserve the seat (Yes (Y/y) or No (N/n)): ";
cin >> choice;
if (toupper(choice) == 'Y') {
switch (letter) {
case 'A':
column = 0;
break;
case 'B':
column = 1;
break;
case 'C':
column = 2;
break;
case 'D':
column = 3;
break;
case 'E':
column = 4;
break;
case 'F':
column = 5;
break;
}
row--;
checkSeat(row, column, seatingPlan, letter);
}
} else {
switch (letter) {
case 'A':
column = 0;
break;
case 'B':
column = 1;
break;
case 'C':
column = 2;
break;
case 'D':
column = 3;
break;
case 'E':
column = 4;
break;
case 'F':
column = 5;
break;
}
row--;
checkSeat(row, column, seatingPlan, letter);
}
} else {
cout << endl << "Ticket " << row << letter << " is an invalid ticket." << endl << "Please try again.\n";
}
isValidInput = false;
}
return 0;
}
I've done most of what I needed. I am attempting to find a more efficient way to minimize the number of switch statements I need though. Also, are there any recommendations on how to rewrite this more clearly, and any recommendations on how to better document this code? Any recommendations or examples on how you document your code? All I do is mostly in-line comments.
Upvotes: 0
Views: 70
Reputation: 75062
You can use std::map
to create a map to something to another thing.
#include <map> // for std::map
#include <utility> // for std::pair
int reserveSeating(char seatingPlan[][COLS]) {
static const std::map<char, std::pair<int, int> > ticketClassTable = {
{'F', {1, 2}}, {'B', {3, 7}}, {'E', {8, 13}}
};
static const std::map<char, int> columnTable = {
{'A', 0}, {'B', 1}, {'C', 2}, {'D', 3}, {'E', 4}, {'F', 5}
};
char ticketClass, choice, letter;
int column = 0, row = 0, rowStart, rowEnd;
bool isValidInput = true;
while (isValidInput) {
cout << "\nPlease choose class (first class (F/f), business class (B/b), or economy class (E/e)): ";
cin >> ticketClass;
cin.clear(); //this line takes the input stream out of fail state if they entered anything other than a number.
cin.ignore(100, '\n'); //this empties the input stream and gets it ready for the new input.
ticketClass = toupper(ticketClass);
auto it = ticketClassTable.find(ticketClass);
if (it != ticketClassTable.end()) {
rowStart = it->second.first;
rowEnd = it->second.second;
isValidInput = false;
} else {
cout << "Ticket class is invalid." << endl << "Please try again.\n";
}
}
isValidInput = true;
while (isValidInput) {
cout << "\nPlease enter desired seat : ";
cin >> row >> letter;
letter = toupper(letter);
if ((row > 0 && row < 14) && (letter >= 65 && letter <= 70)) {
if ((row < rowStart || row > rowEnd)) {
cout << "\nThe desired seat is not in the selected class. \nReserve the seat (Yes (Y/y) or No (N/n)): ";
cin >> choice;
if (toupper(choice) == 'Y') {
auto it = columnTable.find(letter);
if (it != columnTable.end()) {
column = it->second;
}
row--;
checkSeat(row, column, seatingPlan, letter);
}
} else {
auto it = columnTable.find(letter);
if (it != columnTable.end()) {
column = it->second;
}
row--;
checkSeat(row, column, seatingPlan, letter);
}
} else {
cout << endl << "Ticket " << row << letter << " is an invalid ticket." << endl << "Please try again.\n";
}
isValidInput = false;
}
return 0;
}
Upvotes: 0
Reputation: 67352
Well, this portion:
switch (letter) {
case 'A':
column = 0;
break;
case 'B':
column = 1;
break;
case 'C':
column = 2;
break;
case 'D':
column = 3;
break;
case 'E':
column = 4;
break;
case 'F':
column = 5;
break;
is functionally equivalent to (with perhaps a bounds check, if you really don't trust your data):
column = letter - 'A';
Same goes for the middle one as well. The first switch is more complicated, I'd leave it alone.
Upvotes: 1