Finding volume or surface area of a sphere

Need help. Cant seem to get this to work. The program is suppose to find the volume or surface area of a sphere with a radius input.

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  #include <iostream>
#include <iomanip>
#include <cmath>
#include <math.h>

using namespace std;

char s = '\0';
const char SENTINEL = 's';

float radius, answer;

void get_radius (float&);
float surface_area (float);
float volume (float);
float cross_section (float);

const float PI = 3.14;

int main()
{
    cout << "This program will let you input the radius of a sphere to find its volume or surface area." << endl << endl;
    cout << "Enter 'v' for volume or 'a' for surface area of a sphere" << endl;
    cout << "'s' to stop" << endl;
    cin >> s;
    while (s != SENTINEL)
    {
        get_radius (radius);
        
        if(s == 'V')
        {
            volume (radius);
        }
        else if(s == 'A')
        {
            surface_area (radius);
        }
        
        cout << "Enter 'v' for volume or 'a' for surface area of a sphere" << endl;
        cout << "'s' to stop" << endl;
        cin >> s;
    }
    
    system("PAUSE");
    return 0;
}
void get_radius (float& radius)
{
    cout << "Please enter the radius of the sphere: " << endl;
    cin >> radius;
}

float volume (float radius){
    float answer;
    answer = 4.0/3.0 * PI * pow (radius, 3);
    cout << "The volume is: " << answer << endl;
}
float surface_area (float radius){
    float answer;
    answer =  4.0 * PI * pow(radius, 2);
    cout << "The surface area is: " << answer << endl;
}
Next time you can be more specific about what exactly isn't working. The only thing I would change to the code is to add return answer at the end of your volume function and the surface_area function. Just saying, at line 30 and 34 you check for a capital letter.
Adding a return value for the two functions will not solve the problem even though it should not compile for that reason. Since the answer is being printed in the functions, just make then void. There is no reason to return anything as they are written.

Checking for only capital letters is the real problem. Without doing error checking on input, it only makes sense to use lowercase letters as the argument since most people will not use shift or have caps lock on.
Topic archived. No new replies allowed.