Totally new to C++, can't seem to get this simple code to work?

#include <iostream>

int main() */
{ double radius = 11; /* centimeters;
double pi = 3.14
double sphere_volume = (4/3)*pi*(radius * radius * radius);
double surface_area = 5 * pi * radius;
cout << "Volume = " << sphere_volume;
cout << "Area = " << surface_area;
return 0;
}


I have some error telling me about my int main(), I do not know what it means, I found this code and am just trying to fix it up for practice. Can someone show me how to fix it? I hate getting stuck
Last edited on
closed account (zb0S216C)
The comment on the second line begins, but it does not end. You need a corresponding "*/" to complete the comment block. Otherwise, all subsequent code is considered a comment, including the closing brace of
"main( )" -- without the closing brace, "main( )" is not complete.

Wazzak
Last edited on
{ double radius = 11; /* centimeters

/* is a box comment, not a line comment. If you don't close it you're commenting out all the rest of your code
Does this fix the code? I'm am not at a school computer in linux to compile and see.
EDIT: the "*/" at the end of the code is automatically written by the site, I don't know how to remove it

1
2
3
4
5
6
7
8
9
int main() */
{ double radius = 11; /* centimeters;
double pi = 3.14
double sphere_volume = (4/3)*pi*(radius * radius * radius);
double surface_area = 5 * pi * radius;
cout << "Volume = " << sphere_volume;
cout << "Area = " << surface_area;
return 0;
} */

Line 1: you have a close-comment that will throw an error
Line 2: you need to close that box comment. Just move the one from line one after "centimeters". Also, the semicolon is not needed since a comment is not an instruction
Line 3: missing ;
Line 4: (4/3) will evaluate to 1, because you're dividing two integers. Just writing 4.0/3.0 will suffice to evaluate them as floats
Line 6-7: just using cout will throw an error. You can:
- Write "std::cout" every time you need to use it. This is what I personally prefer
- Put "using namespace std;" before your main. Tip for the future: never do this in header files, it's bad practice
- Put "using std::cout;" before your main. This will allow you to avoid writing std:: before cout, but ONLY before cout. Avoid in headers

Little note: as of now the output to the console will be (random numbers)
Volume = 1986Area = 454

It will look nicer if you write
1
2
cout << "Volume = " << sphere_volume << std::endl;
cout << "Area = " << surface_area << std::endl;

You can see std::endl as the C++ way to put '\n' in strings
Last edited on
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
int main() 
{ 
const float pi = 3.14;
float centimeters, radius = 11, sphere_volume = (4/3)*pi*(radius*radius*radius), surface_area = 5*pi*radius;
cout << "Volume = " << sphere_volume <<endl;
cout << "Area = " << surface_area <<endl;
cin.get ();
return 0;
}


This should perform correctly. Your idea behind your program was good, and you only had syntax errors. I didn't see totally wrong, but so you remember not to do them again, here they are.

1) comment syntax is /* comment */ anything between the slash and star are discarded by the compiler. this is a block comment.
2) You need to the line "using namespace std;" after the header file <iostream>. this tells the compiler that you are using the standard input and output procedures.
3) You have forgotten some ";". They tell the compiler that the statement is over. This is extremely important, and the most common error, and I do it too xD.

Other then that, you did excellent.
I added a couple things to spice up your program.

You can do multiple declarations by separating your variables with commas.
Ex. type var1, var2, var3;
int weight, height;
You can also initialize the variables by separating them with commas too.
ex. type var1 = 0, var 2 =1, var3 = 999;
int height = 62, weight = 90;
At the end of your cout <<""; statement you may have noticed the <<endl; This just gives a new line to the console.
cin.get (); is a function you use with strings, but without any parameters, it just waits for a key to be pressed.
Good work, keep it up.

maeriden has the same point, and since he has more experience, take advice from him! xD
Last edited on
Did anyone mention to the OP about line comments?

double radius = 11; // centimeters

The // starts the comment & it continues until the end of the line.

If you include the math header, then you can use M_PI directly, rather than declaring your own value for pi.

Edit:

I prefer to declare variables on there own line, so you can comment their meaning or other info like units or range etc.
Last edited on
Thank you guys so much, each of you made it super clear for me, now I have the right code and understand it much better!
Glad to help, anytime.
learn more about syntax and C++ basics too :)
Topic archived. No new replies allowed.