modular programming help

we are asked to apply functions(pass by value).
use a switch statement to select the shape we want to calculate the volume for.
then asked to use a while loop so the program does not accept negative numbers.
and only the shapes are to be considered based on the switch statement.
this is what i have so far......




using namespace std;

float cube_volume(float,float,float);
float rec_prism_volume(float,float,float);
float irr_prism_volume;
float cylinder_volume;
float pyramid_volume;
float cone_volume;
float sphere_volume;
float ellipsoid_volume;

int main()
{
int option;
float Cube;
float RecPrism;
float IrrPrism;
float Cylinder;
float Pyramid;
float Cone;
float Sphere;
float Ellipsoid;
float a;
float b;
float c;
float h;
float r;
float r1;
float r2;
float r3;
float a1;
float a2;
float a3;
float volume=0;
const float PI=3.14;

cout<<"Press 1 to calculate volume of a cube\n";
cout<<"Press 2 to calculate volume of a rectangular prism\n";
cout<<"Press 3 to calculate volume of an irregular prism\n";
cout<<"Press 4 to calculate volume of a cylinder\n";
cout<<"Press 5 to calculate volume of a pyramid\n";
cout<<"Press 6 to calculate volume of a cone\n";
cout<<"press 7 to calculate volume of a sphere\n";
cout<<"Press 8 to calculate volume of an ellipsoid\n";
cin>>option;

switch (option)
{

case 1:
{
float cube_volume;
cout<<"The volume of a cube is:"<<cube_volume<<endl;
break;
}

case 2:
{
float rec_prism_volume;
cout<<"The volume of a rectangular prism is:"<<rec_prism_volume<<endl;
break;
}

case 3:
{
float irr_prism_volume;
cout<<"The volume of a irregular prism is:"<<irr_prism_volume<<endl;
break;
}

case 4:
{
float cylinder_volume;
cout<<"The volume of a cylinder is:"<<cylinder_volume<<endl;
break;
}

case 5:
{
float pyramid_volume;
cout<<"The volume of a pyramid is:"<<pyramid_volume<<endl;
break;
}

case 6:
{
float cone_volume;
cout<<"The volume of a cone is:"<<cone_volume<<endl;
break;
}

case 7:
{
float sphere_volume;
cout<<"The volume of a sphere is:"<<sphere_volume<<endl;
break;
}

case 8:
{
float ellipsoid_volume;
cout<<"The volume of an ellipsoid is:"<<ellipsoid_volume<<endl;
break;
}

default:
cout<<"Invalid input\n";
}
system ("PAUSE");
return 0;


{
float cube_volume(float a1,float a2,float a3);
cout<<"Enter value for side 1\n";
cin>>a1;
cout<<"Enter value for side 2\n";
cin>>a2;
cout<<"Enter value for side 3\n";
cin>>a3;
volume=(a1*a2*a3);
while(a1&&a2&&a3>=0);
return volume;
}
{
float rec_prism_volume(float a,float b, float c);
cout<<"Enter value for side A\n";
cin>>a;
cout<<"Enter value for side B\n";
cin>>b;
cout<<"Enter value for side C\n";
cin>>c;
volume=a*b*c;
while(a&&b&&c>=0);
return volume;
}
{
float irr_prism_volume(float b, float h);
cout<<"Enter value for base\n";
cin>>b;
cout<<"Enter value for height\n";
cin>>h;
volume=b*h;
while(b&&h>=0);
return volume;
}
{
float cylinder_volume(float b, float h, float r);
cout<<"Enter value for base\n";
cin>>b;
cout<<"Enter value for height\n";
cin>>h;
cout<<"Enter value for radius\n";
cin>>r;
volume=PI*pow(r,2)*h;
while(b&&h&&r>=0)
return volume;
}
{
float pyramid_volume(float b, float h);
cout<<"Enter value for base\n";
cin>>b;
cout<<"Enter value for height\n";
cin>>h;
volume=1/3*b*h;
return volume;
}
{
float cone_volume(float b, float h, float r);
cout<<"Enter the value for base\n";
cin>>b;
cout<<"Enter the value for height\n";
cin>>h;
cout<<"Enter value for radius\n";
cin>>r;
volume=1/3*PI*pow(r,2)*h;
return volume;
}
{
float sphere_volume(float r);
cout<<"Enter the value for raduis\n";
cin>>r;
volume=PI*pow(r,3);
return volume;
}
{
float ellipsoid_volume(float r1, float r2, float r3);
cout<<"Enter value for r1";
cin>>r1;
cout<<"Enter value for r2";
cin>>r2;
cout<<"Enter value for r3";
cin>>r3;
volume=4/3*PI*r1*r2*r3;
return volume;
}

}
while(a&&b&&c>=0);

