Help with Function that makes Shows time entered in Clock format

Program should continually have user enter a positive integer, and quits on zero entered. This entered number represents the total number of seconds and after each number is entered a function is called that displays the time in hours, minutes and seconds. Sample output is as follows:

Enter Total Seconds --> 3605
1:00:05

The function needs only one value parameter and should return nothing back to main. If the minutes or seconds are a one digit number then make sure to display a leading zero as the example above shows.

Here is my program. my question is how do i make the numbers appear like this?
1:00:05


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream> 
#include <iomanip> 
using namespace std; 

int ClockTime (int H, int M, int S); 
int main(); 
{ 
int Seconds, Minutes, Hours; 
do { 
cout << "Enter total seconds --> "; 
cin >> Seconds; 
Minutes = Seconds / 60; 
Hours = Minutes / 60; 
ClockTime (); 
} 
while (Seconds != 0); 
} 
int ClockTime (int H, int M, int S) 
{ 
cout << H << ":" << M << ":" << S; 
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <iomanip>

int main()
{
    int hr = 1 ;
    int min = 0 ;
    int sec = 5 ;

    // http://www.cprogramming.com/tutorial/iomanip.html
    std::cout << hr << ':'
               << std::setw(2) << std::setfill('0') << min << ':'
               << std::setw(2) << std::setfill('0') << sec << '\n' ;

}
Thank you for the helpful page! i now have this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iomanip>
using namespace std;

void ClockTime ();
int ClockTime (int H, int M, int S);
int main()
{
    void ClockTime ();
    int Hours, Minutes, Seconds;
    do{
        cout << "Enter total seconds --> ";
            cin >> Seconds;
        
        Hours = (Seconds / 3600);
        Minutes = (Seconds / 60) % 60;
        Seconds = Seconds % 60;
        
        cout << Hours << ":" << setw(2) << setfill('0') << Minutes << ":"
        << setw(2) << setfill('0') << Seconds << endl ;}
    
        while (Seconds != 0);
    
}


Can you explain this to me?

"The function needs only one value parameter and should return nothing back to main."

and also

"after each number is entered a function is called." i do not believe i have a function right?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <iomanip>
using namespace std;

// function that displays the time in hours, minutes and seconds
// The function needs only one value parameter and should return nothing back to main
void print_time( int seconds ) ;

int main()
{
    int seconds;
    do {
        cout << "Enter total seconds --> ";
        cin >> seconds ;

        // after each number is entered the function is called.
        if( seconds > 0 ) print_time(seconds) ;
    }
    while( seconds != 0 );
}

void print_time( int seconds )
{
    const int hours = ( seconds / 3600 ) % 24 ;
    const int minutes = ( seconds / 60 ) % 60 ;
    seconds = seconds % 60;

    cout << hours << ":" << setw(2) << setfill('0') << minutes << ":"
         << setw(2) << setfill('0') << seconds << '\n' ;
}
can i exclude the if statement and just have

print_time(seconds) ;
Depends. What do you want to print if a negative value is entered?
ah i see. well then maybe some humor added.

1
2
3
4
if( seconds > 0 ) 
{print_time(seconds);}
if( seconds < 0 )
{cout << "This program does not support time traveling.";}


anyways thank you very much my friend!

Last edited on
Topic archived. No new replies allowed.