如何检查2D数组中是否存在所有请求的值?

时间:2016-12-16 04:04:48

标签: c++ arrays visual-studio loops

我对C ++比较陌生,并且无法编写管理剧院门票的程序。我尝试自己测试代码的每个部分以找到问题,但找不到问题。我也尝试寻找答案,但找不到任何似乎解决这个问题。如果用户选择选项2,则TicketRequest功能应该验证座位的可用性。如果任何请求的席位不存在或不可用,则应返回相应的消息以供客户端程序显示。

我对此功能有一些问题:

  1. 有时会显示座位信息(请求座位数,每个座位的价格,总费用),并且总是询问用户是否想要购买门票,即使座位不存在。
  2. 当我从座位
  3. 开始请求第15行(最大值)中的30个座位时
  4. 当某些请求的座位存在并且可用而不是其他座位时,它就像所有座位都可用一样,但如果并非全部可用,则应显示为不可用。目前,它打印的座位号超出阵列中存在的座位数。
  5. 以下是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

2 个答案:

答案 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
}