issues with loop

I'm trying to write a program that will compute the volume and surface area of
a sphere, cylinder, and a circular cone using function and array. I'm a beginner in C++. I need serious help. I'm struggling with the looping part. I try many syntax without success. I spent already 3 days on it. anything that will guide me in the right direction will be greatly appreciate.

example of the output:

Select a Shape (1) sphere (2) cylinder (3) cone (q) quit: 1
Select a Computation (1) volume (2) surface area: 1
Enter radius of sphere: 5.5
Volume of sphere is 696.91

Select a Shape (1) sphere (2) cylinder (3) cone (q) quit: 1
Select a Computation (1) volume (2) surface area: 2
Enter radius of sphere: 5.5
Surface area of sphere is 380.133

Select a Shape (1) sphere (2) cylinder (3) cone (q) quit: 2
Select a Computation (1) volume (2) surface area: 1
Enter radius of cylinder: 5.5
Enter height of cylinder: 4.2
Volume of cylinder is 399.139

Select a Shape (1) sphere (2) cylinder (3) cone (q) quit: 2
Select a Computation (1) volume (2) surface area: 2
Enter radius of cylinder: 5.5
Enter height of cylinder: 4.2
Surface area of cylinder is 335.208

Select a Shape (1) sphere (2) cylinder (3) cone (q) quit: 3
Select a Computation (1) volume (2) surface area: 1
Enter radius of cone: 5.5
Enter height of cone: 4.2
Volume of cone is 133.046

Select a Shape (1) sphere (2) cylinder (3) cone (q) quit: 3
Select a Computation (1) volume (2) surface area: 2
Enter radius of cone: 5.5
Enter height of cone: 4.2
Surface area of cone is 214.607

Select a Shape (1) sphere (2) cylinder (3) cone (q) quit: q

Good bye!

here are my codes:
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include<iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath> //math library function

using namespace std;

const double PI = 3.14159; //constant variable for PI

      //prototype functions
      double sphere_Volume(double radius); 
      double cylinder_volume(double radius, double height);
      double cone_volume(double radius, double height);
      double sphere_surface_area(double radius);
      double cylinder_surface_area(double radius, double height);
      double cone_surface_area(double radius, double height);
    
      

