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

Beginner C++ avoiding repetitive if/else if statements

本文关键字:if else 语句 c++ 初学者      更新时间:2023-10-16

我正在做一个座位表程序。该程序的目的是显示带有座位价格的座位表,并允许用户选择特定的座位或基于价格的座位。除了一些例外,我基本上已经使它正常工作了。

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

#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: nn" << 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 seatn";
    cout << "Press (P) to select a seat based on pricen";
    cout << "Press (Q) to quitnn";
    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;
}

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

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

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