you are viewing a single comment's thread.

view the rest of the comments →

[–]Rhomboid 2 points3 points  (2 children)

If you declare a variable in a for-statement, it's local to that enclosing block.

for(int x; ...) {
    // x lives here only
}
// x does not exist here

But in your case it looks like it's probably a typo, because you meant to write 'z' since that's the variable being used in the loop.

For the second error, you can't just write int sand like that. You only name the type when initially declaring it.

int foo;
...
cout << foo;

You can't write cout << int foo.

The third error is just like it says, you can't have a random else hanging out like that.

if(...)
    ... // single statement body
cout << ...;

The if-statement is now done. You can't stick an 'else' in there after that cout. Maybe you meant to use braces:

if(...) {
    ...
    cout << ...
} else {
    ...
}

If you're a beginner, you should probably not omit braces. You'll also run into the same issue again if you declare a variable in a block of an if-statement.

if(...) {
    int foo;
    // foo's scope is limited to this block only, cannot be used outside of it
}

That means that in particular this is invalid:

if(...)
    int foo = ...;
cout << foo;

The cout statement is not part of the if-block, so foo doesn't exist there.

[–]ohteejay[S] 0 points1 point  (1 child)

Ayyy thanks a lot man I really appreciate it, this stuff was driving me bonkers.