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
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
+ 2
What do you want to do with the variable no?
+ 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...
+ 1
🅰🅹 🅐🅝🅐🅝🅣 Thank you man,
I didn't knew we could use 'cin>>x' in condition also . Learned new lesson.💛
+ 1
SUDIP
I also didn't know that but I learn from others mistakes. So thank you too.
+ 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.
0
To fix the no of sound to be entered and i increased it every time a user inputs sound
0
the value of no is increasing always and the loop is becoming infinite
0
so , do you know solution😣
0
🅰🅹 🅐🅝🅐🅝🅣 bro,how break works in your solution v1?