My teacher gave me an assignment to make a cooties program
* rollDice which rolls the dice and returns a value
* applyRoll which takes the roll and adds the correct piece
* addHead, addBody, addEyes, etc which add the appropriate piece to the appropriate variable
* cootieComplete return 1 (true) if the cootie is finished, 0 (false) otherwise
* printCootie which tells what pieces are in the cootie so far
You main should look similar to:
do {
rollDice();
applyRoll();
printCootie();
while (!cootieComplete()) ;
Of course, you'll have lots and lots of variables in the above code because GLOBALS ARE BAD!!
Especially for small programs like yours the source code might be easier to understand and if you use unambiguous variable names you will barely ever meet any of the potential problems of global variables.
What kind of help do you still need? Just realise what you have listened above.
I didn't say he should use globals all the time, but globals should be used if they are appropriate.
In the case that it is a variable used throughout the whole program it is often better to use a global variable than eg passing a pointer from function to function. Regarding both, performance and readability of your source code.
//It's not the same to have
void function(T var){
var=/*...*/;
}
//than
void function(){
var=/*...*/;
}
If you had a large enough project with the latter, could you tell what 'var' is without doing a global search?
Performance decreases by no more than one level of indirection when passing by reference.
I never said that globals shouldn't be used. I said that things should be kept in the smallest possible scope. If you can avoid using globals then do so. In OP's case, using globals can only hurt.