1

我是这个论坛的新手。任何关于代码、布局或其他相关内容的建设性批评都将受到欢迎!

我遇到的问题是关于(如标题所示)将字符串添加到标准座位预订程序中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

4

4 回答 4

0

你想用这条线做什么:

cin >> seatArray[i][j].fullName;

为什么用户在输入名字和姓氏之后还要输入他们的全名?或者,也许我误会了。正如@ahenderson 指出的那样,只需在.h 文件中声明SeatPlan 并在.cc 中定义方法。否则它是内联的,这意味着它会在项目中包含此 .h 文件的任何位置复制代码。从空间的角度来看效率低下。通常可以内联非常小的函数,这些函数只返回一些东西或进行非常小的计算。

于 2012-12-19T15:31:42.733 回答
0

在您使用第二个名称 ( cin >> sName;) 后,您会再次使用 来提示用户cin >> seatArray[i][j].fullName;。您是否打算改为执行以下操作?

cout << "Please enter your first name \n";
cin >> fName;
cout << "Please enter your second name \n";
cin >> sName;
fullName = = fName + " " + sName;
seatArray[i][j].fullName = fullName;

此外,一旦输入了第二个名称,循环将继续,并提示下一个可用座位的名称。您需要设置一个条件,以便for在预订座位后跳出两个循环。你可以这样做:

bool booked = false;
for(int i = 0; i < 4 && !booked; i++)
{
  for(int j = 0; j < 6 && !booked; 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;
      fullName = = fName + " " + sName;
      seatArray[i][j].fullName = fullName;
      booked = true;
    }
  }
}
于 2012-12-19T15:28:46.680 回答
0
            cout << "Please enter your first name \n";
            cin >> fName;
            cout << "Please enter your second name \n";
            cin >> sName;
            cin >> seatArray[i][j].fullName;

这将:

  • 提示输入名字
  • 将单个空格分隔的字符串读入局部变量 fName
  • 提示输入第二个名字
  • 将单个空格分隔的字符串读入局部变量 sName
  • 将另一个以空格分隔的字符串读入seatArray[i][j].fullName

这些读取中的任何一个都将等待您输入某些内容,因此一旦您输入第二个名称,它就会静默返回等待fullName

你已经有了这个代码:

//Create full name for pass to asign to seat
fullName = fName + " " +sName;

但它在循环之前,何时fName并且sName尚未分配任何值。

于 2012-12-19T15:28:58.163 回答
0

在您回答“请输入您的第二个名字”后,您的程序仍在等待输入:

cin >> seatArray[i][j].fullName;
于 2012-12-19T15:29:18.067 回答