Help with learning C++

If you know the base radius and height of a right circular cone, then you can calculate the surface area (A), slant height(s), and volume (V) of the cone using the following formulas.
A = PI*r^2 + PI*r*s
s = sqrt(r^2 + h^2)
V = (1/3)PI*r^2*h

where:
π (pi) is a constant with a value approximately equal to 3.14159
r is the radius of the cone base
h is the height of the cone
s is the slant height of the cone

Note that the slant height is the hypotenuse of the right triangle, whose other two sides are the height and radius of the cone. So it can also be calculated by just knowing the radius and height of the cone.

Requirements:

Write a program that will:
• Define any fixed values as constants.
• Display a program description to the user before asking for any input
• Read the radius and height of a cone as input from the user
• Calculate the volume of the cone using the above formula
o Use the pre-defined pow function from the cmath library in the volume calculation
• Calculate the slant height of the cone using the above formula
o Use the pre-defined sqrt function from the cmath library in the slant height calculation
• Using the slant height calculated above, calculate the surface area of the cone using the above formula
• Write and call a separate, user-defined function to output the results
o The function should have four input parameters: the cone radius, height, volume and surface area
o From within the user-defined function, display the radius, height, volume and surface area of the cone formatted as shown below, each rounded to 1 decimal place. The decimals of the volume and surface area should line up.
o No calculations should be performed from within this function.
So far, I have this:

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
41
42
43
44
45
#include <iostream>                    // for I/O
#include <iomanip>                     // for formatting output
#include <cmath>                       // for using pow function
using namespace std;

// global constants
const float PI = 3.14159;              // Approximation of Pi

int main()
{
    float surfaceArea;
    float slantHeight;
    float volume;
    float radius;
    float height;
    
    cout << "Cone Calculations" << endl;
    cout << endl;
    
    cout << "Enter cone radius (in meters): ";
    cin >> radius;
    
    cout << "Enter cone height (in meters): ";
    cin >> height;
    cout << endl;
    
    cout << endl;
    
    cout << "RESULTS:" << endl;
    
    cout << "For a cone with a radius of " << radius << " meters and a heaight of "
         << height << " meters" << endl;
    cout << endl;
    
    surfaceArea = ((PI * pow(radius, 2)) + (PI * radius * slantHeight));
    slantHeight = sqrt((pow(radius, 2)) + (pow(height, 2)));
    cout << "Cone Volume: " << volume << endl;
    
    volume = ((1 / 3) * PI * (pow(radius, 2)) * height);
    cout << "Cone Surface Area: " << surfaceArea << endl;
    

    system("PAUSE");
    return EXIT_SUCCESS;
}




I am having issues getting the correct results though. They are not in cubic meters or square meters. Maybe I put the formulas in wrong? Any help would be appreciated!
I also know I haven't used setprecision yet in order to set the decimal places where I want them, nor have I used setw to align anything. I am hoping to get to that asap and will update this so you guys have more to hack away at. Any advice is helpful! Thanks!
Last edited on
Please start code with [code] and end it with [ /code] (without the space), it makes it a lot easier to read.
Better? :) Thank you for the tip!
I think the problem is that you're defining volume AFTER you try to output it. So you tell the program to say what volume is, then it calculates volume.

(Side note: use cin.get(), cin.peek(), or cin.ignore() instead of system("pause"), I suggest.)
BINGO! I didn't realize I had accidentally put the formulas in the wrong places. A fresh set of eyes always helps!

The only thing I am missing now...is:
Display a program description to the user before asking for any input
and
Write and call a separate, user-defined function to output the results
Topic archived. No new replies allowed.