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;
> 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;
@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?
@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!
#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
#include <thread>
#include <cstdlib>
usingnamespace std;
constint DAY = 24 * 60 * 60;
constint 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 ) returnfalse;
cout << "Clicked!\n";
returntrue;
}
//=====================================================================
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 );
}
}
//=====================================================================