this will loop forever or not at all, depending. The ; is considered to be a statement, and it does not modify the condition variables, so if it loops it will loop forever. It also isn't doing what you think. It checks that a is not zero, b is not zero, and c is >= 0. You have to do each condition explicitly (see below).

You may want to do them one at a time, rather than a total reset if they make 1 mistake entering, but that is just a user aggravation /design question, it will work.

you want
something like this: (you can add your own text output prompts to it)

do
{
cin >> a;
cin >> b;
cin >> c;
//optional:
if( a < 0 || b < 0 || c<0)
cout <<"a b and c should be positive" << endl;
}
while(a < 0 && b<0 && c< 0) //you wanted them to be 0 or positive, right? So if they are not, make the user re-enter.




When doing a program like this, it's best to get one or two pieces (shapes) working correctly first to figure out how to do it. Then you can add the code for the rest. So below, I ifdef'ed out the all but the first two shapes.

Why do cube_volume and rec_prism_volume take parameters? Since they prompt for the values and use them internally, the values can be local variables.

With that in mind and using a slight change to jonnin's good advice, here is cube_volume:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
float cube_volume()
{
    float a, b, c;
    float volume;
    while (true) {
        cout << "Enter value for side 1\n";
        cin >> a;
        cout << "Enter value for side 2\n";
        cin >> b;
        cout << "Enter value for side 3\n";
        cin >> c;
        if (a >= 0 && b >= 0 && c >= 0) {
            break;
        }
        cout << "Values cannot be negative. Please try again\n";
    }
    volume = (a * b * c);
    return volume;
}

Then I go cut, paste and change the formula for rec_prism_volume and....

I see something odd. The formula for rec_prism_volume() is the same as for cube_volume. Hmm... Oh Duh! The sides of a cube are all the same, so you only need to prompt for the one side.

Putting it all together and fixing syntax errors, here is what I get:

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <iostream>
#include <cmath>

using namespace std;

float cube_volume();
float rec_prism_volume();
float irr_prism_volume;
float cylinder_volume;
float pyramid_volume;
float cone_volume;
float sphere_volume;
float ellipsoid_volume;

