为什么班级计数器没有增加Coach课程?

时间:2016-03-20 04:07:52

标签: c++ project

好吧我的问题是,每当我运行代码并在第5 - 9行中选择任何内容时,它都不会更新教练计数器,但是Buisness类计数器请帮忙!

//arrays for airline problem

const int ROW = 9;
const int COL = 4;
const int CTR = 3;

//initial seats in the plane
    char layout[ROW][COL] = {  { 'A', 'B', 'C', 'D' },
                            { 'A', 'B', 'C', 'D' },
                            { 'A', 'B', 'C', 'D' },
                            { 'A', 'B', 'C', 'D' },
                            { 'A', 'B', 'C', 'D' },
                            { 'A', 'B', 'C', 'D' },
                            { 'A', 'B', 'C', 'D' },
                            { 'A', 'B', 'C', 'D' },
                            { 'A', 'B', 'C', 'D' }};

    int classCtr[CTR] = {0,0,0};

    string classes[] = {"First Class", "Business Class", "Coach"};

    double fare [] = {500, 300, 100};

头文件:

   1   A   B   C   D
   2   A   B   C   D
   3   A   B   C   D
   4   A   B   C   D
   5   A   B   C   D
   6   A   B   C   D
   7   A   B   C   D
   8   A   B   C   D
   9   A   B   C   D
Enter row <-1 to stop>  5
Enter your prefered seat  a

        Chesapeaake Airlines

   1   A   B   C   D
   2   A   B   C   D
   3   A   B   C   D
   4   A   B   C   D
   5   X   B   C   D
   6   A   B   C   D
   7   A   B   C   D
   8   A   B   C   D
   9   A   B   C   D
Enter row <-1 to stop>  7
Enter your prefered seat  c

        Chesapeaake Airlines

   1   A   B   C   D
   2   A   B   C   D
   3   A   B   C   D
   4   A   B   C   D
   5   X   B   C   D
   6   A   B   C   D
   7   A   B   X   D
   8   A   B   C   D
   9   A   B   C   D
Enter row <-1 to stop>  9
Enter your prefered seat  d

        Chesapeaake Airlines

   1   A   B   C   D
   2   A   B   C   D
   3   A   B   C   D
   4   A   B   C   D
   5   X   B   C   D
   6   A   B   C   D
   7   A   B   X   D
   8   A   B   C   D
   9   A   B   C   X
Enter row <-1 to stop>  -1
   Total Seats =  3
   Percent Occupied = 8.33
        Ticket Price       Total Sales

   First Class  500.00     0

Business Class  300.00     3 <------See this is where the rows are being updated 

         Coach  100.00     0 <---- but i need rows 5 - 9 to be updated here
Have a nice day!
Press any key to continue . . .

这是我运行代码时的输出         切萨皮亚克航空公司

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Form control: select</h2>
  <p>The form below contains two dropdown menus (select lists):</p>
  <form role="form">
    <div class="form-group">
      <label for="sel1">Select list (select one):</label>
      <select class="form-control input-small" id="sel1">
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
      </select>
      <br>
      <label for="sel2">Mutiple select list (hold shift to select more than one):</label>
      <select multiple class="form-control" id="sel2">
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
        <option>5</option>
      </select>
    </div>
  </form>
</div>

</body>
</html>

1 个答案:

答案 0 :(得分:2)

你不能缩写像这样的复合条件:

else if (row == 2 || 3 || 4)

你需要写:

else if (row == 2 || row == 3 || row == 4)

否则,该语句被解释为“如果row == 2为真或3为真或4为真”。当将整数解释为布尔值时,C ++认为零为假,非零数字为真,因此您的原始语句等同于

else if ((row == 2) || true || true)

你可能会注意到,这总是正确的!

您可能还想考虑编写语句的替代方法,如:

else if (row >= 2 && row <= 4)
如果数字范围很大,

可能比列出每个可能的数字要紧凑得多。

相关问题