- 1
“Countdown” (using “while” conditions
//Code a timer that counts down to zero. #include <iostream> using namespace std; int main() { int seconds; cin>>seconds; //your code goes here while (seconds>=0); cout <<seconds <<seconds--; return 0; } //Why am I not getting this???
5 Antworten
+ 4
You are printing every second twice
+ 2
🎞Tahiti🍷
Semicolon (;) used to break statement so if you use after while condition then inside statement will not execute.
+ 2
if 'seconds' was assigned with 5 for example, your code output 5, 4, 3, 2 and 1 (post decrement: after variable)
if you've instead put decrement operator (double dash) before variable, your output would have been 4 to 0 ;)
+ 1
firstly, don't put semi-colon (;) just after while condition: this will result to an infinite loop...
output (cout) only seconds once and I think you must also output each number on a new line:
while (seconds > 0)
cout << seconds-- << endl;
... as you post decrement, you could do it "onelined", however, if you need more than one statement (many instruction semi-colon separated) you need to put a curly braces block (else block stop at first semi-colon) :P
edit: if you need countdown to zero, change the condition to 'seconds >= 0'
+ 1
Angelo , I Am AJ ! , visph
Thank you 🙏🏽
You have no idea how helpful this has been. Such small changes/errors make such a big difference.
I’m still a little confused on when to decrement with - - in front
(- - seconds) vs in back
(seconds- -). What would have happened if I has placed the decrement symbol after the variable? Thanks again.