Volume of Cone

Note - This IS a homework question, so just hints.

Problem - Write a C++ program that computes and outputs the volume of a cone, given the diameter of its base and its height. Formula: 1/3 pi x radius squared x height

Confused On: How to label radius, volume and pi, and how to connect them to the User Input. If I can have one example of only one of this, I could probably work out the other two.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*Cone Program
Computes and outputs the volume of a cone, given the diameter of it's base and height.
*/

//Starting code.

#include <ionstream>
#include <string>
using namespace std;

int main ()
{

//User inputs cone's radius.

cout << "Input cone's radius.";
cin >> r;

//User inputs cone's height.

cout << "Input cone's height.";
cin >>  ;

//Declares pi, r, h, v.

const float pi = 3.14159;
float = r
float = h
float = v

//Caculates the cone's volume.

v = (1/3) * pi * (r*r) * h

//Output to screen.

	cout << "The volume of the cone is " << v;

return 0;
}


I know, probably errors galore. XP
Last edited on
Not to sure on what exactly your asking, but a few things i noticed:

You should be declaring your variables after the program starts at main, otherwise at compile time, the compiler wont know what r,h and v are. I imagine this might give you some errors.
Also, line 22... theres no variable next to cin...

Edit: You've also include <string> which you dont seem to be using
Last edited on
I would suggest reading this:

http://www.cplusplus.com/doc/tutorial/variables/

Other then that and what MYST said, I think you are OK.
Thank you guys. I moved the variables up after main, fixed line 22 and removed <string>.

As for the tutorial, I have very similar stuff in my textbook and online sections, but to be honest I'm not sure where to start to fix the code. I thought I had "declared" them "float". (This is what I'm trying to understand, MYST.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*Cone Program
Computes and outputs the volume of a cone, given the diameter of it's base and height.
Formula = 1/3 PI x Radius Squared x Height
*/

//Starting code.

#include <ionstream>
using namespace std;

int main ()
{

//Declares pi, r, h, v.

const float pi = 3.14159;
float = r
float = h
float = v

//User inputs cone's radius.

cout << "Input cone's radius.";
cin >> r;

//User inputs cone's height.

cout << "Input cone's height.";
cin >>  h;

//Caculates the cone's volume.

v = (1/3) * pi * (r*r) * h

//Output to screen.

	cout << "The volume of the cone is " << v;

return 0;
}


Debug Messages:
1>------ Build started: Project: MannonDavisCone, Configuration: Debug Win32 ------
1>Compiling...
1>MannonDavisConeCode.cpp
1>c:\users\mannon\documents\visual studio 2008\projects\mannondaviscone\mannondaviscone\mannondavisconecode.cpp(8) : fatal error C1083: Cannot open include file: 'ionstream': No such file or directory
1>Build log was saved at "file://c:\Users\MANNON\Documents\Visual Studio 2008\Projects\MannonDavisCone\MannonDavisCone\Debug\BuildLog.htm"
1>MannonDavisCone - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Last edited on
Haha sorry mate, just realized, you've put <ionstream>... should be <iostream>

Bloody typo's eh? :)

Edit: just compiled the code, and got lots of errors after correcting it to <iostream>

I suggest you change your variables to the following:

1
2
3
4
//Declares pi, r, h, v.

float r, h, v;
const float pi = 3.14159;


You'll get a warning from the compiler but it runs. The actual calculation didnt work for me, kept saying the volume is 0. Im gunna leave that for you to sort out
Last edited on
Ahhhh!!!! Thank you!!! It's actually showing some errors now.

Question - I saw on some cone codes that you had to use a "double" for some of your numbers. Do I need to include that somehow in this program?

With iostream fixed, it reads as such:
(It keeps saying v, h, and r are undeclared. Can someone give me an example as one declared?)

