Calculate the surface area, volume, or cross sectional area of a sphere

I somehow managed to get stuck in an infinite loop and i have no idea how to fix it.

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <iomanip>
#include <cmath>
#include <math.h>

using namespace std;

char q = '\0';       
 const char SENTINEL = 'q';
 
 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 << "Enter 'V' for volume, 'A' for surface area, 'X' for cross-sectional area of a" << endl;
    cout << "sphere:" << endl;
    cout << " " << endl;
    cout << "'q' to quit" << endl;
    cin >> q;
        while (q != SENTINEL)                         
 {
      get_radius (radius);                                       
      
      if(q == 'V')                         
      {
          volume (radius);                 
      }
      else if(q == 'A')
      {
          surface_area (radius);           
      }
      else if(q == 'X')
      {
         cross_section (radius);        
      }
      cout << "Enter 'V' for volume, 'A' for surface area, 'X' for cross-sectional area of a" << endl;
      cout << "sphere:" << endl;
      cout << " " << endl;
      cout << "'Q' to quit" << endl; 
 }

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;
}

float cross_section (float radius)    
{
   float answer;   
   answer =  PI * pow(radius, 2);
   cout << "The cross-section is: " << answer << endl;
}
Look what your while loop parameters are: q != SENTINEL, where in your code do you modify q? If q never changed and remains constant then the while loop is always true. This gives you the infinite loop you're stuck in. you need to move the prompt
1
2
3
4
5
   cout << "Enter 'V' for volume, 'A' for surface area, 'X' for cross-sectional area of a" << endl;
    cout << "sphere:" << endl;
    cout << " " << endl;
    cout << "'q' to quit" << endl;
    cin >> q;
INSIDE your while loop, so q is constantly modified every time the program loops through.
You didn't get a new value for 'q' inside the while loop. Do another cin >> q; at the bottom of the while loop.
Yes i saw that now. Thank you for your help. I ran into another problem though. When i enter in the radius it will loop back around and not calculate.

Enter 'V' for volume, 'A' for surface area, 'X' for cross-sectional area of a
sphere:

'q' to quit
V
Please enter the radius of the sphere:
34.8
Enter 'V' for volume, 'A' for surface area, 'X' for cross-sectional area of a
sphere:

'q' to quit



Nevermind. I figured out what was wrong. Thanks again for the help.
Last edited on
Topic archived. No new replies allowed.