+ 2

Help me with my code please

For some reason my code still prints "hi" when the bool is false. Can anyone help? #include <iostream> using namespace std; class John{ public: void johnMeeting(){ cout << "Hi"; } }; int main() { bool johnInteraction = false; int x = johnInteraction; if (johnInteraction = true){ John Meeting; Meeting.johnMeeting(); } if (johnInteraction = false){ cout << "John will not be doing anything"; } }

16th Jan 2018, 2:09 PM
Rislim
Rislim - avatar
3 Answers
+ 2
In both your if-structures you need to put == instead of = to compare the booleans. Now you're actually putting the value of johnInteraction on true in your first if-structure so the program will always execute that first if.
16th Jan 2018, 2:46 PM
Jente
+ 2
replace "=" with "==". "==" means you are comparing the right hand side to the left hand side, which i guess is what you want your program to do. So replace it and it will work fine.
16th Jan 2018, 3:08 PM
nil( );
nil( ); - avatar
+ 2
Thank you both!
16th Jan 2018, 3:36 PM
Rislim
Rislim - avatar