+ 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; }
10 Answers
+ 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.
+ 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?
+ 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).
+ 3
Alexandru sterhan ,
both your posts here are considered and reported as spam.
none of your posts is related to the initial question
+ 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
+ 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;
}
+ 1
Many Thanks for the hints guys.
Will try it with decrement n.
0
Hi Lothar,
What do you mean with a properly ?
This is the Full Code, i tried to solve the task.
Kind regards
soNa
0
20
0
https://sololearn.com/compiler-playground/WToHLV8dJIk5/?ref=app
https://sololearn.com/compiler-playground/c74fqLPXcesE/?ref=app
https://sololearn.com/compiler-playground/WMgTRc1C8kgk/?ref=app
https://sololearn.com/compiler-playground/W8XYox9r868Z/?ref=app
https://sololearn.com/compiler-playground/c5hm1Adbfy5w/?ref=app
https://sololearn.com/compiler-playground/Wb6lwH9zbIe5/?ref=app
https://sololearn.com/compiler-playground/cb6HU90JH4Pq/?ref=app
https://sololearn.com/compiler-playground/Wmr0T5IAxaI1/?ref=app
https://sololearn.com/compiler-playground/WZLYE8STjao5/?ref=app