bool

how to make bool var which is 1 be 0 after adding with 1???

1
2
3
4
5
6
7
8
   #include <iostream>
   using namespace std;
   int main(){
	bool test=1;
	test+=1;
	cout<<test;
	return 0;
    }
closed account (iAk3T05o)
Are you sure you want to use bool?
If you want to make it go from true to false have you considered assigning it false (0) instead of trying to make 2 false.
1
2
3
4
5
6
7
8
9
10
11
12
//flip the sign
if(test)
{
    test = false;
}
else
{
    test = true;
}

//or
test = test ? false : true;
You could use a char instead of bool.

1
2
3
4
5
6
7
8
9
10
11
12
#define BOOL unsigned char;
#define TRUE 1;
#define FALSE 0;
#define IS_TRUE(a) (((a) & 0x1) == 1)

BOOL test = TRUE;

test += TRUE;
if ( IS_TRUE(test) )
{
  // do something
}

If you're just wanting to flip a boolean, you can do: bool test = true; test = !test;

If you want to make something greater than 1 be 1 again, you can do bool test = 15; test = !!test;

http://codepad.org/KwQ2E3Fy
http://codepad.org/1a0fktLO
Last edited on
Topic archived. No new replies allowed.