2016-10-03 84 views
-1

我正在制定座位表程序。該計劃的目的是顯示座位價格的座位表,並允許用戶選擇特定的座位或基於價格的座位。除了少數例外,我幾乎可以正常工作。初學C++避免重複的if/else if語句

我在這裏發佈了關於其中一個例外的問題,但很快就被告知首先我需要解決每個價格的重複代碼問題。我需要弄清楚一個代碼塊,它將在每個價格範圍內實現這一點。我已經看過它,但不知道從哪裏開始。有什麼建議麼?

#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

在我看來,你知道所有關於'||'操作符。你能想出一種方法來讓一個'if()'語句檢查輸入的價格是幾個可接受的價格之一,如果是,執行其餘的邏輯,指的是指定價格的變量?只需一個'if()'語句來檢查價格是10,***還是*** 20,***或*** 30,等等...... –

+0

您可能想給'switch'語句或桌子試駕。 –

回答

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

+0

謝謝。幸運的是,一回到我的腦海中,我就能夠自己想出這件事了 – iriejams