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

时间:2016-10-03 03:14:17

标签: c++ if-statement repeat

我正在制作座位表计划。该计划的目的是显示座位价格的座位表,并允许用户根据价格选择特定座位或座位。除了少数例外,我几乎让它正常工作。

我在这里发布了一个关于其中一个例外的问题,但很快就被告知,首先我需要解决每个价格的重复代码问题。我需要弄清楚一个代码块,它将在每个价格范围内实现这一点。我已经查看了但不太确定从哪里开始。有什么建议吗?

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

1 个答案:

答案 0 :(得分:2)

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

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

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)。