【问题标题】:Assign name to seat and display in array为座位分配名称并在数组中显示
【发布时间】:2012-12-19 19:32:25
【问题描述】:

我在将用户分配给数组然后显示数组时遇到问题。典型的“飞行座位分配”计划。任何帮助都会很棒,因为我被严重卡住了。

我在编译时没有遇到任何错误,所以我假设我离得不远?代码如下。

注意,单独的头文件中的类。

// 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 << "=" << i+1;

                else
                cout << "Seating Plan is unavailable";
                break;
                }
        }// End of for loop
    }// End of seatPlan function

};// End of Flight class

这也是我的预订舱位,因为我相信这将有助于发现问题...

//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 = 1.0;
                            };

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


            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;
                                    // Message on screen for customer displaying cost of flight
                                cout << "*******************************\n";
                                cout << "\tBooking for " << fName + " " + sName << " confirmed.\n";
                                cout << "\tTotal cost = " << tCost << " GBP.\n";
                            }//end of if
                        }//end of for2
                    }//end of for1


    }// End of addBooking function
};// End of Booking class

任何帮助将不胜感激!

【问题讨论】:

  • 程序做错了什么?是什么让您提出这个问题?
  • 只有一堆代码和对问题的非常模糊的描述......你尝试调试了吗?
  • break 函数中的 seatPlan 语句看起来不合适。它会导致内部循环只运行一次。
  • 与其使用ij作为变量,我建议将它们重命名为rowcolumn,这样可以让你的程序更容易阅读。

标签: c++ arrays assign


【解决方案1】:

以下是我发现的一些错误:

  1. 首先,您永远不会将座位标记为不可用。将此添加到您的添加嘘声功能中。
  2. 我认为第二个中的第二个 else for in seatPlan 应该在 else 中。
  3. else 语句后不需要半列(在将折扣设置为 1.0 时在 else 中)

因为你从来没有提到你得到的错误是什么,所以这是我能得到的最好的。希望这个答案有帮助。

【讨论】:

  • 对不起各位。我确实忘记强调这个问题。当我调试时,我没有得到错误。这就是它出错的地方。我在这里尝试做的是让乘客预订 24 座飞机上的座位。它接收乘客的详细信息,即名字、姓氏,并将其分配给飞机上的座位。它让我编译和运行,但是当我从菜单中选择查看飞机座位计划时,它是布局,然后错误消息“座位计划不可用”3 次。对不起,我第一次忘记包括所有这些! :(
猜你喜欢
  • 1970-01-01
  • 2021-04-01
  • 2021-04-03
  • 1970-01-01
  • 1970-01-01
  • 2015-09-17
  • 1970-01-01
  • 2021-11-02
  • 1970-01-01
相关资源
最近更新 更多