Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed Timer won't work

+2 votes
asked Apr 2 by Master (240 points)
closed Apr 4 by Master
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;

int main () {
    cout << "This a timer.\n";
    int timer;
    cout << "Please enter a time. \n";
    cin >> timer;
    int countdown = timer * 1000;
    std::this_thread::sleep_for(std::chrono::milliseconds(countdown));
    cout << "Stop, Timer finished";
    return 0;
}
closed with the note: Solved

1 Answer

0 votes
answered Apr 3 by Peter Minarik (63,340 points)

Let me point out some strange code of yours:

Overcomplicated maths

int timer3  = timer * 2;
int timer2 = timer - timer3;

So timer2 = timer - (timer * 2) = -timer; Why the complicated mathematics and not just writing simply as

int timer2 = -timer;

Also, you could come up with better names than just appending a number after the variables as there's not much meaning what's the purpose between the variants (e.g. timertimer1timer2timer3, etc).

Strange loop

do {
    cout << "Stop, timer finished";
    return 0;
}
while (timer2 == 0);

So the loop starts by printing to the standard output (std::cout) then exiting (return) from the main() function and by this, the whole program. After this, you check if the timer2 is 0 and if it is true, you keep repeating your loop.

Since you exit from the whole program, you'll never really have a loop. If you'd remove the return statement, then you would have an infinite loop as timer2 does not change and will never get to be 0

Not a timer

There is no timer here really, not in a classical meaning anyway (seconds or milliseconds).

Not?

I have never seen the keyword not being used in C++ before. I wasn't even aware it exists. The regular not operator is the exclamation mark (!), e.g.:

while (timer2 != 0);
commented Apr 3 by Master (240 points)
edited Apr 3 by Master
Thanks for showing me how to simplify the maths and by the way you can do not and != but to make my code more simple I'll change it to !=
commented Apr 3 by Master (240 points)
With the timer concept what I've done is I make the amount of time negative and make it so that time will go up by 1 every second until you get to 0 and when you have 0 it will print stop, timer finished however the problem I encounter is when the timer goes up it goes up by way too much and finishes instantly and the loop is just a way to make it so it will continually ask whether or not the timer is at 0 yet as if I just put an if it would be run only once that was my main problem
commented Apr 3 by Master (240 points)
edited Apr 3 by Master
My problem now is as soon as you type your number the program stops
commented Apr 4 by Peter Minarik (63,340 points)
You didn't add any *wait* in your code. So counting back from 10 to 0 happens so quickly (CPUs are fast) that you cannot even notice the time it took.

You can put the current thread to sleep (and allow other threads to run) in C++ like this:

#include <chrono>
#include <thread>
// ...
std::this_thread::sleep_for(std::chrono::milliseconds(time_in_milliseconds));

or if you want seconds, you can do

std::chrono::seconds(time_in_seconds)

Check this out for other durations: https://en.cppreference.com/w/cpp/chrono/duration
commented Apr 4 by Master (240 points)
Thank you so much it works now

#include <iostream>
#include <chrono>
#include <thread>
using namespace std;

int main () {
    cout << "This a timer.\n";
    int timer;
    cout << "Please enter a time. \n";
    cin >> timer;
    int countdown = timer * 1000;
    std::this_thread::sleep_for(std::chrono::milliseconds(countdown));
    cout << "Stop, Timer finished";
    return 0;
}
commented Apr 5 by Peter Minarik (63,340 points)
My pleasure.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...