【发布时间】:2016-12-16 04:04:48
【问题描述】:
我对 C++ 比较陌生,并且在编写管理剧院门票的程序时遇到了困难。我尝试自己测试代码的每个部分以找到问题,但找不到问题。我也尝试寻找答案,但找不到任何似乎可以解决此问题的方法。如果用户选择选项 2,TicketRequest 函数应该验证座位的可用性。如果任何请求的席位不存在或不可用,则应返回适当的消息以由客户端程序显示。
我对这个函数有一些问题:
- 有时它会显示座位信息(请求的座位数量、每个座位的价格、总费用),并且它总是询问用户是否要购买门票,即使座位不存在也是如此。
- 当我从座位开始请求第 15 排的 30 个座位(最大值)时
- 当一些请求的席位存在并且可用但其他席位不可用时,它的行为就像所有席位都可用,但如果并非所有席位都可用,则应显示为不可用。目前,它打印的票的座位号超出了数组中现有的数量。
以下是 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
【问题讨论】:
标签: c++ arrays visual-studio loops