Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You've <a href="https://stackoverflow.com/a/2848109/560648">correctly</a> emptied the string buffer with <code>ss.str("")</code>, but you also need to clear the stream's error state with <code>ss.clear()</code>, otherwise no further reads will be attemped after the first extraction, which led to an EOF condition.</p> <p>So:</p> <pre><code>string whatTime(int seconds) { string h,m,s,ans; stringstream ss; ss &lt;&lt; (seconds/3600); seconds -= (3600*(seconds/3600)); ss &gt;&gt; h; ss.str(""); ss.clear(); ss &lt;&lt; (seconds/60); seconds -= (60*(seconds/60)); ss &gt;&gt; m; ss.str(""); ss.clear(); ss &lt;&lt; seconds; ss &gt;&gt; s; return (h + ":" + m + ":" + s ); } </code></pre> <p>However, if this is your full code and you do not need the individual variables for any reason, I'd do this:</p> <pre><code>std::string whatTime(const int seconds_n) { std::stringstream ss; const int hours = seconds_n / 3600; const int minutes = (seconds_n / 60) % 60; const int seconds = seconds_n % 60; ss &lt;&lt; std::setfill('0'); ss &lt;&lt; std::setw(2) &lt;&lt; hours &lt;&lt; ':' &lt;&lt; std::setw(2) &lt;&lt; minutes &lt;&lt; ':' &lt;&lt; std::setw(2) &lt;&lt; seconds; return ss.str(); } </code></pre> <p>It's much simpler. <a href="http://ideone.com/pZ1jX2" rel="nofollow noreferrer">See it working here</a>.</p> <p>In C++11 <a href="https://stackoverflow.com/questions/8065413/stdlexical-cast-is-there-such-a-thing">you can avoid the stream altogether</a> using <code>std::to_string</code>, but that doesn't allow you to zero-pad.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload