重载>> c ++中的Date类的运算符会导致无限循环 [英] Overloading >> operator for Date class in c++ causes infinite loop

查看:168
本文介绍了重载>> c ++中的Date类的运算符会导致无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图重载一个C ++中的Date类的>>运算符,但是当运行进入第一个if语句时,它将进入无限循环,你能帮助我吗?

I'm trying to overload the >>operator for a Date class in c++ but the it goes in infinite loop when the run goes into the first if statement, can you please help me?

//operator
    istream& operator >>(istream& is,CustomDate& d){
        int day,month,year; 
        char ch1,ch2;
        string test;
        is>>skipws>>day>>ch1>>month>>ch2>>year;
        if(!is){
            is.clear(ios_base::failbit);
            return is;
        }
        if(ch1!='/' || ch2!='/')
            error("invalid date input");
        d = CustomDate(day,month,year);
        return is;
    }

这是调用它的函数

CustomDate Menu::inputDate(){
    CustomDate date;
    cout<<"Input your departure date"<<endl;
    cin>>date;
    if(!cin){
        error("invalid date format");
    }
    return date;
}

这是调用函数

do{
    try{
        date = inputDate();
        validDate = true;
    }
    catch(runtime_error& e){
        cout<<"Date format not valid! must input as dd/mm/yyyy!"<<endl;
        validDate = false;
    }
}while(!validDate);

//customdate constructor
CustomDate::CustomDate()
    :day(1),month(1),year(2012){}

CustomDate::CustomDate(int day, int month, int year)
    :day(day),month(month),year(year){

    if(day<0 || day>30)
        error("Error: Date constructor");
    if(month<0 || month>12)
        error("Error: Date constructor");
    if(year<0)
        error("Error: Date constructor");
}


推荐答案

正如我在评论中所说:


你的意思是什么clear()函数应该清除流?它不会丢弃流内容,所以如果流中有垃圾邮件(例如不能被解析为int的字符a),它将永远不会清除该垃圾,并将继续重试。我认为问题是清楚不会做你认为的事情。

What do you mean by "the clear() function should clear the stream"? It doesn't discard the stream contents, so if there's junk in the stream (such as the character 'a' that can't be parsed as an int) it will never "clear" that junk and will just keep retrying. I think the problem is that clear doesn't do what you think it does.

而不是从流提取算子中抛出异常只有在不能提取整数或分隔符不正确的情况下(也尝试使用更多的空格来帮助使代码更易于阅读)。

Rather than throwing an exception from the stream extraction operator just the failbit if it can't extract integers or the separator characters are wrong (also try using more whitespace to help make the code more readable):

istream& operator >>(istream& is, CustomDate& d){
    int day, month, year;
    char ch1, ch2;
    if (is >> day >> ch1 >> month >> ch2 >> year)
    {
        if (ch1 == '/' && ch2 == '/')
            d = CustomDate(day, month, year);
        else
            is.setstate(ios::failbit);
    }
    return is;
}

然后在 inputDate

CustomDate inputDate(){
    CustomDate date;
    cout << "Input your departure date" << endl;
    if (cin >> date)
      return date;

    // something went wrong
    if (cin.eof())
        error("No more data in stream");
    else // cin.fail() is true, probably invalid date format
    {
        // clear error and discard input up to next newline
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        error("invalid date format");
    }
}

这篇关于重载&gt;&gt; c ++中的Date类的运算符会导致无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