Why it is not working? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why it is not working?

#include <iostream> using namespace std; string a,b,c,d; void tiger() { if((a||b||c||d) =="Rawr") cout <<"Tiger "; } void bird() { if((a||b||c||d) == "Chirp") cout <<"Bird "; } void snake() { if((a||b||c||d) == "Ssss") cout <<"Snake "; } void lion() { if((a||b||c||d) == "Grr") cout <<"Lion "; } int main() { cin >> a >> b >> c >> d; tiger(); bird(); snake(); lion(); return 0; }

28th Feb 2020, 3:11 PM
Ahmad Hellani
Ahmad Hellani - avatar
24 Answers
+ 3
[EDIT] disregard the comment here. ~ swim ~ let me know this won't work. See his comment that follows. The switch statment is also an option here: https://www.sololearn.com/learn/C/2924/
28th Feb 2020, 3:29 PM
Paul K Sadler
Paul K Sadler - avatar
+ 3
~ swim ~ Good to know, I haven't done this challenge in C yet and that was my plan 🙃🤯
28th Feb 2020, 3:36 PM
Paul K Sadler
Paul K Sadler - avatar
+ 3
You chould compare like this: if(a == "Rawr" || b == "Rawr" || c == "Rawr" || d == "Rawr") cout <<"Tiger "; It will work.
28th Feb 2020, 4:50 PM
Rafik Abdelhak Nadir
Rafik Abdelhak Nadir - avatar
+ 3
Rafik Abdelhak Nadir It worked thanks, but now there is new problem. If the input is Rawr Rawr my output is Tiger one time I need my function to work for each input. I tried cin >> a; tiger(); cin >> b; tiger(); ...... but this didnot help me since I put the 4 functions for each cin and the function worked 4 times for each correct string.
29th Feb 2020, 9:29 PM
Ahmad Hellani
Ahmad Hellani - avatar
+ 3
I used a do/while loop reading in each sound with scanf and an if/else if/else if/ else if structure (4 if's) as Rafik Abdelhak Nadir suggested and that worked.
29th Feb 2020, 9:35 PM
Paul K Sadler
Paul K Sadler - avatar
+ 3
The problem I see with the code structure is that you could run into a problem if the sound order is Rawr Chirp Rawr for example. You would call tiger() which will output Tiger Tiger. Then you call bird() which will output Chirp. But you need Tiger Bird Tiger. As Rafik Abdelhak Nadir suggested a loop with the tests broken into 4 ifs might be better. I did the solution in c and I tagged you in a comment to the code. You could use a similar approach in C++.
29th Feb 2020, 10:42 PM
Paul K Sadler
Paul K Sadler - avatar
+ 2
You can make a loop within cout. Or you can split the one if condition to 4 if.
29th Feb 2020, 9:32 PM
Rafik Abdelhak Nadir
Rafik Abdelhak Nadir - avatar
+ 2
Paul K Sadler Its my fault here is the code #include <iostream> using namespace std; string a,b,c,d; string x ="Rawr"; string y ="Chirp"; string z ="Ssss"; string w ="Grr"; void tiger() { if (a == x){cout <<"Tiger ";} if (b == x){cout <<"Tiger ";} if (c == x){cout <<"Tiger ";} if (d == x){cout <<"Tiger ";} } void bird() { if (a == y){cout <<"Bird ";} if (b == y){cout <<"Bird ";} if (c == y){cout <<"Bird ";} if (d == y){cout <<"Bird ";} } void snake() { if (a == z){cout <<"Snake ";} if (b == z){cout <<"Snake ";} if (c == z){cout <<"Snake ";} if (d == z){cout <<"Snake ";} } void lion() { if (a == w){cout <<"Lion ";} if (b == w){cout <<"Lion ";} if (c == w){cout <<"Lion ";} if (d == w){cout <<"Lion ";} } int main() { cin >> a >> b >> c >> d; tiger(),bird(),snake(),lion(); return 0; } and Rafik Abdelhak Nadir how else can help me.
29th Feb 2020, 10:30 PM
Ahmad Hellani
Ahmad Hellani - avatar
+ 2
This is because this method does not maintain the same arrangement of animals and sounds
29th Feb 2020, 10:36 PM
Rafik Abdelhak Nadir
Rafik Abdelhak Nadir - avatar
+ 2
Suppose that a and c is Rawr, and b is Grr and d is Ssss, the output must be Tiger Lion Tiger Snake. But you output will be lik this Tiger Tiger Snake Lion. Because you are calling function in that order and each function evalute the 4 inputs then excute all cout when the if condition is true.
29th Feb 2020, 10:42 PM
Rafik Abdelhak Nadir
Rafik Abdelhak Nadir - avatar
+ 2
Rafik Abdelhak Nadir I only did this and all cases become correct while(cin >> a){tiger(), bird(),....... } and it worked. Thank you every one for helping me.
1st Mar 2020, 7:41 AM
Ahmad Hellani
Ahmad Hellani - avatar
+ 1
Ahmad Hellani Do you have your code in the code playground? You could tag me in a comment to the code and then I would be able to see it.
29th Feb 2020, 10:27 PM
Paul K Sadler
Paul K Sadler - avatar
+ 1
It must use a loop
29th Feb 2020, 10:43 PM
Rafik Abdelhak Nadir
Rafik Abdelhak Nadir - avatar
+ 1
Try to use one input then make a loop which call the 4 functions inside the loop for 4 sequences. And change the 4 function to evalute just one variable as if condition.
29th Feb 2020, 10:48 PM
Rafik Abdelhak Nadir
Rafik Abdelhak Nadir - avatar
+ 1
Don't forget to put the 'cin' inside the loop.
29th Feb 2020, 10:54 PM
Rafik Abdelhak Nadir
Rafik Abdelhak Nadir - avatar
+ 1
Your comparison's logic is little bit wrong. It perform oring operation in a,b,c,d this result compare with "Rawr". Comparison should be- if(a == "Rawr" || b == "Rawr" || c == "Rawr" || d == "Rawr") cout <<"Tiger "; This is may be solution. 😊
1st Mar 2020, 12:27 PM
Rajale Dhanashri Rajendra
+ 1
Comparison is wrong
1st Mar 2020, 3:12 PM
SRIKANTH
SRIKANTH - avatar
0
~ swim ~ I tried your suggestion but it didn't work. I also tried this string x="Rawr" if ((a == x)||(b == x)..... cout <<"Lion". The comparison worked but now it says there is no type for x.
28th Feb 2020, 4:02 PM
Ahmad Hellani
Ahmad Hellani - avatar
0
Rafik Abdelhak Nadir I tried spliting if https://www.sololearn.com/coach/45?ref=app but cases 3 and 5 still wrong.
29th Feb 2020, 10:24 PM
Ahmad Hellani
Ahmad Hellani - avatar
0
Split if without puting else statement. Did you do that?
29th Feb 2020, 10:27 PM
Rafik Abdelhak Nadir
Rafik Abdelhak Nadir - avatar