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