C ++-抛出错误消息

时间:2019-12-03 14:41:38

标签: c++ throw

main()函数应显示适当的 如果为以下任何一个输入了非数字或负值,则会引发错误消息 数据成员。

有人可以帮我编写主要功能吗

这是我的代码

#include <iostream>
using namespace std;

class RealEstate
{
private:
    int price;
    int bedroomNumb;
    int bathsNumb;

public:
    RealEstate(int p, int bed, int bath)
    {
        price = p;
        bedroomNumb = bed;
        bathsNumb = bath;
    }
    friend ostream &operator<<(ostream &out, const RealEstate &r);
    friend istream &operator>>(istream &in, RealEstate &r);
};

istream &operator>>(istream &in, RealEstate &r)
{
    cout << "Enter Real Estate Price: ";
    in >> r.price;
    cout << "Enter Numbers of Bedroom: ";
    in >> r.bedroomNumb;
    cout << "Enter Numbers of Baths: ";
    in >> r.bathsNumb;

    return in;
}

ostream &operator<<(ostream &out, const RealEstate &r)
{
    out << "Price: " << r.price << endl;
    out << "Bedroom: " << r.bedroomNumb << endl;
    out << "Baths: " << r.bathsNumb << endl;

    return out;
}

int main()
{

}

1 个答案:

答案 0 :(得分:0)

据我了解,您希望尝试一下main()并抛出接受用户输入的函数。在这种情况下,您绝对应该在函数中使用throw关键字。在您的主要方面,您会

int main(){
    try{
       function() //note that the function you are calling here should throw an error of type char* and throw this error if the number input by the user is negative and non-numeric. 
    }catch(const char* msg)
}

这是有关如何实现错误处理的简要说明(根据您提供的程序和信息)。

希望这会有所帮助!