1

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?

ThrawnProg
  • 23
  • 3
  • 1
    This has nothing to do with overloading the operator. It's the same as trying to do specially-formatted input in `main`. `~` is not a valid integer. – chris Feb 18 '15 at 17:42
  • it's a requirement for my assignment, this isn't all of the code I have but I figured this would give an idea of what I have to do, the instructions say:Create an overload of the extraction operator >> for reading Time objects from an input stream. The format for the input of a Time object is the same as the output format listed above. This operator will need to do some error checking, as well. – ThrawnProg Feb 18 '15 at 17:46
  • I should have clarified. The *problem you're having* has nothing to do with the overload. That is, you could post a complete example consisting of just a `main` function, it would exhibit the same problem, and we wouldn't even have to know the overload exists because it's not relevant (though it could be mentioned for context). – chris Feb 18 '15 at 17:48
  • The point is that when you do the reading from `cin` you must take care to read the `~` and `:` characters as necessary, and verify that what you read is indeed what you expected. – T.C. Feb 18 '15 at 18:01
  • okay, yea I thought it would just read the integers, I forgot I have to ignore those values. I did that and they all have a value now but double digit figures only show up as 0 and the last digit, should be much easier to work with now though, thanks! – ThrawnProg Feb 18 '15 at 18:15

0 Answers0