1>------ Build started: Project: MannonDavisCone, Configuration: Debug Win32 ------
1>Compiling...
1>MannonDavisConeCode.cpp
1>c:\users\mannon\documents\visual studio 2008\projects\mannondaviscone\mannondaviscone\mannondavisconecode.cpp(16) : warning C4305: 'initializing' : truncation from 'double' to 'const float'
1>c:\users\mannon\documents\visual studio 2008\projects\mannondaviscone\mannondaviscone\mannondavisconecode.cpp(17) : error C2513: 'float' : no variable declared before '='
1>c:\users\mannon\documents\visual studio 2008\projects\mannondaviscone\mannondaviscone\mannondavisconecode.cpp(18) : error C2065: 'r' : undeclared identifier
1>c:\users\mannon\documents\visual studio 2008\projects\mannondaviscone\mannondaviscone\mannondavisconecode.cpp(18) : error C2144: syntax error : 'float' should be preceded by ';'
1>c:\users\mannon\documents\visual studio 2008\projects\mannondaviscone\mannondaviscone\mannondavisconecode.cpp(18) : error C2513: 'float' : no variable declared before '='
1>c:\users\mannon\documents\visual studio 2008\projects\mannondaviscone\mannondaviscone\mannondavisconecode.cpp(19) : error C2065: 'h' : undeclared identifier
1>c:\users\mannon\documents\visual studio 2008\projects\mannondaviscone\mannondaviscone\mannondavisconecode.cpp(19) : error C2144: syntax error : 'float' should be preceded by ';'
1>c:\users\mannon\documents\visual studio 2008\projects\mannondaviscone\mannondaviscone\mannondavisconecode.cpp(19) : error C2513: 'float' : no variable declared before '='
1>c:\users\mannon\documents\visual studio 2008\projects\mannondaviscone\mannondaviscone\mannondavisconecode.cpp(23) : error C2065: 'v' : undeclared identifier
1>c:\users\mannon\documents\visual studio 2008\projects\mannondaviscone\mannondaviscone\mannondavisconecode.cpp(23) : error C2146: syntax error : missing ';' before identifier 'cout'
1>c:\users\mannon\documents\visual studio 2008\projects\mannondaviscone\mannondaviscone\mannondavisconecode.cpp(24) : error C2065: 'r' : undeclared identifier
1>c:\users\mannon\documents\visual studio 2008\projects\mannondaviscone\mannondaviscone\mannondavisconecode.cpp(29) : error C2065: 'h' : undeclared identifier
1>c:\users\mannon\documents\visual studio 2008\projects\mannondaviscone\mannondaviscone\mannondavisconecode.cpp(33) : error C2065: 'v' : undeclared identifier
1>c:\users\mannon\documents\visual studio 2008\projects\mannondaviscone\mannondaviscone\mannondavisconecode.cpp(33) : error C2065: 'r' : undeclared identifier
1>c:\users\mannon\documents\visual studio 2008\projects\mannondaviscone\mannondaviscone\mannondavisconecode.cpp(33) : error C2065: 'r' : undeclared identifier
1>c:\users\mannon\documents\visual studio 2008\projects\mannondaviscone\mannondaviscone\mannondavisconecode.cpp(37) : error C2065: 'h' : undeclared identifier
1>c:\users\mannon\documents\visual studio 2008\projects\mannondaviscone\mannondaviscone\mannondavisconecode.cpp(37) : error C2146: syntax error : missing ';' before identifier 'cout'
1>c:\users\mannon\documents\visual studio 2008\projects\mannondaviscone\mannondaviscone\mannondavisconecode.cpp(37) : error C2065: 'v' : undeclared identifier
1>Build log was saved at "file://c:\Users\MANNON\Documents\Visual Studio 2008\Projects\MannonDavisCone\MannonDavisCone\Debug\BuildLog.htm"
1>MannonDavisCone - 17 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
*see my previous post edit*
Thank you for your help MYST.

I'm down to seven errors, and I'll continue to try and work on it. : )
The reason you are getting zero is (1/3) which is exactly "integer value '1' divided by integer value '3'" which equals zero (three goes into one zero times).

To get the fractional value one-third, you need to explicitly use floats:

v = (1.0/3.0) * pi * (r*r) * h;

Hope this helps.
Last edited on
No problem mate.

and yeah Duoas fixed it.

The code that compiles correctly and executes perfectly is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//Starting code.

#include <iostream>
using namespace std;

int main ()
{

//Declares pi, r, h, v.

float r, h, v;
float pi = 3.14159;

//User inputs cone's radius.

cout << "Input cone's radius.";
cin >> r;

//User inputs cone's height.

cout << "Input cone's height.";
cin >>  h;

//Caculates the cone's volume.

 v = (1.0/3.0) * pi * (r*r) * h;

//Output to screen.

	cout << "The volume of the cone is\n\n " << v << "\n";

system ("PAUSE");

return 0;
}

Alright, thank you both. : )

I put this as solved.
Topic archived. No new replies allowed.