我对C ++比较陌生,并且无法编写管理剧院门票的程序。我尝试自己测试代码的每个部分以找到问题,但找不到问题。我也尝试寻找答案,但找不到任何似乎解决这个问题。如果用户选择选项2,则TicketRequest功能应该验证座位的可用性。如果任何请求的席位不存在或不可用,则应返回相应的消息以供客户端程序显示。
我对此功能有一些问题:
以下是TicketRequest函数的代码行:
string TicketManager::TicketRequest(int seatsRequested, int rowRequested, int seatNumber)
{
bool displayInfo = false;
ostringstream os;
os << fixed << showpoint << setprecision(2);
if (seatsRequested < 1 || seatsRequested > 30 || rowRequested < 1 || rowRequested > 15
|| seatNumber < 1 || seatNumber > 30)
{
os << "\nInvalid entry. The seats requested do not exist.\n";
}
for (int count = 0; count < seatsRequested; count++)
{
seatNumber++;
if (SeatStructures[rowRequested - 1][seatNumber - 1] == '#')
{
cost = seatsRequested * price[rowRequested - 1];
displayInfo = true;
}
else
{
return string ("\nSorry, your requested seat(s) is unavailable.\n\n");
}
}
if (displayInfo == true)
{
os << "\nRequested seats: ";
os << seatsRequested;
os << "\nPrice per seat: $";
os << price[rowRequested - 1];
os << "\nTotal cost: $";
os << cost;
}
return os.str();
}
我将整个程序粘贴到pastebin中,所以这篇文章不会太长。这是链接:http://pastebin.com/ZyZskG4S
答案 0 :(得分:0)
cout << TicketManager1.TicketRequest(seatsRequested, rowRequested, seatNumber);
如果票证可用,此行可以返回true
值,如果可以,则可以
bool isTicketAvailable = false;
TicketManager1.TicketRequest(seatsRequested, rowRequested,seatNumber, isTicketAvailable);
if (isTicketAvailable)
{
cout << "\n\nDo you wish to purchase these seats? (Y/N)";
cin >> confirmation;
//do whatever you need here
}
函数参数应如下所示
string TicketManager::TicketRequest(int seatsRequested, int rowRequested, int seatNumber, bool& isTicketAvailable)
答案 1 :(得分:0)
假设如下:
struct SeatStructures[TotalRows][TotalSeatsInARows];
with TotalRows = 15 and TotalSeatsInARows = 30
现在,如果函数被调用TicketRequest(int seatsRequested, int rowRequested, int seatNumber)
,则以下是参数的有效值:
1 <= rowRequested <= 15
1 <= seatsRequested <= 30
1 <= seatNumber <= 30
seats will be allocated from seatNumber onwards (i.e inclusive of seatNumber)
现在,除了检查参数的有效值之外,还可以进行一次检查:
1 <= (seatNumber + seatsRequested - 1) <= 30
现在,可以检查函数TicketRequest
返回的值,看它是否包含字符串“ERROR”,因为您无法将返回值修改为bool
。
这是功能:
string TicketManager::TicketRequest(int seatsRequested, int rowRequested, int seatNumber)
{
if((1 < seatsRequested) || (seatsRequested > 30))
{
return string("ERROR: Invalid number of seats requested.\n");
}
if((1 < rowRequested) || (rowRequested > 15))
{
return string("ERROR: Invalid row number requested.\n");
}
if((1 < seatNumber) || (seatNumber > 30))
{
return string("ERROR: Invalid seat number requested.\n");
}
//seat number should be between [1 and 30] (both inclusive)
//(seatNumber + seatsRequested - 1) should be equal to last valid seat number
if((1 < (seatNumber + seatsRequested - 1)) || ((seatNumber + seatsRequested - 1) > 30)) &&
{
return string("ERROR: Invalid seat number or number of seats requested.\n");
}
//if you come till here then all the parameters are valid, so just check the availability of seats.
int cost = 0;
for (int sn = seatNumber; sn <= (seatNumber + seatsRequested - 1); sn++)
{
if (SeatStructures[rowRequested - 1][sn - 1] == '#')
{
//seat is available
cost += price[rowRequested - 1];
}
else
{
return string("ERROR: Sorry, your requested seat(s) is/are unavailable.\n\n");
}
}
//if you come till here then all the seats are available.
ostringstream os;
os << fixed << showpoint << setprecision(2);
os << "\nRequested seats: ";
os << seatsRequested;
os << "\nPrice per seat: $";
os << price[rowRequested - 1];
os << "\nTotal cost: $";
os << cost;
return os.str();
}
现在调用该函数并检查返回的值:
TicketManager obj;
string szResult = obj.TicketRequest(45, 3, 2);
if(szResult.find("ERROR") != string::npos)
{
//error
cout << "szResult << endl;
}
else
{
//success
}