Timer output problem

Hi.

I just recently started playing around with c++ and set myself with a challenge of making a simple timer with what little I know just to get the hang of it, I know there are million better ways it could be done but please humor me.


My issue is when the timer hits 59 instead of starting from 0 again it starts from 10 for some reason and increase by 10 instead of by and then after it does that 10 times it goes back to normal until the next time. I have no idea how to fix it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
   int hours = 0;
    int seconds = 0;
    int minutes = 0;
    int start = 0;

    while (start < 1){
    for (int timer = 0; timer <= 60; Sleep(100),timer++){
    if (seconds == 59){
    seconds = 0;
    }
    else if (seconds < 59){
    seconds++;
    }
    cout << hours << ":" << minutes << ":" << seconds << "\r" << flush;
    }
    }
Last edited on
it worked just fine for me when I modified it to print one line at a time for the output, I get ...59, 0, 1, ... 59, 0, 1, over and over. And the code looks correct, I wonder if its an artifact on the display somehow? I don't see any issue.

you can condense everything in that for loop to:
seconds = ++seconds %60;
(Just to boost your exposure to the language a little).
Last edited on
Thanks for the help!

Do you know any other way of printing out a single line with multiple variables?

Update: Ok, I think the "\r" is the problem but I don't know any other ways to make it work like that without "\r".
Last edited on
Topic archived. No new replies allowed.