bool

Mar 23, 2014 at 8:55pm
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;
    }
Mar 23, 2014 at 9:09pm
closed account (iAk3T05o)
Are you sure you want to use bool?
Mar 23, 2014 at 9:32pm
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;
Mar 23, 2014 at 9:43pm
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
}

Mar 23, 2014 at 9:51pm
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 Mar 23, 2014 at 9:52pm
Topic archived. No new replies allowed.