error C2059: syntax error : '='

Started by
4 comments, last by MaulingMonkey 13 years, 6 months ago
I'm trying to do simple math here but it keeps on giving me error C2059, what's up with that?


#include <iostream>
using namespace std;

int main()
{
int length;
int width;
int area;

length = 4;
width = 2;

area = length * width;

cout << "Area is ";
cout << area;

return = 0;
}

Advertisement
'return' is not a variable, it's a keyword. replace the last line with 'return 0;'
Oh my God, you are right, thanks a lot.
Quote:Original post by Promethium
'return' is not a variable, it's a keyword. replace the last line with 'return 0;'
Getting a little more advanced here, but its good to know none-the-less: You don't need any return statement at the end of main(). main() is a special-case function where 'return 0;' is implicit at the end of the function. That is, this is allowed:
int main() {int length = 4;int width = 2;int area = length * width;cout << "Area is " << area << "\n";}
Any other function with a return type that's not 'void' requires you return something. Also, and just a reminder, main() must always return an int to be valid C++. void main() may work but it's not standard.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Ignore the error codes, just look at the line number.
Quote:Original post by pinacolada
Ignore the error codes, just look at the line number.

The error message is often useful, although I'll agree about the error code.

D4rk_Angel: For future reference, in general, whenever you ask for help with an error, you should always copy and paste two things:

1) The exact error message and where it occurs, even if it doesn't seem to make any sense (a simple comment will work.)
2) The exact code you're using. Cleaning up or transcribing it can often fix or hide the actual problem. You see to already be showing the exact code, so this is more to say good job and keep doing so :)

This topic is closed to new replies.

Advertisement