0
Need help with c++ countdown code
I’m currently working on the module “countdown” and am having trouble with the code. The problem asks that you create a counter that takes a users input and counts it down, beeping after multiples of 5. The code I have seems to do what is asked but some of the test cases say it doesn’t. I don’t want the answer to the module I just want to know what is wrong with the code so I can avoid the mistake in the future. Here is the code. https://code.sololearn.com/cZe0n8K3nrL9/?ref=app
6 odpowiedzi
+ 4
Mason
Your code is right but there is Beep not beep. Check spelling.
To avoid mistakes in future read problem carefully.
+ 2
the question actually says to replace the multiple of 5 with the beep string
In your code, you're just trying the print whole countdown first then you're checking to print out beep in the multiple of 5 on line 11.
usually, the compiler excutes the code from 1 - n lines.
the fix is to make an else statement and put the line 10 into the else statement. (don't try to put n-- variable into the if and else statement. otherwise it will result in infinite loop!)
also, finally someone says not to post answers without explanation!
+ 2
Thanks for the help guys, didnt notice i misspelled Beep.
+ 2
This worked for me
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
//your code goes here
while(n > 0){
cout<< n << endl;
if(n % 5 == 0){
cout << "Beep" << endl;
}
n--;
}
return 0;
}
0
//countdown
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
while(n>=1)
{
cout<<n<<endl;
if(n%5==0)
cout<<"Beep"<<endl;
n--;
}
return 0;
}
An example of Countdown
0
//countdown
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
while(n>=1)
{
cout<<n<<endl;
if(n%5==0)
cout<<"Beep"<<endl;
n--;
}
return 0;
}
An example of Countdown