I have 4 variables called check1, check2, check3 & check4.
I wish to apply the same code to all of them in turn.
Is there any way I can do this in a loop by incrementing the number of the variable name (i.e. checkx) or do I really need to have the same code for each variable (i.e. the same code 4 times)?
as they stand now ..... NO
if you define an array called check with 4 elements you could use a loop and assign in the loop like this....
check[i]=0; // where i is your loop variable.
depending on your compiler, you can make use of the precompiler directives to accomplish what you want.
use the number define as follows:
#define adder( x, y ) ( check##x += y )
And in your code:
adder( 1, 4 ); // adds 4 to check1
adder( 3, 2 ); // adds 2 to check3
well pointed out. I forgot about token pasting, a feature of c/c++ that I haven't used for years. But I would still prefer the array way.