+ 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"; } }
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.
+ 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.
+ 2
Thank you both!