-1
if (iButton == 1)
    {
        for (int counter = 0; counter < 2; counter ++)
        {
            if(counter == 0)
            {
            currentTapRead = currentTapRead * 0.5;
            printf("1/2\n");
            }

            if(counter == 1)
            {
            currentTapRead = currentTapRead * 2;
            printf("1\n");
            }
        }
    }
}

Hi guys, im trying to make it so that when a button is tapped, it will divide something by 2, when the button is tapped again, it is multiplied by 2 then goes back to the start. the problem is when i hit the button it does them both at the same time. therefore does nothing.. I need to find a way to make the counter only count up 1 at a time instead of up the whole sequence however you cant just put count + 1 in the for loop.

Anyone have any ideas?

Haris
  • 12,120
  • 6
  • 43
  • 70

1 Answers1

1

Your code should not be in a loop, but should be executed only once when the button is tapped. The counter variable should be static, so that it holds its value between each tap.

Richard St-Cyr
  • 970
  • 1
  • 8
  • 14
  • how would i make the counter variable static? – Luke Nicholls Dec 05 '15 at 16:34
  • The best way to do it depends a lot on your program structure. The easiest way might be to declare it with the static keyword in the routine where it is used. You should then set it to 1 in the first if (counter == 0) and to 0 in the second if (counter == 1). See [this post](http://stackoverflow.com/questions/6223355/static-variables-in-class-methods) for additional details. – Richard St-Cyr Dec 06 '15 at 15:56