Skip to main content
gamedev.net
🔒 Locked

[.net] Problem with very simple C++ code

Started by jt419oh 2 replies 1.1k views
Original Post
jt419oh
jt419oh
I do not understand the error I get with this very simple code. #include using namespace std; void main() { int acreSqFeet = 43,560; int totalSqFeet = 389,767; int totalAcres; // define as int to get rid of decimal places totalAcres = totalSqFeet / acreSqFeet; cout << "A tract of land with " << totalSqFeet << " feet is " << totalAcres << "acres\n"; } The error is as follows Compiling... acreage.cpp .\acreage.cpp(5) : error C2059: syntax error : 'constant' .\acreage.cpp(6) : error C2059: syntax error : 'constant' P.S. IS there any special tags I can wrap my code in to make it more readable?
jouley
jouley
When giving values to variables in C++ (and most other languages), the compiler expects the numbers to be just that, numbers. Including things like formatting throws them off. In this case, this corresponds to the commas you've included in your integer initialization. That is, instead of
int acreSqFeet = 43,560;int totalSqFeet = 389,767;// should beint acreSqFeet = 43560;int totalSqFeet = 389767;

Other than that, it looks good, allowing you've realized the integer truncation that will take place.

Best of luck!
-jouley

P.S. - The code should go in [ code ] and [ /code ] blocks or, if the code is longer, [ source ] and [ /source ] blocks, without the spaces. [smile]
Nytegard
Nytegard
Take the comma's out of your code, they mean something completely different.

As far as making your code more readable, put it in source tags.

[source]cout << "Hello World" << endl;[/source]

comes out to look like

cout << "Hello World" << endl;
jt419oh
jt419oh
Thanks for the quick responses guys. Worked perfectly.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.