因输入而改变占用率并根据容量进行检查

时间:2013-11-19 16:26:04

标签: c++

这是我的问题我设置了这个程序,我需要它做的事我无法理解。我需要创建一个参数方法Change_Status,它将房间的状态更改为其参数的值。该方法应验证参数值是否超过房间容量。如果是,则该方法应返回-1。这是我写的代码

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>


using namespace std;

class HotelRoom
{
 private:
string room_no;
int capacity;
int occupancy;
double rate;
   public:
HotelRoom();
HotelRoom(string, int,double, int);
string get_number();
int get_capacity();
int get_status();
double get_rate();
void change_rate(double);
bool change_status(int);

};  
    HotelRoom::HotelRoom (string room, int cap,double rt, int occup)
{
room_no = room;
capacity = cap;
occupancy = occup;
rate = rt;
}
string HotelRoom::get_number()
{
return room_no;
}
int HotelRoom::get_capacity()
{
return capacity;
}
int HotelRoom::get_status()
{
return occupancy;
}
double HotelRoom::get_rate()
{
return rate;
}
void HotelRoom::change_rate( double amount)
{
rate += amount;
}
bool HotelRoom::change_status(int occupancy)
{
bool result;
if (capacity <= occupancy )
{ occupancy = capacity ;
result = true;
}
else
    result = false;
return result;
 }


   int _tmain(int argc, _TCHAR* argv[])

{

cout << setprecision(2)
     << setiosflags(ios::fixed)
     << setiosflags(ios::showpoint);





int occupancy;
double amount;
string room = "123";
int capacity = 4;
double rate = 150.00;


cout << endl;
cout << "enter the number of guests: ";
cin >> occupancy;

HotelRoom guest ( room, capacity, rate, occupancy);

cout << endl;
cout << " Room number is " << guest.get_number() << endl << endl;
cout << " Room Capacity is " << guest.get_capacity () << endl << endl;
cout << " Room Rate is " << guest.get_rate() << endl << endl;
cout << " Room Occupancy is " << guest.get_status() 
     << endl << endl;

cout << endl;
cout << "enter the number of guests: ";
cin >> occupancy;

cout << endl;
cout << " Room number is " << guest.get_number() << endl << endl;
cout << " Room Capacity is " << guest.get_capacity() << endl << endl;
cout << " Room Rate is " << guest.get_rate() << endl << endl;
cout << " Room Occupancy is " << guest.get_status() 
     << endl << endl;


cout << endl;
cout << " Change room rate: ";
cin >> amount;

guest.change_rate(amount);

cout << endl;
cout << " Room number is " << guest.get_number() << endl << endl;
cout << " Room Capacity is " << guest.get_capacity() << endl << endl;
cout << " Room Rate is " <<  amount << endl << endl;
cout << " Room Occupancy is " << guest.get_status()
     << endl << endl;


    cout << endl;
cout << "enter the number of guests: ";
cin >> occupancy;
cout << endl;
cout << " Room number is " << guest.get_number() << endl << endl;
cout << " Room Capacity is " << guest.get_capacity() << endl << endl;
cout << " Room Rate is " <<  amount << endl << endl;
cout << " Room Occupancy is " << guest.get_status()
     << endl << endl;




system("pause");
return 0;
}

1 个答案:

答案 0 :(得分:0)

首先,我相信这是相关的代码?

    bool HotelRoom::change_status(int occupancy)
    {
        bool result;
        if (capacity <= occupancy )
        {
            occupancy = capacity ;
            result = true;
        }
        else
            result = false;
        return result;
    }

这里有一些问题。首先是布尔逻辑被破坏了。您正在检查容量是否小于或等于预期的占用率......它应该是相反的。

第二个问题是,您始终将占用率设置为容量。如果酒店的容量为100,而您致电change_status(50),则入住人数应更改为50,而不是100。

最后一个问题是,当你在函数中设置occupancy时,你指的是变量occupancy,它是函数的一个参数。函数作用域优先于类作用域。因此,您应该为参数指定一个不同的名称,或使用this->occupancy来引用成员变量。所以修改后的版本是:

    bool HotelRoom::change_status(int occupancy)
    {
        if (occupancy <= capacity)
        {
            this->occupancy = occupancy;
            return true;
        }
        else
            return false;
    }