+ 1

Countdown Test

Can some explain me why this code is not working? Or did I approach the task incorrectly?? My code: #include <iostream> using namespace std; int main() { int n; cin >> n; //your code goes here while(n != 1){ if(n % 5 == 0){ cout << "Beep" << endl; cin >> n; } else{ cin >> n; } } return 0; }

29th Oct 2024, 8:00 PM
soNa
soNa - avatar
10 odpowiedzi
+ 4
Jerry Hobby , it is not seen as very helpful when we are going to post a ready-made code. it is more helpful to give hints and tips, so that the op has a chance to find a solution by himself.
29th Oct 2024, 8:47 PM
Lothar
Lothar - avatar
+ 3
soNa , can you please share a proper task description or anything else (tutorial / module / lesson name) that allow us to see what has to be done?
29th Oct 2024, 8:02 PM
Lothar
Lothar - avatar
+ 3
soNa, only take the input of n once, as you have at the beginning of the program. Inside the loop do not take further input. Instead, decrement n in each iteration. Add a line that outputs the value of n. Be sure to print the last value of 1. (The description got cut off above).
29th Oct 2024, 8:36 PM
Brian
Brian - avatar
+ 3
Alexandru sterhan , both your posts here are considered and reported as spam. none of your posts is related to the initial question
30th Oct 2024, 8:05 PM
Lothar
Lothar - avatar
+ 1
Sorry, my bad. here the Task description: ############################################################################## You need to make a countdown app. Given a number N as input, output numbers from N to 1 on separate lines. Also, when the current countdown number is a multiple of 5, the app should output "Beep". Sample Input: 12 Sample Output: 12 11 10 Beep 9 8 7 6 5 Beep 4 3 2
29th Oct 2024, 8:10 PM
soNa
soNa - avatar
+ 1
There are a couple small problems. 1. You are not decrementing n. You need: n--; 2. You used cin >> n; -- twice -- don't need to prompt again for input. The entire else statement is unnecessary. 3. You were not printing out n. That needs to be at the top so it prints "10" before the "beep". Look carefully at the sample output. Run the code, input 12, and you should see the exact output in the instructions. Here is the adjusted code: #include <iostream> using namespace std; int main() { int n; cin >> n; //your code goes here while(n != 1){ cout << n << endl; // ADDED if(n % 5 == 0){ cout << "Beep" << endl; } // REMOVED ELSE STATEMENT n--; // ADDED } return 0; }
29th Oct 2024, 8:41 PM
Jerry Hobby
Jerry Hobby - avatar
+ 1
Many Thanks for the hints guys. Will try it with decrement n.
29th Oct 2024, 9:13 PM
soNa
soNa - avatar
0
Hi Lothar, What do you mean with a properly ? This is the Full Code, i tried to solve the task. Kind regards soNa
29th Oct 2024, 8:05 PM
soNa
soNa - avatar
0
20
30th Oct 2024, 4:58 PM
Alexandru sterhan