int
main()
{
    int option;
    float volume = 0;
    const float PI = 3.14;

    cout << "Press 1 to calculate volume of a cube\n";
    cout << "Press 2 to calculate volume of a rectangular prism\n";
    cout << "Press 3 to calculate volume of an irregular prism\n";
    cout << "Press 4 to calculate volume of a cylinder\n";
    cout << "Press 5 to calculate volume of a pyramid\n";
    cout << "Press 6 to calculate volume of a cone\n";
    cout << "press 7 to calculate volume of a sphere\n";
    cout << "Press 8 to calculate volume of an ellipsoid\n";
    cin >> option;

    switch (option) {

    case 1:
	{
	    volume = cube_volume();
	    cout << "The volume of a cube is:" << volume << endl;
	    break;
	}

    case 2:
	{
	    volume = rec_prism_volume();
	    cout << "The volume of a rectangular prism is:" << volume << endl;
	    break;
	}

#if 0
    case 3:
	{
	    float irr_prism_volume;
	    cout << "The volume of a irregular prism is:" << irr_prism_volume << endl;
	    break;
	}

    case 4:
	{
	    float cylinder_volume;
	    cout << "The volume of a cylinder is:" << cylinder_volume << endl;
	    break;
	}

    case 5:
	{
	    float pyramid_volume;
	    cout << "The volume of a pyramid is:" << pyramid_volume << endl;
	    break;
	}

    case 6:
	{
	    float cone_volume;
	    cout << "The volume of a cone is:" << cone_volume << endl;
	    break;
	}

    case 7:
	{
	    float sphere_volume;
	    cout << "The volume of a sphere is:" << sphere_volume << endl;
	    break;
	}

    case 8:
	{
	    float ellipsoid_volume;
	    cout << "The volume of an ellipsoid is:" << ellipsoid_volume << endl;
	    break;
	}
#endif
	
    default:
	cout << "Invalid input\n";
    }
    system("PAUSE");
    return 0;
}

float cube_volume()
{
    float a;
    float volume;
    while (true) {
	cout << "Enter value for a side\n";
	cin >> a;
	if (a >= 0) {
	    break;
	}
	cout << "Values cannot be negative. Please try again\n";
    }
    volume = (a * a * a);
    return volume;
}

float rec_prism_volume()
{
    float a, b, c;
    float volume;
    while (true) {
	cout << "Enter value for side 1\n";
	cin >> a;
	cout << "Enter value for side 2\n";
	cin >> b;
	cout << "Enter value for side 3\n";
	cin >> c;
	if (a >= 0 && b >= 0 && c >= 0) {
	    break;
	}
	cout << "Values cannot be negative. Please try again\n";
    }
    volume = (a * b * c);
    return volume;
}

#if 0
    {
	float irr_prism_volume(float b, float h);
	cout << "Enter value for base\n";
	cin >> b;
	cout << "Enter value for height\n";
	cin >> h;
	volume = b * h;
	while (b && h >= 0);
	return volume;
    }
    {
	float cylinder_volume(float b, float h, float r);
	cout << "Enter value for base\n";
	cin >> b;
	cout << "Enter value for height\n";
	cin >> h;
	cout << "Enter value for radius\n";
	cin >> r;
	volume = PI * pow(r, 2) * h;
	while (b && h && r >= 0)
	    return volume;
    }
    {
	float pyramid_volume(float b, float h);
	cout << "Enter value for base\n";
	cin >> b;
	cout << "Enter value for height\n";
	cin >> h;
	volume = 1 / 3 * b * h;
	return volume;
    }
    {
	float cone_volume(float b, float h, float r);
	cout << "Enter the value for base\n";
	cin >> b;
	cout << "Enter the value for height\n";
	cin >> h;
	cout << "Enter value for radius\n";
	cin >> r;
	volume = 1 / 3 * PI * pow(r, 2) * h;
	return volume;
    }
    {
	float sphere_volume(float r);
	cout << "Enter the value for raduis\n";
	cin >> r;
	volume = PI * pow(r, 3);
	return volume;
    }
    {
	float ellipsoid_volume(float r1, float r2, float r3);
	cout << "Enter value for r1";
	cin >> r1;
	cout << "Enter value for r2";
	cin >> r2;
	cout << "Enter value for r3";
	cin >> r3;
	volume = 4 / 3 * PI * r1 * r2 * r3;
	return volume;
    }

}
#endif 


Now you should move those two #ifdef 0 s down, one shape at a time fix (or rewrite) the code for the new shape, test it, and move on to the next one.
Wouldn't a cube by definition have 3 sides the same.... ?

Wouldn't a cube by definition have 3 sides the same.... ?

Yes. I addressed that later on in my post.
Topic archived. No new replies allowed.