Overloading Extraction operator

删除回忆录丶 提交于 2020-01-06 03:17:25

问题


For a hw assignment for a Time class I have the instructions to overload the extraction operator, however the input format has to be the same as the output which is (days~HH:MM:SS). This is what i have for the operator: header file

friend ostream& operator<<(ostream& out, const Time& t);
friend istream& operator>>(istream& in, Time& t);

cpp file

istream& operator>>(istream& in,  Time& t)
{
    in >> t.day;
    in >> t.hour;
    in >> t.minute;
    in >> t.second;

    if (t.day < 0 || t.hour < 0 || t.minute < 0 || t.second < 0)
    {
        t.day = t.hour = t.minute = t.second = 0;
    }
    //else




    return in;
}

main file for output

cout << "Enter first Time object (DAYS~HH:MM:SS): "; cin >> t1;

cout << t1;

when i go to output a Time object, however, it only prints out days and 00:00:00 afterwards, as if I didn't get the rest of them. How can I get days into t.day, HH into t.hour, etc... The examples in my book show distance examples but none where I have to extract from a stream, they normally ask for the input of each separate part. How can I do this all at once from the format I listed?

来源:https://stackoverflow.com/questions/28590219/overloading-extraction-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!