changing variable within loop - so this is not possible?

hi is anyone able to help me on this?
for example if i have code like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>

using namespace std;

int main() {
bool test1 , test2 ;
int a = 0; int b = 0;
for (int i = 0 ; i < 60 ; i++){
 if ( i == 0){ test1 = true; }
 if ( test1 == true){
  if(a<=10){ a++; }
  if(a > 10) {
  b = 0;
  test2 = true;
  test1 = false;
cout<<"changed to light B at time "<<i<<endl;
  }
  }
/////////////HEERE//////////// if test 2 is changed to true should be true from this point on
 if ( test2 == true){
  if(b<=10){ b++; } // modified
  if(b > 10) {
  a = 0;
  test1 = true;
  test2 = false;
cout<<"changed to light A at time "<<i<<endl;
  }
 }
}
}

the result of a and b changing is when i equals 0 10 20 31 41 52 62
The question is at the line of my comment //here// the value changed from
test1 true to false and test2 false to true but when reach to check if test2
is true it is still false since the change will only work next time we come to loop.
is there a way when we reach line here the test2 will be true.(originally when entering loop test2 is false).
Im sorry for bad explaination. any help will be nice.
thank you
Last edited on
If the question is not possible is there alternative way to achieve what i want?
I can't figure out what you're trying to do and/or what your problem is.

Can you try to clarify it a little more?

Meaningful variable names, comments, or an explanation of what the code is doing would help.

Also if you could tell us what behavior you expect vs. what behavior you're getting.
Im trying to simulate traffic light system.
so the variables would be test1 = traffic light A is green
test2 = traffic light B is green ( omit orange lights)
so say for 100 seconds
 
if ( i == 0){ test1 = true; } // start by setting trafic light A to green 

1
2
3
4
5
6
7
 if ( test1 == true){ // if trafic light A is green
  if(a<=10){ a++; } // if we did not reach 10 sec a++
  if(a > 10) { //if we dd reach 10 sec 
  b = 0; // set counter for trafic light B to 0;
  test2 = true; // turn on light B
  test1 = false; // turn off light A
  }

this code means if the traffic light A is green then let the light run for 10 seconds
and once it reaches 10 seconds turn traffic light A off and turn on traffic light B.

and same for traffic light B. if its on let it run for 10 seconds and after 10 seconds turn
off light B and turn on light A.

what i expact the is traffic light to be turned on
0 sec - A turn on
10 sec - B turn on
20 sec - A trn on
and so on 30 40 50 60 70 80 90 100.

what im getting is
0 sec - A turn on
10 sec - B turn on
20 sec - A turn on
31sec - B turn on
41 sec - A turn on
52 sec - A turn on
62 sec - B turn on


So i need help with when reaching /////HERE/// line what ever variable changed above to
effect below inside loop.(I know it will change once out of loop and re-entering loop )
Hope I explained it well. please let me know if i need to explain more :)
thank you for helping me out.!! ^^

PS: 10 sec is just sample i will later try to figure out minimum times for both queue

Last edited on
take a look at line 14. You made an "copy-and-paste" error there.

Btw. your "results" are not possible with the current code.
It should rather be:
0 sec - A turn on
10 sec - B turn on
(no change from here on)
Last edited on
ur right :) thanks for pointing that out. let me change it

my current code it what i just typed on top of my head to
try and explain what i wish to do. :(
i got on top of my for loop some code like
1
2
if (test1 == true ){ test2 = false;}
if (test2 == true ){ test1 = false;}

if thats what u mean.
But what im asking is how to change variable inside loop perminantly.
so as loop continues, it reads changed variable.
Last edited on
I just meant that when b got never incremented, test1 would never be true again after the inital setting when i==0.
i think test1 sould be true if
1. i == 0
2. if b > 10
and b will increment when test2 == true and if(b<=10)
 
if(b<=10){ b++; } 

am i missing something?
Last edited on
after the change of line 14, it should be ok.
Simply test it ;-).
I changed the code so it can be copy and pasted.
the result im getting is

changed to light B at time 10
changed to light A at time 20
changed to light B at time 31
changed to light A at time 41
changed to light B at time 52


what i want is the numbers to be 10 20 30 40 50 60
but not changing number 10 inside code
anyone know how to solve this?
Last edited on
since no ones posting reply on this im guessing this is not possible.
can someone just tell me if its possible or not possible.
It's possible.
Surely your for loop should just be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if(i%10 == 0)
{
	if(test1 == true)
	{
		test1 = false;
		test2 = true;
		cout << "Changed to light B at time " + i;
	}
	else
	{
		test2 = false;
		test1 = true;
		cout << "Changed to light A at time " + i;
	}
}


Or am I not understanding?

May be a few typos, but the logic should be sound.
Last edited on
the values of a and b should be changable
like a = 10 b = 20
I found a way to solve my probelm. Thank you all who helped me :)
For those who come across the same question. I solved it by looking at when is
(test2 == true) and relised its true when (test1 == true) && (a > 10)
so line 11 and 22 would change to
11 if ( test1 == true || (test2 == true && b > 10)){
22 if ( test2 == true || (test1 == true && a > 10)){
simple when u think abou it :)
Last edited on
Topic archived. No new replies allowed.