Global Variables in a For Loop

Hey, I have a quick question. When I try to compile this program that has a global variable declared at the beginning, the variable works in all cases except when used in a for loop. It comes up as "variable counter undeclared". What can I do to fix this? (And don't tell me global variables are evil, I'm supposed to use them for this assignment)

int counter;
int main()
{
for(counter=0;counter<45;counter++)
etc etc
}
int function()
{
for(counter=0;counter<20;counter++)
etc etc
}
You showed unrelevant code. Please show a simple example that everybody could compile it and get the error message.
1. Why would you do something like that ? It's irrelevant and confuses
2. Code should be written in such a manner that it is easier for humans and optimized for machine . So this way of writing things is weird , I guess

I always use something like
1
2
3
4
5
6
7
for(int i=0 ; i<size ; i++){
 // content
}

int j
for(j =0 ; j<size ; j++ ){
}


I don't think in case of global variable , it will give any error .
Try posting the complete code.
Hope that helps
I figured out the problem. I was using the word "count" instead of "counter" which for some reason I guess is a word we're not allowed to use as a variable name. Oh well
If somewhere you wrote "using namespace std;" you should know that that there is a function std::count that you are bringing into the global namespace.
Topic archived. No new replies allowed.