0
I can not get down test case 4, Cheer Creator, C++
My code is #include <iostream> using namespace std; int main() { int x; cin >> x; if (x > 10) { cout << "High Five"; } else if (x <= 10 && x >= 1) { for(x; x<10 ; x++) { cout << "Ra!"; } } else { cout << "shh"; } } For some reason everything works except test case four, which I suspect is input 10
2 Respuestas
+ 6
The loop that prints "Ra!" should execute x times. This code executes it 10 - x times.
0
yesiamavdol As Brian indicated, the problem is how many times your for loop runs.
Since you use the same variable x as both input variable and for loop counter,
It executes 10-x times instead of x times.
EXAMPLE - 6 yards, so read in x = 6.
So your for loop starts at 6, increments each time, and stops at 10.
6(Ra),7(Ra),8(Ra),9(Ra),10(stop)
Ra! only 4 times (10-6) instead of intended 6 times.
You should either
👉 create a second variable y in the for loop and count to x
int y=0; y<x; y++
Or
👉 stick with one variable x and count down instead of up
x; x>0; x--
GOOD DEBUGGING TIPS
👉 Trace the execution of code by hand on paper if code simple enough.
👉 Add temporary print statements for changing variables to see how they change as program runs