# need help with bool expresions(Solved)

This topic is 4092 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i have a problem using c++ i want to make a function that will seta nodes activation to true (i.e. sActiveNode.node6Active = true;) and set the last node that was set to false, can anyone help?
struct  sActiveNodes
{
bool node1Active;
bool node2Active;
bool node3Active;
bool node4Active;
bool node5Active;
bool node6Active;
bool node7Active;
bool node8Active;
bool node9Active;
bool node10Active;
}

sActiveNodes sActiveNode;
sActiveNode.node6Active = true;
sActiveNode.node1Active = false;


[Edited by - Prog101 on November 10, 2006 3:55:42 AM]

##### Share on other sites
Here's a perfect example of when arrays shoule be used...
struct sActiveNodes{	bool nodeActive[10];}sActiveNodes sActiveNode;sActiveNode.nodeActive[6] = true;sActiveNode.nodeActive[1] = false;void setNodeActive(sActiveNodes& nodeStruct, int nNode){   for(int i=0; i<10; ++i)      nodeStruct.nodeActive[nNode] = false;   nodeStruct.nodeActive[nNode] = true;}

EDIT: Or, if you'll never have more than 32 nodes (Assuming 32-bit compiler):
struct sActiveNodes{	unsigned int nodeActive;}void setNodeActive(sActiveNodes& nodeStruct, int nNode){   nodeStruct.nodeActive = 1 << nNode;}

Assuming in both cases that nNode is 0..9

##### Share on other sites
Use an array instead of a struct. Something like this should do the trick.

bool sActiveNodes[10];void SetActive(int index){  sActiveNodes[index - 1] = false;  sActiveNodes[index] = true;}

Of course the above is full of "issues" (it's riduculously easy to get out-of-bounds errors in the above, which would have to be fixed, and it doesn't check whether the previous node was true to begin with, assuming that matters. And of course, it doesn't actually initialize the contents of the array. [wink]

So please, don't use that code as it is.... [lol]

Edit: Oops, looks like I misunderstood a bit of what you wanted. Better look at Evil Steve's code instead. :)

Also, normally I'd suggest using a std::vector instead of an array, but vector<bool> is a bit... weird...

##### Share on other sites
thanks that is a good example cheers guys thanks a lot

##### Share on other sites
Don't use duplication like that. If you have more than 2 of the same thing then you should always make it into some kind of array. In this case it will also solve your problem.
int oldNode = 6;int newNode = 1;struct sActiveNodes{	bool nodeActive[10];};sActiveNodes sActiveNode;sActiveNode.nodeActive[oldNode] = true;sActiveNode.nodeActive[newNode] = false;
This is an improvement, but still not how I'd personally do it.
If there were enough items in the array you could use vector<bool>, but in this case I'd probably use something like:
int oldNode = 6;int newNode = 1;unsigned short sActiveNode;sActiveNode = 1<<newNode; //This clears out all other bits at the same time// Now to test them...int testNode = 3;if ((sActiveNode>>testNode) & 1) // Then node 3 is active...
Also take a look at this, and a usable implementation of it here.
Hope that helps.

Edit: Damn, way too slow this morning!

##### Share on other sites
How exactly would you uses vector<bool> because i intend to extend the amount of nodes i want to use

##### Share on other sites
vector<bool> is a specialization; the standard defined it as a specializations that "optimizes space." In practice this means instead of storing/managing an array of bools (generally what vector does), the boolean values are encoded and packed into individual bits.

This causes problems; vector<bool> does not meet container requirements (because, among other reasons, you can't address individual bits so workarounds must be implemented; vector<bool>::iterator isn't random-access, et cetera).

Depending on your actual needs, this might not matter too much.

##### Share on other sites
Right i have experimented and i think i got it

#include <vector>int SizeOfNodes = 86;void setNodeActive(std::vector<bool>& nodeStruct, int nNode);// =======================// Create the nodes// =======================void	Nodes_Create(){	for (int i=0; i < SizeOfNodes; ++i)	{             vNodesActive.push_back(false);//Set all the values to false	}       //so the 1st node is at vNodesActive[0]       //When all nodes have been sent to false set the 1st node to true	vNodesActive[0]=true;}// =======================// Set the nodes active// =======================void setNodeActive(std::vector<bool>& nodeStruct, int nNode){	for(int i = 0; i < SizeOfNodes; ++i)//loop through all the nodes		nodeStruct = false;//set all the nodes to false	nodeStruct[nNode] = true;//set the selected node to true}// ==========================// Some code on how to use it// ==========================	if (vNodesActive[1] = true)		{		setNodeActive(vNodesActive, 2);	}

[Edited by - Prog101 on November 10, 2006 3:29:12 AM]

##### Share on other sites
There is an error in your code.

void setNodeActive(std::vector<bool>& nodeStruct, int nNode)
{
for(int i = 0; i < SizeOfNodes; ++i)
nodeStruct[nNode] = false; <<<******
nodeStruct[nNode] = true;
}

should be

void setNodeActive(std::vector<bool>& nodeStruct, int nNode)
{
for(int i = 0; i < SizeOfNodes; ++i)
nodeStruct = false;
nodeStruct[nNode] = true;
}