Flipping a boolean value upon user entry

Hi all, I would like to know the best way to flip a boolean value to true from false. It is regarding an assignment I have for an introductory c plus plus course. The assignment requires us to create a simple console program that keeps track of two trucks in a business. I plan to use a boolean to keep track of the truck availability. bool avail1 for truck 1 and bool avail2 for truck 2

Feature #2: Truck Returned Home when the work is done,
the truck returns home. We want to use this feature to let the program
know that the truck is available for new work.

The above feature requires me to flip the boolean value upon user entry to true again. I could use "cin" to get the user entry but how do I use that to flip the boolean value. Is it even possible? Can someone explain? Thank you!
Last edited on
foo = not foo;
I don't get it. I'm sorry, I'm a beginner at this subject so if you can elaborate I really appreciate it. Nevertheless, Thanks for the reply :)
you can change a Boolean many ways. Without showing a bunch of nonsense stuff:

you can say

foo = !foo; //! is logical not in c++, same as above. If foo is true, not true is false, so foo is assigned not true (false) and flipped. If foo is false, not false is true, ....

or you can be explicit (this is inefficient):
if(foo == true)
foo = false;
else
foo = true;

so in your program, where you have code that determines the user has provided input and the variable needs flipped, you can use this type of code to change the value of the Boolean variable.


Last edited on
Got it! Thank you so much Jonnin! You are a life saver :)
Topic archived. No new replies allowed.