I am working on a seating chart program. The purpose of the program is to display a seating chart with seat prices and allow users to select either a specific seat or a seat based off price. I've pretty much got it working properly with a few exceptions.
I posted on here with a question regarding one of those exceptions but was quickly informed that first I needed to address the issue of the repetitive code for each price. I need to figure out a single chunk of code that will implement this for every price range. I've looked it over but not quite sure where to start. Any suggestions?
#include <iostream>
#include <iomanip>
using namespace std;
const int ROWS = 9;
const int COLUMNS = 10;
int main()
{
bool isDone = false;
string rowNumber[] =
{
"Row 1: ",
"Row 2: ",
"Row 3: ",
"Row 4: ",
"Row 5: ",
"Row 6: ",
"Row 7: ",
"Row 8: ",
"Row 9: ",
};
int seatingChart[ROWS][COLUMNS] =
{
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
{10, 10, 20, 20, 20, 20, 20, 20, 10, 10},
{10, 10, 20, 20, 20, 20, 20, 20, 10, 10},
{10, 10, 20, 20, 20, 20, 20, 20, 10, 10},
{20, 20, 30, 30, 40, 40, 30, 30, 20, 20},
{20, 30, 30, 40, 50, 50, 40, 30, 30, 20},
{30, 40, 50, 50, 50, 50, 50, 50, 40, 30},
};
// Prints array to the screen
cout << "\t Please choose a seat or a price: \n\n" << endl;
cout << " 1 2 3 4 5 6 7 8 9 10" << endl;
cout << " --------------------------------------" << endl;
for (int row = 0; row < 9; row++)
{
cout << rowNumber[row];
for (int column = 0; column < 10; column++)
{
cout << seatingChart[row][column] << " ";
}
cout << endl;
}
cout << "\n" << endl;
// Main Program Loop
do
{
char input;
cout << "Press (S) to select a specific seat\n";
cout << "Press (P) to select a seat based on price\n";
cout << "Press (Q) to quit\n\n";
cout << "Your selection: ";
cin >> input;
// Select a specific seat by it's coordinates
if (input == 's' || input == 'S')
{
int xCoord;
int yCoord;
cout << "\nPlease input the row number: ";
cin >> yCoord;
int seatRow = yCoord - 1;
cout << "Please input the seat number: ";
cin >> xCoord;
int seatNumber = xCoord - 1;
if (seatingChart[seatRow][seatNumber] == 0)
{
cout << "\nI'm sorry that seat has been sold. Please select a different seat." << endl;
}else
{
cout << "\nThe seat you selected is $" << seatingChart[seatRow][seatNumber] << endl;
seatingChart[seatRow][seatNumber] = 0;
}
// Select a seat based off of price
}else if (input == 'p' || input == 'P')
{
int seatPrice;
cout << "Please enter a seat price: $";
cin >> seatPrice;
// $10 seats
if (seatPrice == 10)
{
bool found = false;
while (found == false)
{
for (int row = 0; row < 9; row++)
{
for (int column = 0; column < 10; column++)
{
if (seatingChart[row][column] == 10 && !found)
{
found = true;
seatingChart[row][column] = 0;
cout << "\n" << endl;
}
}
}
}
}
// $20 seats
else if (seatPrice == 20)
{
bool found = false;
while (found == false)
{
for (int row = 0; row < 9; row++)
{
for (int column = 0; column < 10; column++)
{
if (seatingChart[row][column] == 20 && !found)
{
found = true;
seatingChart[row][column] = 0;
cout << "\n" << endl;
}
}
}
}
}
// $30 seats
else if (seatPrice == 30)
{
bool found = false;
while (found == false)
{
for (int row = 0; row < 9; row++)
{
for (int column = 0; column < 10; column++)
{
if (seatingChart[row][column] == 30 && !found)
{
found = true;
seatingChart[row][column] = 0;
cout << "\n" << endl;
}
}
}
}
}
// $40 seats
else if (seatPrice == 40)
{
bool found = false;
while (found == false)
{
for (int row = 0; row < 9; row++)
{
for (int column = 0; column < 10; column++)
{
if (seatingChart[row][column] == 40 && !found)
{
found = true;
seatingChart[row][column] = 0;
cout << "\n" << endl;
}
}
}
}
}
// $50 seats
else if (seatPrice == 50)
{
bool found = false;
while (found == false)
{
for (int row = 0; row < 9; row++)
{
for (int column = 0; column < 10; column++)
{
if (seatingChart[row][column] == 50 && !found)
{
found = true;
seatingChart[row][column] = 0;
cout << "\n" << endl;
}
}
}
}
}else // Input validation
{
cin.fail();
cout << "\nSorry, there are no seats available for that price" << endl;
}
}else if (input == 'q' || input == 'Q')
{
isDone = true;
}else
{
cin.fail();
cout << "\nInvalid selection" << endl;
}
cout << "\n" << endl;
}while (isDone == false);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire