+ 1
Why is my code for C++ 21 code project wrong?
#include <iostream> using namespace std; int main() { int n; cin >> n; cout<<n<<endl; while(n>1){ cout<<--n<<endl; if((n%5)==5){ cout<<"Beep"<<endl; } } //your code goes here return 0; }
2 Respuestas
+ 1
The code decrements before printing. That causes it to skip printing the starting value of the countdown. Move the decrement to then end of the loop. Or more appropriately, use a for loop because it is a known number of iterations.
The conditional in the if statement will be always false. Modulo (%) returns the remainder of a division. If you divide by 5, the remainder will be within 0 through 4, never 5. To check for exact multiples of 5, look for remainder of 0.
+ 1
You have to test how it works and what equals:
n%5
The result of it can on console be printed. Then you can see what happend and why.