Q:

How can I use setfill() properly in C++?

So I have been tackling this problem for a while now, and with the help of members, I am almost finished. My last question for this is above.
I need to format the cout time to 01:01:01 if necessary, however, my output is 1:1:1
The problem is here:
std::ostream& operator<< (ostream& os, const MyTime& m)
{
    os << setfill('0') << m.hours << ":" << setfill ('0') << m.minutes << ":" << setfill ('0') << m.seconds;
    return os;
}

This is the entire code.
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <iomanip>

using namespace std;
struct MyTime { int hours, minutes, seconds; };
MyTime DetermineElapsedTime(const MyTime *t1, const MyTime *t2);

const int hourSeconds = 3600;
const int minSeconds = 60;
const int dayHours = 24;
const char zero = 0;

MyTime DetermineElapsedTime(const MyTime *t1, const MyTime *t2)
{
    long hourDiff = ((t2->hours * hourSeconds) - (t1->hours * hourSeconds));
    int timeHour = hourDiff / hourSeconds;
    long minDiff = ((t2->minutes * minSeconds) - (t1->minutes * minSeconds));
    int timeMin = minDiff / minSeconds;
    int timeSec = (t2->seconds - t1 -> seconds);
    MyTime time;
    time.hours = timeHour;
    time.minutes = timeMin;
    time.seconds = timeSec;
    return time;
}

std::ostream& operator<< (ostream& os, const MyTime& m)
{
    os << setfill('0') << m.hours << ":" << setfill ('0') << m.minutes << ":" << setfill ('0') << m.seconds;
    return os;
}

int main(void)
{
    char delim1, delim2;
    MyTime tm, tm2;
    cout << "Input two formats for the time. Separate each with a space. Ex: hr:min:sec\n";
    cin >> tm.hours >> delim1 >> tm.minutes >> delim2 >> tm.seconds;
    cin >> tm2.hours >> delim1 >> tm2.minutes >> delim2 >> tm2.seconds;

    if (tm2.hours <= tm.hours && tm2.minutes <= tm.minutes && tm2.seconds <= tm.seconds)
        {
            tm2.hours += dayHours;
        }
    cout << DetermineElapsedTime(&tm, &tm2);

    return 0;

}

Any insight to solve this issue will be greatly appreciated. If you need any more information please ask.

A:

You need to also call setw to let the stream know it has to pad the output. Keep in mind that in contrast to setfill, setw only applies to the next piece of output.
std::ostream& operator<< (ostream& os, const MyTime& m)
{
    return os << setfill('0') << setw(2) << m.hours << ":"
              << setw(2) << m.minutes << ":"
              << setw(2) << m.seconds;
}

