0
when I type "0" in "your_age" it only shows "you are but a child" instead of "you are dead". can you help me?
#include <iostream> using namespace std; int main() { int my_age; int your_age; my_age=15; cin>>your_age; if(my_age>your_age){cout<<"you are but a child";} else{cout<<"my dude";} if(your_age=0){cout<<"you are dead";} return 0; }
6 Réponses
+ 1
when you type "0"as your age, both of the statements:'your_age==0' and 'my_age>your_age' evaluate to 'true' hence both of their bodies are executed. you should rather type:
if(your_age==0){
cout<<"you are dead";
}
else if((your_age>0)&&(my_age>your_age)){
cout<<"you are but a child";
}
else{
cout<<"my dude";
}
+ 5
…
if(your_age==0){cout<<"you are dead";}
…
"=" is an assignment operator
"==" — comparison for equality
+ 2
That's correct, according to the programm's logic because 0 is less than "my_age" (15).
You may write it like this:
if (your_age==0) {
cout << "you are dead";
} else if (my_age>your_age) {
cout << "you are but a child";
} else {
cout << "my dude"; // I'm not sure when this should be printed
}
0
thanks!
0
thanks but now it shows both "you are but a child" and "you are dead" at the same time. now what?
0
thank you!