1
\$\begingroup\$

I'm taking my first C++ class right now that started back in September. At the beginning of the semester we were given the option to work on an extra credit final project which I decided to try and tackle. The program produces a seating chart for a theater. The user is to be given the option of picking a specific seat or to choose a seat based off price. I started working on the program as soon as we started learning about loops. We just got done covering functions and we're barely getting into the chapter on arrays and vectors now. I've completed the bulk of the program. For the most part it works but I know it is far from perfect.

What can I do to make this code more concise? I realize I have a lot of duplicate code and I'm sure it can be cleaned up, possibly by implementing functions but I'm not quite sure where to start with that.

My other major issue is I don't know how to get the program to recognize when there are no more seats available at a certain price. For example if you choose a seat by price and select all the $50 seats I want the program to notify the user when there are no more seats available at that price. As of right now once all the seats are gone at a particular price the program does not give a warning message and instead just returns the user to the initial prompt.

#include <iostream>
#include <iomanip>
using namespace std;

const int ROWS = 9;
const int COLUMNS = 10;

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},

};

int main()
{
    bool isDone = false;

    // 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 << setw(2) << seatingChart[row][column] << "  ";
        }

        cout << endl;
    }

    cout << "\n" << endl;

    // Main Program Loop
    do
    {
        bool found = false;

        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] = seatingChart[seatRow][seatNumber] / 10;
            }

        // 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)
            {
                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] = 1;

                            cout << "\nSeat " << column + 1 << ", in row " << row + 1 << ", is available for $10\n" << endl;

                        }else if(seatingChart[row][column] == 1 && found)
                        {
                            cout << "I'm sorry there are no more seats available at that price" << endl;
                        }
                    }
                }
            }

            // $20 seats
            else if (seatPrice == 20)
            {
                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] = 2;

                            cout << "\nSeat " << column + 1 << ", in row " << row + 1 << ", is available for $20\n" << endl;

                        }else if(seatingChart[row][column] == 2 && found)
                        {
                            cout << "I'm sorry there are no more seats available at that price" << endl;
                        }
                    }
                }
            }

            // $30 seats
            else if (seatPrice == 30)
            {
                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] = 3;

                            cout << "\nSeat " << column + 1 << ", in row " << row + 1 << ", is available for $30\n" << endl;


                        }else if(seatingChart[row][column] == 3 && found)
                        {
                            cout << "I'm sorry there are no more seats available at that price" << endl;
                        }
                    }
                }
            }

            // $40 seats
            else if (seatPrice == 40)
            {
                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] = 4;

                            cout << "\nSeat " << column + 1 << ", in row " << row + 1 << ", is available for $40\n" << endl;


                        }else if (seatingChart[row][column] == 4 && found)
                        {
                            cout << "I'm sorry there are no more seats available at that price" << endl;
                        }
                    }
                }
            }

            // $50 seats
            else if (seatPrice == 50)
            {
                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] = 5;

                            cout << "\nSeat " << column + 1 << ", in row " << row + 1 << ", is available for $50\n" << endl;

                        }else if (seatingChart[row][column] == 5 && found)
                        {
                            cout << "Sorry, there are no more seats available at that price" << 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);


    cout << "Purchase Summary" << "\n" << endl;

    // Purchase Summary
    double total = 0;

    for (int row = 0; row < 9; row++)
    {
        for (int column = 0; column < 10; column++)
        {
            if (seatingChart[row][column] == 1 || seatingChart[row][column] == 2 || seatingChart[row][column] == 3 || seatingChart[row][column] == 4 || seatingChart[row][column] == 5)
            {
                total = total + seatingChart[row][column];

                cout << "You purchased: Seat: " << column + 1 << " Row: " << row + 1 << " for $" << seatingChart[row][column] * 10 << endl;
            }
        }
    }

    cout << endl << "Your purchase total is: $" << total * 10 << endl << "\n" << endl;

    return 0;
}
\$\endgroup\$
2
  • \$\begingroup\$ We can only give advice on code written not on code not written as its off-topic here.Can you rephrase your question to reflect only improvements \$\endgroup\$
    – Tolani
    Nov 10, 2016 at 18:38
  • \$\begingroup\$ @TolaniJaiye-Tikolo I edited my post. I believe I am only asking advice on code that is written. Specifically why my else if statement is not executing when a seat at a certain price is no longer available and on what code I have written that could be implemented into a function. Hopefully this clarifies \$\endgroup\$
    – RootFire
    Nov 10, 2016 at 18:47

2 Answers 2

1
\$\begingroup\$

A main point of creating a function is to prevent duplicate code(seems like you've already realized that). So where does it seem like you're writing the same thing multiple times? When searching for a seat based off the price, you're doing the same thing under each if or else if statement. The only thing that changes is the value you're searching for. In this case, you could create a function that takes the value as a parameter and does the searching.

The reason why you're else if statement isn't executing is because the condition for that isn't met. My suggestion is to run through your code yourself and follow the "found" variable closely. I think you'll find the bug by doing that.

Going back to the point of creating functions. It's useful to create functions, because you can test small portions of your code separately(unit testing), and have an easier time debugging.

Good luck!

\$\endgroup\$
0
\$\begingroup\$

In line with the comments about using functions, one option you have is to create a copy of the seatingchart and change any the value of any seat that's not offered at a particular price to, say, a -1(this should work well since no seat will be offered at a negative price). Then if you have a print function that accepts the array you can print out all the seats or just the ones at that price:

const int ROWS = 9;
const int SEATS = 10;
array<array<int, SEATS>, ROWS> seatingChart =
{ {
    {{ 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 }},

} };
//By creating a copy and changing it, you get the option of saving it, if you find it
//necessary.
array<array<int, SEATS>, ROWS> GetAvailableSeats(int price)
{
    array<array<int, SEATS>, ROWS> outVal = seatingChart;
    for (int row = 0; row < ROWS; row++)
    {
        for (int seat = 0; seat < SEATS; seat++)
        {
            if (outVal[row][seat] != price)
            {
                outVal[row][seat] = -1;
            }
        }
    }
    return outVal;
}
void PrintSeatingChart(const array<array<int, SEATS>, ROWS> chart, ostream& out)
{
    int numSeats = 0;
    out << "Seats: ";
    for (int seat = 0; seat < SEATS; seat++)
    {
        out << setw(2) << seat << ' ';
    }
    out << '\n';
    for (int row = 0; row < ROWS; row++)
    {
        out << "Row " << row << "  ";
        for (int seat = 0; seat < SEATS; seat++)
        {
            if (chart[row][seat] != -1)
            {
                numSeats++;
                out << setw(2) << chart[row][seat] << ' ';
            }
            else
            {
                out << "-- ";
            }
        }
        out << '\n';
    }
    if (numSeats == 0)
    {
        out << "Sorry there are no seats available.\n";
    }
    else
    {
        out << "There are " << numSeats << " seats available\n";
    }

}

int main()
{
    PrintSeatingChart(seatingChart, cout);
    PrintSeatingChart(GetAvailableSeats(40),cout);
    //to prove that the original seating chart isn't changed.
    PrintSeatingChart(seatingChart, cout);
    return 0;
}

I changed the array to the C++11 array, which I find easier to use in many respects.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.