+ 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; }
24 Respuestas
+ 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/
+ 3
~ swim ~ Good to know, I haven't done this challenge in C yet and that was my plan 🙃🤯
+ 3
You chould compare like this:
if(a == "Rawr" || b == "Rawr" || c == "Rawr" || d == "Rawr")
cout <<"Tiger ";
It will work.
+ 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.
+ 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.
+ 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++.
+ 2
You can make a loop within cout.
Or you can split the one if condition to 4 if.
+ 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.
+ 2
This is because this method does not maintain the same arrangement of animals and sounds
+ 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.
+ 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.
+ 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.
+ 1
It must use a loop
+ 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.
+ 1
Don't forget to put the 'cin' inside the loop.
+ 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. 😊
+ 1
Comparison is wrong
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.
0
Rafik Abdelhak Nadir
I tried spliting if
https://www.sololearn.com/coach/45?ref=app
but cases 3 and 5 still wrong.
0
Split if without puting else statement.
Did you do that?