Got problem in "Jungle Camping " .😭[solved] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Got problem in "Jungle Camping " .😭[solved]

#include<iostream> using namespace std; int main() { string x; int no = 1 ; string a = "Grr", b = "Rawr",c = "Ssss"; for (int i = 0; i < no ; i++) { cin>>x; if (x == a) { cout<<"Lion "; } else if ( x == b) { cout<<"Tiger "; } else if(x==c) { cout<<"Snake "; } else { cout<<"Bird "; } no++; } return 0; } error:: timeout : the requested command dumped core

3rd Jun 2021, 10:38 AM
Sudip Shrestha
Sudip Shrestha - avatar
10 Answers
0
SUDIP You have to break your loop if there is no more input because no++ always incrementing so loop is running till infinite and giving error. But remember you have to declare string x inside loop otherwise x will never be empty. Here is solution with your logic. https://code.sololearn.com/cT1p5m3myqMl/?ref=app Here is another solution also: https://code.sololearn.com/clYz9awh1fu9/?ref=app
3rd Jun 2021, 12:30 PM
A͢J
A͢J - avatar
+ 2
What do you want to do with the variable no?
3rd Jun 2021, 10:51 AM
JaScript
JaScript - avatar
+ 1
SUDIP bro you are getting in to infinite loop. Because every time loop executes i increments by 1,and no also by 1 ,it means your loop terminating condition i.e. i<no will remain always true,i.e. never be evaluate to false so loop never ends hence timeout occurs. Hope u got it...
3rd Jun 2021, 10:58 AM
saurabh
saurabh - avatar
+ 1
🅰🅹 🅐🅝🅐🅝🅣 Thank you man, I didn't knew we could use 'cin>>x' in condition also . Learned new lesson.💛
3rd Jun 2021, 1:29 PM
Sudip Shrestha
Sudip Shrestha - avatar
+ 1
SUDIP I also didn't know that but I learn from others mistakes. So thank you too.
3rd Jun 2021, 3:15 PM
A͢J
A͢J - avatar
+ 1
When the user stops entering data the length of string becomes 0 , which makes the condition true and breaks the loop from beings infinite. Work of break(here) is to change the flow of program and get out from the loop if condition is true.
4th Jun 2021, 4:22 AM
Sudip Shrestha
Sudip Shrestha - avatar
0
To fix the no of sound to be entered and i increased it every time a user inputs sound
3rd Jun 2021, 10:57 AM
Sudip Shrestha
Sudip Shrestha - avatar
0
the value of no is increasing always and the loop is becoming infinite
3rd Jun 2021, 11:01 AM
Atul [Inactive]
0
so , do you know solution😣
3rd Jun 2021, 11:02 AM
Sudip Shrestha
Sudip Shrestha - avatar
0
🅰🅹 🅐🅝🅐🅝🅣 bro,how break works in your solution v1?
4th Jun 2021, 4:09 AM
saurabh
saurabh - avatar