初学者 C++ 避免重复 if/else if 语句

Beginner C++ avoiding repetitive if/else if statements

我正在研究座位表程序。该程序的目的是显示包含座位价格的座位表,并允许用户 select 指定座位或基于折扣价的座位。除了一些例外,我几乎已经让它正常工作了。

我在此处发布了关于其中一个例外的问题,但很快被告知我首先需要解决每个价格的重复代码问题。我需要找出一个代码块来为每个价格范围实现它。我已经看过了,但不太确定从哪里开始。有什么建议吗?

#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;

        //  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;

                        }
                    }
                }
            }

        }

        //  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;

                        }
                    }
                }
            }


        }

        //  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;

                        }
                    }
                }
            }


        }

        //  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;

                        }
                    }
                }
            }


        }

        //  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;
}

你已经知道你需要的一切。

要检查座位价格是否有效,请使用 ||:

if (seatPrice == 10 || seatPrice == 20 || seatPrice == 30)
{
    find a seat
}
else
{
    tell user that price is invalid
}

并检查座位价格是否等于用户输入的价格,使用 seatingChart[row][column] == seatPrice(而不是固定数字 10、20、30、40 或 50)。