Please help me | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Please help me

I was doing a project in C++ and I don't know what is happening. My code is supposed to count down from the inputed number, and beep after a multiple of 5. It does this perfectly, but at the end it prints an enter, and this makes my code wrong. I can't figure out why this doesnt work, so I'm hoping that someone else can. Here is my code: #include <iostream> using namespace std; int main() { int n; cin >> n; int y=5; while (n>0) { int x=n%y; cout << n << endl; if (x<1) { cout<< "Beep \n"; n--; } else { n--; int x=n%y; } } return 0; }

3rd Dec 2020, 4:31 PM
Taco Tube
Taco Tube - avatar
2 Answers
+ 2
That is not the problem, your code does not pass the test cases due to the extra space you print after "Beep". Remove that and everything should be fine.
3rd Dec 2020, 4:45 PM
Shadow
Shadow - avatar
+ 1
instead of the lines declaring x = n%y you could just evaulate n%y == 0 in your if statement. and yes also the space in “Beep_\n” needs to be removed
3rd Dec 2020, 4:50 PM
Roderick Davis
Roderick Davis - avatar