I'm a complete noob, but I think I am learning this language ok. Until this happens:
I wrote a simple timer program. I was expecting to see the seconds revert to 0 when they got to 60, but as you can see from the attachments, they continued to count upwards. Then a very weird thing happened. It jumped from 19552 to 25874 and then from 25927 to -32410...
So what happened. Is my code incorrect to get the seconds to start back at zero when they reach 60, and then add a 1 in the minutes place???
The OP has probably understood that every C Language line should be terminated by a semicolon, and accordingly he has placed a terminating semicolon at the end of the if() structure. Hopefully, some one will explain that all lines of C Language are not be terminated by semicolons. What are those lines that need semicolon terminations?
GolamMostafa:
The OP has probably understood that every C Language line should be terminated by a semicolon, and accordingly he has placed a terminating semicolon at the end of the if() structure. Hopefully, some one will explain that all lines of C Language are not be terminated by semicolons. What are those lines that need semicolon terminations?
The failure is thinking in terms of lines instead of statements.
Danois90:
The code you post cannot work, and why would you introduce (double) division when it is not needed?
Have you run these codes? I tested these codes prior to posting. You may suggest modifications and improvements -- you just can't say that the codes are not working!
GolamMostafa:
Have you run these codes? I tested these codes prior to posting. You may suggest modifications and improvements -- you just can't say that the codes are not working!
You code would cause seconds to be increased ~100 times per second, this is IMHO defunct behaviour
Croy007:
Is that because the curly bracket is there?
No. It's because the language is poorly designed. There are countless syntactically valid constructs that are handily discarded by the optimizer because they have no side-effects. Welcome to C++.
Do your current and future self a favour. Always use curly brackets with conditionals. (And don't put semicolons immediately after the closing parenthesis of if-statements. )
And why would they jump from a high positive to a high negative?
Excellent observation. That would be a symptom of an integer overflow.
No. It's because the language is poorly designed. There are countless syntactically valid constructs that are handily discarded by the optimizer because they have no side-effects. Welcome to C++.
Excellent. So all of our programming is based on a "poorly designed", dare I say it, flawed, language. Got it! How did we get to here, wow. Looks like my brain will force me to learn some history now, haha!
Excellent observation. That would be a symptom of an integer overflow.
So to cure the integer overflow, I would use long? But even that has a limitation, correct?
Croy007:
So to cure the integer overflow, I would use long?
That only delays the inevitable. All C integer datatypes have a fixed size so they eventually overflow. The C / C++ language does not include run-time range checking so the result for signed datatypes is exactly what you observed.
But even that has a limitation, correct?
Correct.
How did we get to here, wow.
A combination of things like "highly portable", "market dominance", and "monopolist practices".
Danois90:
The code you post cannot work, and why would you introduce (double) division when it is not needed?
Danois90:
You code would cause seconds to be increased ~100 times per second, this is IMHO defunct behaviour
Now, you have discovered that the codes are working but at a speed of x100 times! This is expected as the time scale of the Time-Tick has been reduced to only 10 ms in place of 1000 ms. Why? Who is going to wait for an hour to see the hour-hand UP?