Thursday, July 30, 2009

What is wrong with my c++ code?

I can't for the life of me figure out what is wrong with this.





for(int y=0; y%26lt;FileLength; y++)


{


for(int z=0; z%26lt;254; z++); //(b)


{


if(FileData[y] == KeyCodes[z]) //(a)


cout %26lt;%26lt; (char)z;


};


};





I get this error when compiling:


(a) name lookup of `z' changed for new ISO `for' scoping


(b) using obsolete binding at `z'





a and b indicate the lines that the errors point to in my compiler. I am using the Dev-C++ 4.9.9.2 beta.


Any help is appreciated thanks.

What is wrong with my c++ code?
i'm a rookie, but are you sure there is supposed to be a ; (semicolen) ending line (b) ?





isn't line (a) supposed to end in { for the statement on the next line?





for(int y=0; y%26lt;FileLength; y++)


{


for(int z=0; z%26lt;254; z++) //(b)


{


if(FileData[y] == KeyCodes[z]) { //(a)


cout %26lt;%26lt; (char)z; };


};


};
Reply:I suspect there's another definition of z earlier in the function. I'd change the variable in the loop.





Hope that helps.





Edit: The next answerer pointed out the problem: The stray semicolon provides the loop with an empty statement, therefore the now-detached if statement following it is using an obsolete variable.(z).
Reply:That dev c++ version is in beta I've had similar problems, you should just use a later version, and listen to cluck, his code seems to work.


No comments:

Post a Comment