+ 1

Help with Jungle Camping C++ [Solved]

Code Works for first 4 tests, fails 5th. I have tried using dozens of input attempts, all print correctly. 5th test is hidden so I can't see expected result. The code is linked so easy to test. I also included the description of challenge below the code. Any suggestions are welcome. #include <iostream> using namespace std; int main() { string sound[]={"Grr", "Rawr", "Ssss", "Chirp"}; string input[4]; cin >> input[0] >> input[1] >> input[2] >> input[3] ; for (int x=0; x<4; x++){ if (input[x] == sound[0]){ cout << "Lion "; }else if (input[x] == sound[1]){ cout << "Tiger "; }else if (input[x] == sound[2]){ cout << "Snake "; }else if (input[x] == sound[3]){ cout << "Bird "; } } return 0; } https://code.sololearn.com/c5LiCFp5uzmD/?ref=app

10th Nov 2022, 8:14 PM
Scott D
Scott D - avatar
17 ответов
+ 1
Not more than four possible animals. Four kinds of animals, but they may repeat. This is my rewrite, Scott D #include <iostream> using namespace std; int main() { string sound[]={"Grr", "Rawr", "Ssss", "Chirp"}; string input; // cin >> input[0] >> input[1] >> input[2] >> input[3] >> input[4]; while(cin >> input) { // for (int x=0; x<4; x++){ if (input == sound[0]){ cout << "Lion "; }else if (input == sound[1]){ cout << "Tiger "; }else if (input == sound[2]){ cout << "Snake "; }else if (input== sound[3]){ cout << "Bird "; } } return 0; }
10th Nov 2022, 9:29 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 2
Did you think over what happend when input have 4 or 5 sounds?
10th Nov 2022, 8:41 PM
JaScript
JaScript - avatar
+ 2
There is no guarantee that input have maximum 4 words..!
10th Nov 2022, 9:22 PM
Jayakrishna 🇮🇳
+ 2
I think it was meant as: what if there are more than 4 animals, e.g. grr grr ssss rawr ssss chirp grrr? I have rewritten your idea to accept any number of animals and it works fine. I thus assume that there are more than 4 animals in the last test case.
10th Nov 2022, 9:25 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 2
Scott D I mean, test this sample input : Grr Grr Grr Grr Grr expected : Lion Lion Lion Lion Lion but your code ignores last sound. Hope you Got it now ?
10th Nov 2022, 9:30 PM
Jayakrishna 🇮🇳
+ 2
Scott D You are not understanding what we saying actually.. There are only 4 possible noises. But input may have any number of words which represents sounds and may repeated also. Like Grr Grr Grr Grr Grr (5 words example) Ok. What are you looking now? what is your doubt? Do you want solution how to correct it? or suggestion or mistake? Clear it pls.
10th Nov 2022, 9:41 PM
Jayakrishna 🇮🇳
+ 2
Jayakrishna🇮🇳 Perhaps you are right, I wasn't understanding correctly. Although Ani Jona 🕊 code works, it is important for me to understand why mine failed. I will review this and try to figure out the difference in how the while statement works. I sincerely thank you both for the help and may ask for more advice as to why my code didn't work. I think the answer is obviously in what you both suggested and if you have any other suggestions/advice please feel free to comment. Thank you both again!
10th Nov 2022, 9:51 PM
Scott D
Scott D - avatar
+ 2
You're welcome. Feel free to ask any time if there is still something unclear. You are most certainly right, it is more important to understand rather than just to earn some xp.
10th Nov 2022, 9:53 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 2
Scott D Yes. Last case failing for not counting the fifth input. Instead of variables for each input , you can try : while( cin >> input) { // can accept any inputs of words with single variable. .. } You're welcome..
10th Nov 2022, 9:58 PM
Jayakrishna 🇮🇳
+ 2
Jayakrishna🇮🇳 Ani Jona 🕊 Ok, I think I see my mistake. Using the "for" loop and increasing it to repeat 5 times was causing first sound to repeat therefore extra animal (Lion) in output, so I mistakenly made an assumption that there could be only 4 loops in the "if" statement. Using the while statement resolves this. This is great, I learned something new from this. As is said, we learn more from our mistakes than successes! P.S. nice avatar Ani, I am a cat lover. My Soundcloud pages are littered with cat pics, a different one for each track. For many years I took care of 16 feral cats.
10th Nov 2022, 10:10 PM
Scott D
Scott D - avatar
+ 1
Ani Jona 🕊 I do not understand what you are saying. The description clearly states there are 4 possible animals. Accepting a 5th input causes multiple tests to fail, I have shown that to be true and you can test it yourself. If you have successfully tested your code in the actual code coach than please prove it passed.
10th Nov 2022, 9:28 PM
Scott D
Scott D - avatar
+ 1
Jayakrishna🇮🇳 I already stated above, if you allow a 5th noise to be recognized it fails! I would appreciate that advisors always test their advice in code coach before making definitive suggestions. It would make these discussions much smoother.
10th Nov 2022, 9:32 PM
Scott D
Scott D - avatar
0
First, thank you for the suggestion but unfortunately it hasn't helped. I have tested with over 4 sounds and the output is whatever sounds were recognized as you can see in the description which is in my linked code. The description does not specify any expected output for sounds not recognized therefore the expectation in my mind is that the output should be No output. The description simply says output what animals have made a sound. My code tests 4 times so extra input is ignored. If I test 5 times, it fails multiple times with the expected output. For example if input is Grr Grr boo boo it should print Lion Lion However, if I set the "if" statement to test 5 times( x<5) it fails as the result will be Lion Lion Lion but the expected result is Lion Lion Try it, you will see. This therefore proves any input larger than 4 is expected to be ignored. Also, if you check my Code Bits you'll see several extremely similiar examples that all passed for ex. Ballpark Order and Izzy the Iguana.
10th Nov 2022, 8:50 PM
Scott D
Scott D - avatar
0
Anyone suggesting input could be more then 4 words is clearly not reading what I wrote and has not tested the code.... nor have they passed the Code Coach themselves.
10th Nov 2022, 9:24 PM
Scott D
Scott D - avatar
0
Any one can help me am beggener to learning
12th Nov 2022, 6:56 AM
Mahmoud Hassn
Mahmoud Hassn - avatar
0
Mahmoud Hassn I'm a beginner as well but I will help you if I can. Do you have a question?
12th Nov 2022, 7:22 AM
Scott D
Scott D - avatar
0
How to sit first code I choose Java
12th Nov 2022, 7:26 AM
Mahmoud Hassn
Mahmoud Hassn - avatar