2

This program runs all of the functions for each condition, when it should only run one function for each condition. Why? i am supposed to Write functions that compute the volume and surface area of a sphere, circular cylinder, and a circular cone: I can't figure out if it is the if statements that are messing up,or the functions themselves. An ideal output for this program would be as follows:

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!

#include <iostream>
#include <math.h>

using namespace std;

double sphere_volume(double radius);
double sphere_surface_area(double radius);
double cylinder_volume(double radius, double height);
double cylinder_surface_area(double radius, double height);
double cone_volume(double radius, double height);
double cone_surface_area(double radius, double height);

int main()
{
double entHeight;
double entRadius;
char shapeCall;
char compCall;

cout << "Select a Shape (1) sphere (2) cylinder (3) cone (q) quit: ";
cin >> shapeCall;
cout << "Select a Computation (1) volume (2) surface area: ";
cin >> compCall;

    if ( shapeCall == 1 )
    {
        if ( compCall == 1)
            cout << "Enter Radius: ";
            cin >> entRadius;
            cout << sphere_volume (entRadius) << endl;
    }
    if ( shapeCall == 1 )
    {
        if ( compCall == 2)
            cout << "Enter Radius: ";
            cin >> entRadius;
            cout << sphere_surface_area (entRadius) << endl;
    }
    if ( shapeCall == 2 )
    {
        if ( compCall == 1)
            cout << "Enter Radius: ";
            cin >> entRadius;
            cout << "Enter Height: ";
            cin >> entHeight;
            cout << cylinder_volume (entRadius, entHeight) << endl;
    }
    if (shapeCall == 2 )
    {
        if ( compCall == 2)
            cout << "Enter Radius: ";
            cin >> entRadius;
            cout << "Enter Height: ";
            cin >> entHeight;
            cout << cylinder_surface_area (entRadius, entHeight) << endl;
    }
    if (shapeCall == 3 )
    {
        if ( compCall == 1)
        cout << "Enter Radius: ";
        cin >> entRadius;
        cout << "Enter Height: ";
        cin >> entHeight;
        cout << cone_volume (entRadius, entHeight) << endl;
    }
    if (shapeCall == ) 
    {
        if ( compCall == 2)
        cout << "Enter Radius: ";
        cin >> entRadius;
        cout << "Enter Height: ";
        cin >> entHeight;
        cout << cone_surface_area (entRadius, entHeight) << endl;
    }

return 0;
}


double sphere_volume(double radius)
{
    double sphereVolume; 
    sphereVolume = 3.14 * pow(radius, 3) * 4/3;
    return sphereVolume;
}
double sphere_surface_area(double radius)
{
    double sphereSurfArea = 4 * 3.14 * pow( radius, 2 );
    return sphereSurfArea;
}
double cylinder_volume(double radius, double height)
{
    double cylinderVolume = 3.14 * pow (radius, 2) * height;
    return cylinderVolume;
}
double cylinder_surface_area(double radius, double height)
{
    double cylinderSurfArea = 2 * 3.14 * pow (radius, 2) + 2 * 3.14 * radius * height;
    return cylinderSurfArea;
}
double cone_volume(double radius, double height)
{
    double coneVolume;
    coneVolume = (1/3) * 3.14 * pow (radius, 2) * height;
    return coneVolume;
}
double cone_surface_area(double radius, double height)
{
    double coneSurfArea = 3.14 * pow (radius, 2) + 3.14 * sqrt(pow (radius, 2) + pow (height, 2));
    return coneSurfArea;
}
3
  • It would be helpful if you provided the real code and formatted it consistently. Commented Feb 23, 2013 at 9:41
  • i've also tried this format,and it also doesn't work Commented Feb 23, 2013 at 10:00
  • What is "this format"? Formatting can be automated, but if that is not an option, you need to do it manually. Commented Feb 25, 2013 at 6:41

1 Answer 1

1

An if statement expects to be in the following format

if (expression)
    statement;

In the event that expression evaluates to a non zero number, or true, statement is executed. In every other event, it is not.

The reason why it appears as if every condition evaluates to true, is because you change the way you format your if statements throughout your code. You seem to use the following format for your conditionals very often.

if ( compCall == 1)
        cout << "Enter Radius: ";
        cin >> entRadius;
        cout << sphere_volume (entRadius) << endl;

Consider that the if statement will be associated only with the directly following statement. Thus, that code is equivalent to

if ( compCall == 1)
{
    cout << "Enter Radius: ";
}
cin >> entRadius;
cout << sphere_volume (entRadius) << endl;

The problem you're having arises from not properly enclosing your conditionals in curly braces. It may still be considered valid code, but it can produce very unexpected results if used incorrectly. The following code would, in this situation, produce the results you expect, as the statements associated with the condition are properly enclosed in curly braces.

if ( compCall == 1)
{
   cout << "Enter Radius: ";
   cin >> entRadius;
   cout << sphere_volume (entRadius) << endl;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I agree with your answer, just condition is false when expression evaluates to false or 0 or null. In every other cases it evaluates to true for C languages. So even 5 is evaluated as true or any negative number. But I guess you ment that this !0 == 1 evaluates to true and !0 == 2 evaluates to false. I'll edit this in your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.