0
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; }
7 Respostas
+ 3
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.
+ 2
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?
+ 2
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).
+ 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;
}
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
Many Thanks for the hints guys.
Will try it with decrement n.