Add 0 to numbers less than 10

Not sure how i would add a 0 in front of the modulus calculation if computing seconds to hours, minutes, and seconds if the values equaled a number less than 10. Would a simple iomainip format using setfills work, or would that add an extra character if the number was greater than 10?

for example I would want my output to look like: 03:20:06 if I was entering 12006 as my input into the function.

1
2
3
4
5
6
7
8
9
10
11
12
void convert_seconds(int seconds) {
int hour, sec,min;

hour=seconds/3600; 
seconds=seconds%3600; 
min=seconds/60; 
seconds=seconds%60; 


cout<<hour<<":"<<min<<":"<<seconds; 

}
Last edited on
Topic archived. No new replies allowed.