You need to declare a default value for
a before your loop starts. This way it's not destroyed after the loop restarts. After setting the variable to
a=1 it will thus stay at 1 until you change it again.
If you need an on/off toggle, it's easier, cleaner and more efficient to use a
bool. Then you can simply write
bool a = ...;
while (...) {
if (keypressed)
a = !a;
}
If you have more than 2 toggles, a clean way to write it could be
int a = ...;
while (...) {
if (keypressed)
a = (a+1)%N;
}
where
N is the number of states and
% is the modulus operator.
The easiest way would be to use event and change it to 1 when keypressed is triggered and to 0 if keyrelease is triggere.
If you're just interested in 0 and 1, which could be represented as boolean, theb you could use sf::Keyboard::isKeyPressed() directly, since that returns true if pressed, false otherwise.
I think you are misinterpreting. He meant to toggle a value only when a key is pressed, when released it should stay that value. But it is indeed safer to use events rather than real-time input in case of a toggle.