How can I use setfill() properly in C++?
<p>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.</p>

<p>I need to format the cout time to 01:01:01 if necessary, however, my output is 1:1:1</p>

<p>The problem is here:</p>

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

<p>This is the entire code.</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
#include &lt;cstring&gt;
#include &lt;iomanip&gt;


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-&gt;hours * hourSeconds) - (t1-&gt;hours * hourSeconds));
    int timeHour = hourDiff / hourSeconds;
    long minDiff = ((t2-&gt;minutes * minSeconds) - (t1-&gt;minutes * minSeconds));
    int timeMin = minDiff / minSeconds;
    int timeSec = (t2-&gt;seconds - t1 -&gt; seconds);
    MyTime time;
    time.hours = timeHour;
    time.minutes = timeMin;
    time.seconds = timeSec;
    return time;
}

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

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

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


    return 0;

}
</code></pre>

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