我是这个论坛的新手。任何关于代码、布局或其他相关内容的建设性批评都将受到欢迎!
我遇到的问题是关于(如标题所示)将字符串添加到标准座位预订程序中fullName的数组中。seatArray
我得到的错误是,一旦编译,'please enter your second name'. 编译时调试器中没有显示错误。我还假设这意味着没有任何东西被添加到我的数组中seatArray,所以如果你至少能指出我正确的方向,我将不胜感激!
注意:所有类都是单独的头文件
首先,我的 main.cpp 文件
// Main class - Scotia2
// Main cpp file
#include <iostream>
#include "menu.h"
using namespace std;
int main()
{
Menu m;
m.userMenu();
return 0;
};
我的Menu课(我不确定这与问题是否相关,但无论如何我都会包括在内)
// Menu class - Scotia2
#include <iostream>
#include "flight.h"
using namespace std;
class Menu
{
// Access specifier
public:
    int userMenu()
        {
            Flight f1;
            Booking b1;
            int option;
            cout << "Scotia Airlines\n";
            cout << "\n 1- Check Availability";
            cout << "\n 2- Book a Seat";
            cout << "\n 3- Cancel a Reservation";
            cout << "\n 4- Exit \n ";
            cout << "-------------------\n\n ";
            cin >> option;
            do{
                switch (option)
                {
                    case 1: f1.seatPlan();
                        break;
                    case 2: b1.addBooking();
                        break;
                    //case 3: c1.cancel; // Link to cancel.h
                        //break;
                    case 4: return (0);
                        break;
                }
              }while (option == 0); // End of do-while
                return option;
            cout << "\n";
        }// End of userMenu function
};// End of menu class
我的Flight班级:
// Flight Class - Scotia 2
// Contains information on seating (array), space available and return to menu option.
#include <iostream>
#include <string>
#include "booking.h"
using namespace std;
class Flight
{
public:
    public:
    struct Seat
        {
            int Available;
            string fullName;
        };// End of struct
// Structure for seat plan (24 spaces available)
    struct Seat seatArray[4][6];
    void seatPlan()
    {   //------
        cout << "Scotia Airlines Seating Plan\n";
        cout << "------------------------\n";
        cout << " 1D  2D  3D  4D  5D  6D\n";
        cout << " 1C  2C  3C  4C  5C  6C\n";
        cout << "                       \n";
        cout << " 1B  2B  3B  4B  5B  6B\n";
        cout << " 1A  2A  3A  4A  5A  6A\n";
        cout << "------------------------\n\n\n";
        //------
        for (int i=0;i<4;i++)
        {
            for (int j=0;j<6;j++)
                {
                if (seatArray[i][j].Available == 0)
                cout << seatArray[i][j].fullName;
                else
                cout << "Seating Plan is unavailable";
                }
        }// End of for loop
    }// End of seatPlan function
};// End of Flight class
最后,我的Booking课。我假设这是发生错误的地方。我尝试以不同的方式布置我的代码,因为我认为事情可能没有按正确的顺序执行。然而,这并没有真正奏效。
//Booking class - Scotia Airlines
//This class will reserve a seat for passenger
#include <iostream>
#include <string>
using namespace std;
class Booking{
    public:
    struct Seat
        {
            int Available;
            string fullName;
        };// End of struct
// Structure for seat plan (24 spaces available)
    struct Seat seatArray[4][6];
//variables for taking in customer details, calculating ticket cost (inc discounts) and adding details to system
    public:
    string fName, sName, busName, fullName;
    int age, livesAt;
    float discount, tickPrice, tCost;
    void addBooking()
    {
    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
                            {
                            discount = 0.0;
                            }
    // Calculation - Standard ticket price is 60
                tickPrice = 60.0;
                tCost = (tickPrice * discount);
    //Create full name for pass to asign to seat
                fullName = fName + " " +sName;
    for(int i = 0; i < 4; i++)
     {
         for(int j = 0; j < 6; j++)
         {
             if(seatArray[i][j].Available == 1)
             {
                cout << "Please enter your first name \n";
                cin >> fName;
                cout << "Please enter your second name \n";
                cin >> sName;
                cin >> seatArray[i][j].fullName;
                seatArray[i][j].Available = 0;
             }// end of if
         }// end of nested for
     };//end of for
    // Message on screen for customer displaying cost of flight
        cout << "*******************************\n";
        cout << "\tBooking for " << fullName << " confirmed\n";
        cout << "\tTotal cost = " << tCost << " GBP.\n";
    }// End of addBooking function
};// End of Booking class
我知道代码可能有点草率(相当新手程序员)所以正如我所说,任何提示等都非常受欢迎!#
亲切的问候,
ZM