Timer with an add 30 second function

Hi I am trying to add a function that adds thirty seconds times the user inputted value. For instance if the users inputs 1 then it only adds thirty seconds. If the users inputs 5 it will add 150 seconds. Also I am having trouble finding a solution for the program to convert anything above 60 seconds into 1 minute. Any help please. Complete noob here. Thank you

#include <iostream>
using namespace std;

main()
{
cout << "TIMER" << endl;


for (int hour = h; hour >= 0; hour--)
{
for (int min = m; min >= 0; min--)
{
if (min == 0 && h > 0)
m = 59;
for (int sec = s; sec >= 0; sec--)
{
if (sec == 0)
s = 59;

Sleep(1000);
system("cls");


cout << hour << " :hours " << endl << min << " :mins " << endl << sec << " :secs" << endl;


cout << "TIMER" << endl;


}
}
}
}
> For instance if the users inputs 1 then it only adds thirty seconds.
> If the users inputs 5 it will add 150 seconds.
Something like:
1
2
3
4
5
6
int input = 5;  // input this
int totalSeconds = input * 30;

// convert to minutes and seconds
int minutes = totalSeconds / 60;  // the number of minutes
int seconds = totalSeconds % 60;


Use / and % in a similar same way for hours.
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>
#include <chrono>
#include <thread>
#include <cstdlib>
using namespace std;

#define FMT << setw( 2 ) << setfill( '0' ) <<

int main()
{
   int totalTime;
   cout << "Input number of half-minute intervals: ";   cin >> totalTime;
   totalTime *= 30;
   for (int t = totalTime; t >= 0; t--)
   {
      int second = t;
      int hour   = second / 3600;   second %= 3600;
      int minute = second / 60  ;   second %= 60;
      system( "cls" );                                           // Comment out for testing
      cout FMT hour << " : " FMT minute << " : " FMT second << '\n';
      this_thread::sleep_for( chrono::seconds( 1 ) );            // Comment out for testing
   }
}
Last edited on
@lastchance but the program is supposed to begin at 24 hours. it is then supposed to count down from 24 hours and every time someone clicks on the imaginary button in this case, that should add 30 seconds to the timer? How can I piece these two together any tips on what I should be looking at or where to focus my attention?
salasranthony wrote:
@lastchance but the program is supposed to begin at 24 hours. it is then supposed to count down from 24 hours and every time someone clicks on the imaginary button in this case, that should add 30 seconds to the timer?


You need something event-driven to replace bool clicked() below. Just for demonstration.

this_thread::sleep_for() doesn't work in cpp.sh. Drat!

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
#include <thread>
#include <cstdlib>
using namespace std;

const int DAY = 24 * 60 * 60;
const int HALFMINUTE = 30;

//=====================================================================

class Timer
{
   int totalTime;                      // Time in seconds

public:
   Timer( int s )                      // Start timer with s seconds
   {                                   
      totalTime = s;
   }

   void wait( int s )                  // Wait for s seconds
   {
      this_thread::sleep_for( chrono::seconds( s ) );
      totalTime -= s;   if ( totalTime < 0 ) totalTime = 0;
   }

   void output()                       // Clear screen and do a posh output
   {
      int second = totalTime;
      int hour   = second / 3600;   second %= 3600;
      int minute = second / 60  ;   second %= 60;
      system( "cls" );                                           // Comment out for testing
      #define FMT << setw( 2 ) << setfill( '0' ) <<
      cout FMT hour << " : " FMT minute << " : " FMT second << '\n';
   }

   void add( int s )                   // Top-up time remaining
   {
      totalTime += s;
   }

   int remaining()                     // How much time is remaining
   {
      return totalTime;
   }
};

//=====================================================================

bool clicked()                         // Simulates clicking; does so about once every 10 times
{
   if ( rand() % 10 ) return false;

   cout << "Clicked!\n";
   return true;
}

//=====================================================================

int main()
{
   srand( time( 0 ) );

   Timer timer( DAY );                 // Start with 24 hours on the clock

   timer.output();
   while ( timer.remaining() > 0 )
   {
      timer.wait( 1 );
      timer.output();
      if ( clicked() ) timer.add( HALFMINUTE );
   }
}

//===================================================================== 

Last edited on
Topic archived. No new replies allowed.