2016-10-03 18 views
-1

私は座っているチャートプログラムで作業しています。プログラムの目的は、座席価格の座席表を表示し、ユーザーが特定の座席または座席を価格に基づいて選択できるようにすることです。私はかなり例外なくいくつかの例外を除いて正常に動作しています。初心者のC++は繰り返しを避けるif/else if文

私はここにこれらの例外の1つに関する質問を掲載しましたが、まず各価格の繰り返しコードの問題に対処する必要があることをすぐに知らされました。私はすべての価格帯でこれを実装するコードの単一の塊を理解する必要があります。私はそれを見てきましたが、どこから始めるべきかは十分にはわかりません。助言がありますか?

#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

あなたは '||'演算子について知っているようです。入力された価格がいくつかの受け入れ可能な価格の1つであることを確認するための単一の 'if()'ステートメントを持つ方法を考えることができます。もしそうなら、価格を指定した変数を参照して、 ?価格が10か***か*** 20か***か*** 30かどうかをチェックするif()文は1つだけです... –

+0

'switch'文またはテーブルを試してみてください。 –

答えて

2

あなたはすでに必要なものすべてを知っています。

シートの価格が有効である場合、チェック||を使用する:

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

及びシートの価格は、ユーザが入力した価格に等しい場合代わり固定数10の(seatingChart[row][column] == seatPriceを使用し、チェックするために、20 、30,40、または50)。

+0

ありがとうございます。幸いなことに、私は一度戻って考えてみたら、私自身でこれを理解することができました – iriejams