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;
}
elseif (seconds < 59){
seconds++;
}
cout << hours << ":" << minutes << ":" << seconds << "\r" << flush;
}
}
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).