为什么 class 计数器不增加 Coach class?

Why doesn't the class counter increment the Coach class?

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

#include<iostream>
#include<string>
#include <iomanip>
using namespace std;

#include "C:\Users\barta\OneDrive\Documents\Visual Studio 2015\Projects\Project 4\Project 4\array.h"

void displayPlane(char msg[], char[ROW][COL]);
void getData(int &, char &);
void salesReport(int classCtr[],double fare[], string classes[]);

int main()
{
    int row = 0;
    char seat;


    while (row != -1)
    {

        displayPlane("\tChesapeaake Airlines", layout);
        cout << endl;
        getData(row, seat);
        if (row == -1)
            break;
        cout << endl;
        int COL = seat - 'A';
        if (layout[row - 1][COL] == 'X')
        {

            cout << "Sorry this seat is taken" << endl;
        }
        else
        {

            layout[row - 1][COL] = 'X';

        }

    }   
    double percentOccupied;
    std::cout << std::fixed;
    std::cout << std::setprecision(2);
    int total = 0;
    for (int i = 0; i < 2; i++)
    {
        total += classCtr[i];
    }

    cout << setw(18) << "Total Seats =  " << total << endl;
    percentOccupied = ((double)total / 36) * 100;
    cout << setw(22) << "Percent Occupied = " << percentOccupied << endl;

    salesReport(classCtr, fare, classes);
    cout << endl;
    cout << "Have a nice day! " << endl;

    system("pause");
    return 0;
}
void displayPlane(char msg[] , char[ROW][COL])
{
    cout <<msg << endl;
    for (int r = 0; r < ROW; r++)
    { cout << endl;
    cout << setw(4) << r + 1;
        for (int c = 0; c < COL; c++)
        {
            cout << setw(4) << layout[r][c];
        }

    }

}
void getData(int& row, char& seat)
{
        cout << "Enter row <-1 to stop>  ";
        cin >> row;
        if (row == -1)
            return;
        if (row == 1)
        {
            classCtr[0]++;
        }
        else if (row == 2 || 3 || 4)
        {
            classCtr[1]++;
        }
        else if (row == 5 || 6 || 7 || 8)
        {
            classCtr[2]++;
        }

        cout << "Enter your prefered seat  ";
        cin >> seat;
        seat = toupper(seat);


}
void salesReport(int classCtr[], double fare[], string classes[])
{

    cout << " \tTicket Price" << setw(20) <<"Total Sales\n\n"
        << setw(14) << classes[0] << "\t" << fare[0] << setw(6) <<classCtr[0] << "\n\n"
        << classes[1] << "\t" << fare[1] << setw(6) <<classCtr[1] <<"\n\n"
        << setw(14) << classes[2] << "\t" << fare[2] << setw(6) << classCtr[2];
}

头文件:

//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 . . .

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

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

你需要写:

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

否则,语句被解释为"if row == 2 is true or 3 is true or 4 is true"。将整数解释为布尔值时,C++ 认为零为假,非零数字为真,因此您的原始语句等同于

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

您可能会注意到,这始终是正确的!

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

else if (row >= 2 && row <= 4)

如果数字范围很大,这比列出每个可能的数字要紧凑得多。