我是这个论坛的新手。任何关于代码,布局或任何其他相关的建设性批评都非常受欢迎!
我遇到的问题是(正如标题所示)将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
答案 0 :(得分: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;
}
}
}
答案 1 :(得分:0)
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].fullName
任何这些读取都会等待您输入内容,因此只要您输入第二个名称,它就会默默地返回等待fullName
。
您已拥有此代码:
//Create full name for pass to asign to seat
fullName = fName + " " +sName;
但它在循环之前,尚未为fName
和sName
分配任何值。
答案 2 :(得分:0)
在您回答“请输入您的第二个名字”后,您的程序仍在等待输入:
cin >> seatArray[i][j].fullName;
答案 3 :(得分:0)
你想用这条线做什么:
cin >> seatArray[i][j].fullName;
为什么用户在输入姓名后输入他们的全名?或者也许是我的误会。正如@ahenderson指出的那样,只需在你的.h文件中声明SeatPlan并在.cc中定义方法。否则它是内联的,这意味着它将复制代码,无论此.h文件包含在项目中的哪个位置。从空间的角度来看效率低下。通常它很好地内联非常小的函数,只返回一些东西或进行非常小的计算。