代码不会处理任何异常

时间:2018-05-05 02:12:19

标签: c++

#include<iostream>
#include<string>
using namespace std;
class nonIntegerException : public runtime_error
{
    private:
    string nonIntegerData;
    public:
    nonIntegerException(string);
    string getNonIntegerData();
};
class RealEstate
{
    friend ostream& operator<<(ostream&, const RealEstate);
    friend istream& operator>>(istream&, RealEstate&);
    private:
    int housePrice;
    int numBeds;
    int numBaths;
};
nonIntegerException::nonIntegerException(string s) : runtime_error("Non integer data")
{
    nonIntegerData = s;
}
string nonIntegerException::getNonIntegerData()
{
    return nonIntegerData;
}
ostream& operator<<(ostream& out, const RealEstate house)
{
    out << "Price $" << house.housePrice << ". " << house.numBeds << " bedrooms and " << house.numBaths << " bathrooms." << endl;
}
istream& operator>>(istream& in, RealEstate& house)
{
    cout << "Enter the price. " << endl;
    in >> house.housePrice;
    if(house.housePrice < 0)
    throw("Price is negative");
    cout << "Enter the number of bedrooms" << endl;
    in >> house.numBeds;
    if(house.numBeds < 0)
    throw("Number of bedrooms is negative");
    cout << "Enter the number of bathrooms" << endl;
    in >> house.numBaths;
    if(house.numBaths < 0)
    throw("Number of bathrooms is negative");
}
int main()
{
    RealEstate myHouse;
    try
    {
        if(!(cin >> myHouse))
        nonIntegerException e("nonIntegerData");
    }
    catch(const string MSG)
    {
        cout << MSG << endl;
        printf("Please re-enter the values");
        cin >> myHouse;
    }
    catch(nonIntegerException e)
    {
        cout << "Please enter all integers" << endl;
        cout << "You entered " << e.getNonIntegerData() << endl;
        cin >> myHouse;
    }
    cout << myHouse;
    return 0;
}

该程序应该允许用户输入房屋的数据并将其打印回房屋。它也应该能够处理异常。 但是,它不会处理任何异常。

当我输入一个负值时,它输出:&#34;在抛出一个&#39; char const *&#39;中止&#34 ;.它应该说&#34;请输入所有正整数值&#34;。当我尝试让它处理一个非整数异常时,它输出:&#34;分段错误&#34;。它应输出错误消息,然后重定向用户再次输入值。

如何修复此错误?谢谢你的时间。

 main.cpp: In function 'void inputInteger(std::istream&, int&, const 
 char*)':
 main.cpp:42:19: error: 'numeric_limits' is not a member of 'std'
     in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
               ^
 main.cpp:42:54: error: expected primary-expression before '>' token
     in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                                                  ^
 main.cpp:42:61: error: no matching function for call to 'max()'
     in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                                                         ^
   In file included from /usr/include/c++/5/bits/char_traits.h:39:0,
             from /usr/include/c++/5/ios:40,
             from /usr/include/c++/5/ostream:38,
             from /usr/include/c++/5/iostream:39,
             from main.cpp:1:
 /usr/include/c++/5/bits/stl_algobase.h:219:5: note: candidate: 
 template 
 const _Tp& std::max(const _Tp&, const _Tp&)
 max(const _Tp& __a, const _Tp& __b)
 ^
 /usr/include/c++/5/bits/stl_algobase.h:219:5: note:   template 
 argument 
 deduction/substitution failed:
 main.cpp:42:61: note:   candidate expects 2 arguments, 0 provided
     in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                                                         ^
 In file included from /usr/include/c++/5/bits/char_traits.h:39:0,
             from /usr/include/c++/5/ios:40,
             from /usr/include/c++/5/ostream:38,
             from /usr/include/c++/5/iostream:39,
             from main.cpp:1:
 /usr/include/c++/5/bits/stl_algobase.h:265:5: note: candidate: 
 template const _Tp& std::max(const _Tp&, const _Tp&, _Compare)
 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
 ^
/usr/include/c++/5/bits/stl_algobase.h:265:5: note:   template argument 
deduction/substitution failed:
 main.cpp:42:61: note:   candidate expects 3 arguments, 0 provided
     in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                                                         ^

1 个答案:

答案 0 :(得分:0)

  • 第一个问题应该是catch(const char* MSG)
  • 无法捕获第二个catch因为你没有抛出异常,它应该是throw nonIntegerException("nonIntegerData");
  • 以及其他一些问题

检查修改后的来源:

#include<iostream>
#include<string>
#include<limits>
using namespace std;
class nonIntegerException : public runtime_error
{
private:
    string nonIntegerData;
public:
    nonIntegerException(string);
    string getNonIntegerData();
};
class RealEstate
{
    friend ostream& operator<<(ostream&, const RealEstate);
    friend istream& operator >> (istream&, RealEstate&);
private:
    int housePrice;
    int numBeds;
    int numBaths;
};
nonIntegerException::nonIntegerException(string s) : runtime_error("Non integer data")
{
    nonIntegerData = s;
}
string nonIntegerException::getNonIntegerData()
{
    return nonIntegerData;
}

ostream& operator<<(ostream& out, const RealEstate house)
{
    out << "Price $" << house.housePrice << ". " << house.numBeds << " bedrooms and " << house.numBaths << " bathrooms." << endl;
    return out;
}

static void inputInteger(istream& in, int& intVal, const char* errMsg)
{
    if (!(in >> intVal))
    {
        // Reset the cin when failed
        in.clear();
        in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        throw nonIntegerException("nonIntegerData");
    }
    else if (intVal < 0)
        throw errMsg;
}

istream& operator >> (istream& in, RealEstate& house)
{
    cout << "Enter the price. " << endl;
    inputInteger(in, house.housePrice, "Price is negative");
    cout << "Enter the number of bedrooms" << endl;
    inputInteger(in, house.numBeds, "Number of bedrooms is negative");
    cout << "Enter the number of bathrooms" << endl;
    inputInteger(in, house.numBaths, "Number of bathrooms is negative");
    return in;
}

int main()
{
    RealEstate myHouse;
    while (true)
    {
        try
        {
            cin >> myHouse;

            // Break if not exception occurs
            break;
        }
        catch (const char* MSG)
        {
            cout << "Exception occurs: ";
            cout << MSG << endl;
        }
        catch (nonIntegerException& e)
        {
            cout << "Please enter all integers" << endl;
            cout << "You entered " << e.getNonIntegerData() << endl;
        }
    }
    cout << myHouse;
    return 0;
}