| rjp52193 (6) | |
|
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 } | |
|
|
|
| vlad from moscow (3105) | |
| You showed unrelevant code. Please show a simple example that everybody could compile it and get the error message. | |
|
|
|
| Raman009 (124) | |||
|
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
I don't think in case of global variable , it will give any error . Try posting the complete code. Hope that helps | |||
|
|
|||
| rjp52193 (6) | |
| 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 | |
|
|
|
| L B (3325) | |
| 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. | |
|
|
|