0

You are cheering on your favorite team.

You are cheering on your favorite team. After each play, if your team got over 10 yards further down the field, you stand up and give your friend a high five. If they don't move forward by at least a yard you stay quiet and say 'shh', and if they move forward 10 yards or less, you say 'Ra!' for every yard that they moved forward in that play. Task Given the number of yards that your team moved forward, output either 'High Five' (for over 10), 'shh' (for <1), or a string that has a 'Ra!' for every yard that they gained. //My code// #include <iostream> using namespace std; int main () { int yards; cin>> yards; while (yards<10){ if (yards>1){ cout<<"Ra!"<<endl; yards ++; } else { cout<<"shh"<<endl; break; } } if (yards>10) { cout<<"High Five"<<endl; } return 0; } Can anyone explain what am I doing wrong here? I'm getting 4/5 right, I don't know about 5th condition.

15th Jan 2022, 12:58 PM
Neha Gawali
9 Réponses
+ 1
G'day Neha Gawali As the other have said, try to stop your code as soon as possible by catching the easy options. Eg yards <=1 If you have shortest options first, the code runs better. edit: your code is outputting "High Five" because you are using 👉yards👈 as the loop counter, and you are incrementing that with 👉yards++👈. Your while loop completes when yards>10. You could move the👉 yards>10👈 bit of code above the while loop. also, I think your while loop should have 👉yards--👈 as the loop counter increment. https://code.sololearn.com/cij7LrpPbeEu/?ref=app
16th Jan 2022, 5:29 AM
HungryTradie
HungryTradie - avatar
+ 2
Neha Gawali OK. In this case, you have to test the distance conditions before entering the loop, or the loop will interfere with the decision, just like HungryTradie said in his last edit. Also, read about for loops - they are simpler in this case.
16th Jan 2022, 10:37 PM
Emerson Prado
Emerson Prado - avatar
+ 1
yards = 1 //"shh"
15th Jan 2022, 1:11 PM
FanYu
FanYu - avatar
+ 1
Neha Gawali the logic could benefit from some effort to streamline it. I would recommend constructing the logic in the order given by the description. Apart from that, when printing "Ra!" in the loop it should keep printing on the same line (e.g., "Ra!Ra!Ra!Ra!").
15th Jan 2022, 11:16 PM
Brian
Brian - avatar
+ 1
Also when I try (yards<=10) or (yards>=1) it prints RA!RA!RA!High Five for input 3 in while loop. Why does it include "High Five" when it is less than 10?
16th Jan 2022, 5:10 AM
Neha Gawali
+ 1
Thank you so much 🙌
16th Jan 2022, 6:47 AM
Neha Gawali
+ 1
Emerson Prado while loop so that it outputs Ra! as many time as intput number. Eg. If input is "3" it outputs Ra!Ra!Ra! (3 times.)
16th Jan 2022, 6:57 AM
Neha Gawali
0
BTW, why the while loop?
15th Jan 2022, 2:00 PM
Emerson Prado
Emerson Prado - avatar
0
int main() { int i; cin>>i; if(i>10){ cout <<"High Five"; } if(i<=1){ cout<<"shh"; } if(i>=1 && i<=10){ for(int r=0;r<i;r++){ cout <<"Ra!"; } } return 0; }
26th Mar 2024, 3:57 PM
РАДЖАН ЖУМАНОВИЧ
РАДЖАН ЖУМАНОВИЧ - avatar