int main( )
{
    //declare variables
    double radius;
    double height;
    char select = 'q';   
    
        
   const int SHAPES = 3;  // length of array for the shapes (sphere, cylinder, cone)
   string shape[SHAPES];  //array of strings, size 3
   int input = -1; // store user inputs for the shapes
   shape[0] = "sphere"; // arrays from zero to 2
   shape[1] = "cylinder";
   shape[2] = "cone";
      
   const int COMPUTATION = 2; //length of array for the computation (volume, surface-area)
    string comp[COMPUTATION]; //array of strings, size 2
    int inputcomp = -1; //store user inputs for the computation
    comp[0] = "radius"; //arrays from zero to 1
    comp[1] = "height"; 
    
     // while (select != 'q') if 'q' is not select keep on running
     //{ my problem is here
  
       while(input < 1 || input > 3) //make sure user enter number between 1 and 3
       {
        //prompt user to select a shape  
        cout << "select a shape (1)sphere, (2) cylinder, (3) cone, (q) to quit: ";
        cin >> input; 
       } 
       while (inputcomp < 1 || inputcomp > 2)//make sure user enter number between 1 and 2
       {
        //prompt user to select a computation     
        cout << "Select a computation (1) volume, (2) surface area: ";
        cin >> inputcomp;  
       }
       if (input == 1 && inputcomp == 1) //if sphere and volume is select
       {
        cout << "Enter radius of sphere: "; //prompt user to enter radius
        cin >> radius;
        //call the function to display the sphere's volume
        cout << "Volume of the sphere is: " << sphere_Volume(radius) << endl;
        }
        else if (input == 2 && inputcomp == 1) //if cylinder and volume is select
        {
        cout << "Enter radius of cylinder: "; //prompt user to enter radius
        cin >> radius;
        cout << "Enter height of cylinder: "; //prompt user to enter height
        cin >> height;
        //call the function to display the cylinder's volume
        cout << "Volume of the cylinder is: " << cylinder_volume(radius, height) << endl;
        }
        else if (input == 3 && inputcomp == 1) //if cone and volume is select
        {
        cout << "Enter radius of cone: ";//prompt user to enter radius
        cin >> radius;
        cout << "Enter height of cone: ";//prompt user to enter height
        cin >> height;
        //call the function to display the cone's volume
        cout << "Volume of the cone is: " << cone_volume(radius, height) << endl;
        }
        else if (input == 1 && inputcomp == 2)//if sphere and surface-area is select
       {
        cout << "Enter radius of sphere: ";//prompt user to enter radius
        cin >> radius;
        //call the function to display the sphere's surface-area
        cout << "surface-area of the sphere is: " << sphere_surface_area(radius) << endl;
        }
        else if (input == 2 && inputcomp == 2)//if cylinder and surface-area is select
        {
        cout << "Enter radius of cylinder: ";//prompt user to enter radius
        cin >> radius;
        cout << "Enter height of cylinder: ";//prompt user to enter height
        cin >> height;
        //call the function to display the cylinder's surface-area
        cout << "surface-area of the cylinder is: " << cylinder_surface_area(radius, height) << endl;
        }
        else if (input == 3 && inputcomp == 2)//if cone and surface-area is select
        {
        cout << "Enter radius of cone: ";//prompt user to enter radius
        cin >> radius;
        cout << "Enter height of cone: ";//prompt user to enter height
        cin >> height;
        //call the function to display the cone's surface-area
        cout << "surface-area of the cone is: " << cone_surface_area(radius, height) << endl;
        }
    
    //}          
     

 

   system("pause");
   return 0;
} // end main function

  double sphere_Volume(double radius) 
      { 
       return 4.0 / 3.0 * PI * pow( radius, 3 ); 
      }
  double cylinder_volume(double radius, double height)
      { 
      return PI * pow(radius, 2) * height;
      }
  double cone_volume(double radius, double height)
      {
      return 1.0 / 3.0 * PI * pow(radius, 2) * height;
      }
  double sphere_surface_area(double radius)
      {
      return 4.0 * PI * pow(radius, 2);
      }
  double cylinder_surface_area(double radius, double height)
      {
      return (2.0 * PI * pow(radius, 2)) + (2.0 * PI * radius * height);
      }
  double cone_surface_area(double radius, double height)
      {
      return (PI * pow(radius, 2)) + (PI * radius * sqrt(pow(radius, 2) + pow(height, 2)));
      }
      


Last edited on
Try this idea:

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
char ShapeOption = '1';  //it is char because of the q option
char VolAreaOption = '1';

bool Quit = false;

while(!Quit) {

     ShapeMenu();  //call the function that shows the menu
      std::cin >> ShapeOption;

     VolAreaMenu(); //you should create these functions
     std::cin >> VolAreaOption;

     

     switch (ShapeOption) {
          case '1':
               CalcSphere(VolAreaOption);
          break;

          case '2':
                CalcCylinder(VolAreaOption);
          break;

          case '3':
                 CalcCone(VolAreaOption);
          break;

          case 'Q':
          case 'q':
               Quit = true;

          default:
             std::cout << "invalid input" << std::endl;
             break;
     }
}


You should also have a getHtRadius and getRadius functions. The getHtRadius function would call the getRadius function. You can send them an argument so you can specify in the cout statement which shape the question is about.

It seems there is no need for your shape array at this point.

Here is one thing a lot of people don't notice - numbers like (4.0 / 3.0) * PI and 2.0 * PI and 4.0 * PI and (1.0 / 3.0) * PI are constants, so you can work them out & declare them as constants along with PI. Some compilers already have these in the "math.h" which is included in <cmath> header, notably GNU compilers like gcc.

Speaking of const, you can put const after the function declaration & definition - which means the compiler will not change any of the arguments:

1
2
3
4
5
6

double sphere_Volume(double radius) const; //declaration

double sphere_Volume(double radius) const { 
       return 4_3PI * pow( radius, 3 ); 
}


You can also do this for arguments individually, and for the return value

One final thing - I try to break some bad habits early. Avoid using namepspace std;. It brings in the entire std namespace (the whole STL is in there) into the global namespace, polluting it, which can cause name conflicts. For example there is a std::distance.

So, either put std:: before each std thing, or do this:

1
2
3
4
5
6
7
8
//#includes
using namepspace std;

using std::cout;
using std::cin;
using std::endl;

using std::string


I do a mixture - the using statements for things that appear often, & std:: for those that appear a few times.

There you go - some stuff to work on. Cheers & have fun!
Last edited on
Topic archived. No new replies allowed.