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; }

6th Feb 2018, 8:39 AM
Dokko Krash
Dokko Krash - avatar
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"; }
15th Feb 2018, 7:45 AM
Michael Joseph Jaroya
Michael Joseph Jaroya - avatar
+ 5
… if(your_age==0){cout<<"you are dead";} … "=" is an assignment operator "==" — comparison for equality
6th Feb 2018, 8:43 AM
deFault
+ 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 }
6th Feb 2018, 8:51 AM
deFault
0
thanks!
6th Feb 2018, 8:43 AM
Dokko Krash
Dokko Krash - avatar
0
thanks but now it shows both "you are but a child" and "you are dead" at the same time. now what?
6th Feb 2018, 8:45 AM
Dokko Krash
Dokko Krash - avatar
0
thank you!
6th Feb 2018, 8:50 AM
Dokko Krash
Dokko Krash - avatar