Error in the code

Hello, in this code below I have divided the code into two Sections, so I can make things clear. In First section, I declared the variables and initialized them. In Second section, I am trying to write if statement in for loop. As you can see I want to generate random numbers from 0.0 to 1.0 (float V = rand->uniformReal(0.0,1.0);) and whenever this random number is smaller than 0.5 which is attackLevel, I get 1 in the last variable in location and else I get 0. Please Notice that Location with capital L is another class and this code is from the main.cpp file. Location class has location object with variables (float x, float y, float z). Basically I want the variable z to be 1 or 0 depending on the if statement.

when I run the program, I get this error message from the compiler "‘location’ was not declared in this scope"

Any help would be appreciated :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//First Section:
        float attackLevel = 0.5;
	t_uint numTags = 50;
	t_uint numReaders = 1;
//Second Section:
for(t_uint i = 0; i < numTags; ++i) {
		double locationFactor = numReaders;
float V = rand->uniformReal(0.0,1.0);
        if (V < attackLevel)
		Location location(rand->uniformReal(0.0, (locationFactor * 2.4)),0,1);
        else
        Location location(rand->uniformReal(0.0, (locationFactor * 2.4)),0,0);
        
        
     
		NodePtr tagNode = Node::create(location, NodeId(numReaders+i));

1
2
3
4
5
6
7
if (V < attackLevel)
   Location location(rand->uniformReal(0.0, (locationFactor * 2.4)),0,1);

//is the same as 
if (V < attackLevel){
   Location location(rand->uniformReal(0.0, (locationFactor * 2.4)),0,1);
} //end of scope, 'location' dies here 
that's why you can't access 'location' outside.

what you may do
1
2
3
4
5
6
double z;
if (V < attackLevel)
   z = 1;
else
   z = 0;
Location location(rand->uniformReal(0.0, (locationFactor * 2.4)),0,z);
ne555 Thank you so much, that was really helpful :)
Topic archived. No new replies allowed.