0

[Solved] Cheer Creator: Test Case #5 Fails

#include <iostream> using namespace std; int main() { unsigned int num; cin >> num; switch (num) { case 0: cout << "shh"; break; case 1 ... 10: for (int i = 0; i < num; i++) cout << "Ra!"; break; default: cout << "High Five"; } return 0; }

27th Dec 2019, 7:15 PM
William Johnson
William Johnson - avatar
4 Réponses
+ 1
What's the error? Your code is working fine. It's just giving a warning because you are comparing values of different data types. <unsigned int> type have only positive numbers while <int> have both positive and negative numbers. So when you are comparing <int> with <unsigned int>, the compiler converts unsigned int to int which could give wrong results because of the conversion. That's why compiler is giving warning.
27th Dec 2019, 7:26 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 1
Just noticed now, you are doing the Code Coach challenge. You need to print "shh" when the input is less than 1. In your switch case, "shh" will be printed only when your input is '0'. If the value is negative, then it will run the default case which will print "High Five" which it shouldn't. So you need to convert your switch to if-else to change the first condition. That is: if(num < 1) // 'shh' else if(num <= 10) // 'Ra' else // 'High five'
27th Dec 2019, 7:47 PM
blACk sh4d0w
blACk sh4d0w - avatar
0
Figured it out. Case #5 has a negative input, so I needed to use a signed int and also check for the condition when num is negative and print out "shh". switch (num > 0 ? num : 0)
27th Dec 2019, 7:37 PM
William Johnson
William Johnson - avatar
0
You can try this: #include <iostream> using namespace std; int main() { int n; string new_text, text = "Ra!"; cin >> n; if (n<1){ cout << "shh"; } else if(n>1 && n <=10){ for(int i=0;i<n;i++) { new_text+=text; } cout<<new_text<<endl; } else{ cout << "High Five"; } return 0; }
15th Jan 2020, 7:05 PM
Peter
Peter - avatar