正如您可能从标题中收集的那样,在文件'booking.h'的构造函数中初始化变量'passNum'时出错

该文件包含航类预订程序的预订类别,我想将passNum初始化为0,因为最初航类没有乘客。但是,我一直收到“飞行已满”的消息,该消息一直供用户查看航类是否还有剩余空间。因此,要查看错误,我添加了一个提示,以查看在passNum中实际存储了什么数字。输出为“28080747”,但是每次编译和运行时,该数字都会更改。

预订文件...

//Booking class - Scotia Airlines
//Zac Mazs
//This class will reserve a seat for passenger


#include <iostream>
#include <string>

#include "flight.h"

using namespace std;

class Booking{

public:
    // default constructor
    Booking()
    {
    int passNum = 0; // initialise passenger number to 0
    totalSeats = 24; // total seats on plane
    fName = "empty";
    sName = "empty";
    busName = "empty";
    }
    // accessor methods

    void setPassNum(int p){passNum = p;}

    string getFname(){return fName;}
    string getSname(){return sName;}
    string getBusName(){return busName;}


    void addBooking()
    {

    if (passNum >= totalSeats) // if the number of passengers exceeds 24 then display flight-full message
        {
            cout << passNum;
            cout <<"Flight is full";
        }
        else
            {
                cout << "\tBooking Menu \n\n";
                cout << "Please select ticket type: \n";
                cout << "1- Business \n";
                cout << "2- Western Isles \n";
                cout << "3- Ordinary \n";
                cin >> livesAt;

                    // This will be used to calc total cost for each passenger dependant on ticket type
                    if(livesAt == 1)
                        {
                        discount = 0.75;
                        cout << "Please enter your business name\n";
                        cin >> busName;
                        }

                        else if (livesAt == 2)
                            {
                            discount = 0.90;
                            }

                        else if(livesAt == 3)
                            {
                            discount = 1.0;
                            }
                        else
                        {
                            cout << "Error, please choose 1,2 or 3";
                        }

                // Calculation - Standard ticket price is 60 Beans
                tickPrice = 60.0;
                tCost = (tickPrice * discount);



            bool booked = false;
                for(int ROW = 0; ROW < 4 && !booked; ROW++)
                    {
                        for(int COL = 0; COL < 6 && !booked; COL++)
                        {
                            if(f.isAvailable(ROW,COL) == true)
                            {
                            cout << "Please enter your first name \n";
                            cin >> fName;
                            cout << "Please enter your second name \n";
                            cin >> sName;
                            //create new string fullName from fName and sName
                            fullName == fName + " " + sName;

                            f.setName(ROW,COL, fullName);
                            f.setAvailable(ROW,COL,0);

                            f.seatArray[ROW][COL].available++;

                            booked = true;
                            // increment pass num/decrement totalSeats
                            totalSeats--;
                            passNum++;

                            // Message on screen for customer displaying cost of flight
                            cout << "*******************************\n";
                            cout << "\tBooking for "; cout << fName + " " + sName; cout << " confirmed.\n";
                            cout << "\tTotal cost = " << tCost << " GBP.\n";

                            }//end of if
                        }//end of for2
                    }//end of for1
            }//end else
    }// End of addBooking function

private:
//Declare all variables

string fName, sName, busName, fullName;
int livesAt, totalSeats;
int passNum;

float discount, tickPrice, tCost;

Flight f;
Seat s;
Passenger p;
};// End of addBooking class

有什么想法吗?我非常感谢您的帮助!

提前致谢。

最佳答案

在构造函数中,passNum在本地声明(它隐藏了私有(private)类级别的定义)。

改成:

Booking()
{
    /*int*/ passNum = 0; //initialise passenger number to 0
    totalSeats = 24; // total seats on plane
    fName = "empty";
    sName = "empty";
    busName = "empty";
}

09-06 22:08