Home > Software engineering >  Why is my calculator not responding to my if statements?
Why is my calculator not responding to my if statements?

Time:02-13

Newbie programmer here. I'm finished with this project I was working on, and all of my if and else statements function properly, but only when they are placed at the very top of the code I'm writing.

For example, I started writing this code from if(number == 1) and worked my way down, but I eventually had to cut and paste them at the top, because when I entered a value that wasn't 1 (the first if statement, and thus the top statement that functions), the calculator failed to print anything at all when inputting a value for anything else except the top one. Because 4 is at the top, it works fine, but 1-3 don't work, and if I was to put 1 at the top then 2-4 would not print anything.

Here is a screeshot of the if statements on the bottom that do not print, and also have no debugging signs:

Image

I'm sure it's a small issue that I managed to turn big.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int number;
    float area1, area2, area3, area4;
    int R1;
    int trib;
    int trih;
    int top;
    int bott;
    int traph;
    int majxis;
    int minxis;
    const float PI= 3.14159;
    cout<< "I'm starting my program ";
    cout<<"\n1.Circle";
    cout<<"\n2.triangle";
    cout<<"\n3.trapezoid";
    cout<<"\n4.ellipse ";
    cout<<"\nEnter the corresponding number of your choice(1-4):";
    cin>> number;
    if(number <= 0)
    {
        cout<<"Integer is invalid, try again ";
        cin>> number;
    }
    if(number == 4)
    {
        cout<<"\nWhat is the length of the semi-major axis of the ellipse? ";
    }
     cin>> majxis;
     if(majxis <=0)
     {
         cout<<"Invalid value for the semi-major axis, try again: ";
     }
     else
     {
         cout<<"What is the length for the semi-minor axis of the ellipse? ";
     }
     cin>> minxis;
     area4= PI*minxis*majxis;
     if(minxis <=0)
     {
         cout<<"Invalid value for the semi-minor axis, try again: ";
     }
     else
     {
         cout<<"The area of an ellipse with a semi-major axis of " << majxis<< "and a semi-minor axis of " << minxis << "is " << area4;
     }
    if(number == 3)
    {
        cout<<"\nWhat is the length of the top of the trapezoid? ";
    }
     cin>> top;
    
     if(top <=0 )
     {
         cout<<"\n Invalid value for top length of trapezoid, try again";
     }
     else
     {
         cout<<"\nWhat is the length of the bottom of the trapezoid? ";
     }
     cin>> bott;
     if (bott <= 0)
     {
         cout<<"Invalid value for bottom length of trapezoid, try again: ";
     }
     else
     {
         cout<<"What is the height of the trapezoid? ";
     }
     cin>>traph;
     area3 = (top bott)*0.5 traph;
     if (traph <= 0)
     {
         cout<<"Invalid value for height of trapezoid";
     }
     else
     {
         cout<<"The area of a trapezoid with a bottom of " << bott << "and a top of " << top << "is " << area3;
     }
    if(number == 2)
    {
        cout<<"\nWhat is the length of the base of the triangle?";
    }
    cin>> trib;
    if(trib<=0)
    {
        cout<<"\nInvalid Base of triangle, try again:";
    }
    else
    {
        cout<<"\nWhat is the height of the triangle? ";
    }
    cin>> trih;
    area2 = (0.5*trib)*trih;
    if(trih<=0)
    {
        cout<<"\nInvalid height of triangle, try again:";
    }
    else
    {
        cout<<"\nThe area of a trangle with a base of " << trib << "and a height of "<< trih <<" is " << area2;
    }
    if(number == 1)
    {
        cout<<"\nWhat is the radius of the circle?";
    }
    cin>> R1;
    area1 = PI*(R1 *R1);
    if( R1 <=0)
    {cout<<"\nRadius must be greater than 0, try again";
        cin>> R1;
    }
    else
    {
        cout<< "\nThe area of a circle with a radius of " << R1 << " is" << area1;
    }
}

CodePudding user response:

It indeed is a small issue. Here is the correct program:

#include <iostream>
#include <iomanip>

int main()
{
    int number;
    float area1, area2, area3, area4;
    int R1;
    int trib;
    int trih;
    int top;
    int bott;
    int traph;
    int majxis;
    int minxis;
    const float PI = 3.14159;

    std::cout << "I'm starting my program ";
    std::cout << "\n1.Circle";
    std::cout << "\n2.triangle";
    std::cout << "\n3.trapezoid";
    std::cout << "\n4.ellipse ";
    std::cout << "\nEnter the corresponding number of your choice(1-4):";
    std::cin >> number;

    if (number <= 0)
    {
        std::cout << "Integer is invalid, try again ";
        std::cin >> number;
    }
    else if (number == 1)
    {
        std::cout << "\nWhat is the radius of the circle?";
        
        std::cin >> R1;
        area1 = PI * (R1 * R1);
        if (R1 <= 0)
        {
            std::cout << "\nRadius must be greater than 0, try again";
            std::cin >> R1;
        }
        else
        {
            std::cout << "\nThe area of a circle with a radius of " << R1 << " is" << area1;
        }
    }
    else if (number == 2)
    {
        std::cout << "\nWhat is the length of the base of the triangle?";

        std::cin >> trib;
        if (trib <= 0)
        {
            std::cout << "\nInvalid Base of triangle, try again:";
        }
        else
        {
            std::cout << "\nWhat is the height of the triangle? ";
        }
        std::cin >> trih;
        area2 = (0.5 * trib) * trih;
        if (trih <= 0)
        {
            std::cout << "\nInvalid height of triangle, try again:";
        }
        else
        {
            std::cout << "\nThe area of a trangle with a base of " << trib << "and a height of " << trih << " is " << area2;
        }
    }
    else if (number == 3)
    {
        std::cout << "\nWhat is the length of the top of the trapezoid? ";

        std::cin >> top;

        if (top <= 0)
        {
            std::cout << "\n Invalid value for top length of trapezoid, try again";
        }
        else
        {
            std::cout << "\nWhat is the length of the bottom of the trapezoid? ";
        }
        std::cin >> bott;
        if (bott <= 0)
        {
            std::cout << "Invalid value for bottom length of trapezoid, try again: ";
        }
        else
        {
            std::cout << "What is the height of the trapezoid? ";
        }
        std::cin >> traph;
        area3 = (top   bott) * 0.5   traph;
        if (traph <= 0)
        {
            std::cout << "Invalid value for height of trapezoid";
        }
        else
        {
            std::cout << "The area of a trapezoid with a bottom of " << bott << "and a top of " << top << "is " << area3;
        }
    }
    else if (number == 4)
    {
        std::cout << "\nWhat is the length of the semi-major axis of the ellipse? ";

        std::cin >> majxis;
        if (majxis <= 0)
        {
            std::cout << "Invalid value for the semi-major axis, try again: ";
        }
        else
        {
            std::cout << "What is the length for the semi-minor axis of the ellipse? ";
        }
        std::cin >> minxis;
        area4 = PI * minxis * majxis;
        if (minxis <= 0)
        {
            std::cout << "Invalid value for the semi-minor axis, try again: ";
        }
        else
        {
            std::cout << "The area of an ellipse with a semi-major axis of " << majxis << "and a semi-minor axis of " << minxis << "is " << area4;
        }
    }
}

Explanation: Let's take an example of the area of eclipse (number == 4). In your code, you have made the following if condition:

if(number == 4)
{
    cout<<"\nWhat is the length of the semi-major axis of the ellipse? ";
}

This means if number == 4 then print - "\nWhat is the length of the semi-major axis of the ellipse? " but all of the code after it is outside the if condition's {} brackets, and thus is not considered as a part of the if statement. It will be called anyways even if number != 4. Now, in my code, I've done this:

else if (number == 4)
{
    std::cout << "\nWhat is the length of the semi-major axis of the ellipse? ";

    std::cin >> majxis;
    if (majxis <= 0)
    {
        std::cout << "Invalid value for the semi-major axis, try again: ";
    }
    else
    {
        std::cout << "What is the length for the semi-minor axis of the ellipse? ";
    }
    std::cin >> minxis;
    area4 = PI * minxis * majxis;
    if (minxis <= 0)
    {
        std::cout << "Invalid value for the semi-minor axis, try again: ";
    }
    else
    {
        std::cout << "The area of an ellipse with a semi-major axis of " << majxis << "and a semi-minor axis of " << minxis << "is " << area4;
    }
}

Here, all of the code related to the area of the eclipse is all part of the if (number == 4) statement because it is present between the brackets {} of the if (number == 4) statement.

Apply this to all 4 cases and you get the correct program.

Also, it would be better to refactor your code as such:

#include <iostream>
#include <iomanip>

const float PI = 3.14159;

void areaof_circle()
{
    float area;
    int R1;

    std::cout << "\nWhat is the radius of the circle?";

    std::cin >> R1;
    area = PI * (R1 * R1);
    if (R1 <= 0)
    {
        std::cout << "\nRadius must be greater than 0, try again";
        std::cin >> R1;
    }
    else
    {
        std::cout << "\nThe area of a circle with a radius of " << R1 << " is" << area;
    }
}

void areaof_triangle()
{
    float area;
    int trib, trih;
    std::cout << "\nWhat is the length of the base of the triangle?";

    std::cin >> trib;
    if (trib <= 0)
    {
        std::cout << "\nInvalid Base of triangle, try again:";
    }
    else
    {
        std::cout << "\nWhat is the height of the triangle? ";
    }
    std::cin >> trih;
    area = (0.5 * trib) * trih;
    if (trih <= 0)
    {
        std::cout << "\nInvalid height of triangle, try again:";
    }
    else
    {
        std::cout << "\nThe area of a trangle with a base of " << trib << "and a height of " << trih << " is " << area;
    }
}

void areaof_trapezoid()
{
    float area;
    int top, bott, traph;

    std::cout << "\nWhat is the length of the top of the trapezoid? ";

    std::cin >> top;

    if (top <= 0)
    {
        std::cout << "\n Invalid value for top length of trapezoid, try again";
    }
    else
    {
        std::cout << "\nWhat is the length of the bottom of the trapezoid? ";
    }
    std::cin >> bott;
    if (bott <= 0)
    {
        std::cout << "Invalid value for bottom length of trapezoid, try again: ";
    }
    else
    {
        std::cout << "What is the height of the trapezoid? ";
    }
    std::cin >> traph;
    area = (top   bott) * 0.5   traph;
    if (traph <= 0)
    {
        std::cout << "Invalid value for height of trapezoid";
    }
    else
    {
        std::cout << "The area of a trapezoid with a bottom of " << bott << "and a top of " << top << "is " << area;
    }
}

void areaof_ellipse()
{
    float area;
    int majxis, minxis;

    std::cout << "\nWhat is the length of the semi-major axis of the ellipse? ";

    std::cin >> majxis;
    if (majxis <= 0)
    {
        std::cout << "Invalid value for the semi-major axis, try again: ";
    }
    else
    {
        std::cout << "What is the length for the semi-minor axis of the ellipse? ";
    }
    std::cin >> minxis;
    area = PI * minxis * majxis;
    if (minxis <= 0)
    {
        std::cout << "Invalid value for the semi-minor axis, try again: ";
    }
    else
    {
        std::cout << "The area of an ellipse with a semi-major axis of " << majxis << "and a semi-minor axis of " << minxis << "is " << area;
    }
}

int main()
{
    int number;
    const float PI = 3.14159;

    std::cout << "I'm starting my program ";
    std::cout << "\n1.Circle";
    std::cout << "\n2.triangle";
    std::cout << "\n3.trapezoid";
    std::cout << "\n4.ellipse ";
    std::cout << "\nEnter the corresponding number of your choice(1-4):";
    std::cin >> number;

    if (number <= 0)
    {
        std::cout << "Integer is invalid, try again ";
        std::cin >> number;
    }
    else if (number == 1)
    {
        areaof_circle();
    }
    else if (number == 2)
    {
        areaof_triangle();
    }
    else if (number == 3)
    {
        areaof_trapezoid();
    }
    else if (number == 4)
    {
        areaof_ellipse();
    }
}

Also, don't use the following in your programs:

using namespace std;

...as it's considered as bad practice. Use std:: every time instead.

CodePudding user response:

There is a problem with your if statement.. as the well defined answer is already given, I am simplifying your code's readability and suggesting more better way to do this..

NOTE: Considering you're new bie.

Create Functions to handle different quadrilaterals

#include <iostream>
#include <iomanip>
using namespace std;

    float area1, area2, area3, area4;
    int R1;
    int trib;
    int trih;
    int top;
    int bott;
    int traph;
    int majxis;
    int minxis;
    const float PI= 3.14159;

void handle_circle();
void handle_ellipse();
void handle_triangle();
void handle_trapizoid();

int main()
{
    int number;
    cout<< "I'm starting my program ";
    cout<<"\n1.Circle";
    cout<<"\n2.triangle";
    cout<<"\n3.trapezoid";
    cout<<"\n4.ellipse ";
    cout<<"\nEnter the corresponding number of your choice(1-4):";
    cin>> number;
    if(number <= 0)
    {
        cout<<"Integer is invalid, try again ";
        cin>> number;
    }
    if(number == 4)
    {
        handle_ellipse();
    }
    if(number == 3)
    {
        handle_trapizoid();
    }
    if(number == 2)
    {
        handle_triangle();
    }
    
    if(number == 1)
    {
        handle_circle();
    }
}

void handle_trapizoid()
{
         cout<<"\nWhat is the length of the top of the trapezoid? ";
     cin>> top;
    
    
     if(top <=0 )
     {
         cout<<"\n Invalid value for top length of trapezoid, try again";
     }
     else
     {
         cout<<"\nWhat is the length of the bottom of the trapezoid? ";
     cin>> bott;
     }
    
    if (bott <= 0)
     {
         cout<<"Invalid value for bottom length of trapezoid, try again: ";
     }
     else
     {
         cout<<"What is the height of the trapezoid? ";
     cin>>traph;
     area3 = (top bott)*0.5 traph;
     }
     if (traph <= 0)
     {
         cout<<"Invalid value for height of trapezoid";
     }
     else
     {
         cout<<"The area of a trapezoid with a bottom of " << bott << "and a top of " << top << "is " << area3;
     }

}

void handle_ellipse()
{
     cout<<"\nWhat is the length of the semi-major axis of the ellipse? ";
     cin>> majxis;
    
     if(majxis <=0)
     {
         cout<<"Invalid value for the semi-major axis, try again: ";
     }
     else
     {
         cout<<"What is the length for the semi-minor axis of the ellipse? ";
     cin>> minxis;
     area4= PI*minxis*majxis;
     }
     if(minxis <=0)
     {
         cout<<"Invalid value for the semi-minor axis, try again: ";
     }
     else
     {
         cout<<"The area of an ellipse with a semi-major axis of " << majxis<< "and a semi-minor axis of " << minxis << "is " << area4;
     }

}

void handle_triangle()
{
    cout<<"\nWhat is the length of the base of the triangle?";
    cin>> trib;
    
    if(trib<=0)
    {
        cout<<"\nInvalid Base of triangle, try again:";
    }
    else
    {
        cout<<"\nWhat is the height of the triangle? ";
    cin>> trih;
    area2 = (0.5*trib)*trih;
    }
    if(trih<=0)
    {
        cout<<"\nInvalid height of triangle, try again:";
    }
    else
    {
        cout<<"\nThe area of a trangle with a base of " << trib << "and a height of "<< trih <<" is " << area2;
    }

}

void handle_circle()
{
    cout<<"\nWhat is the radius of the circle?";
    cin>> R1;
    area1 = PI*(R1 *R1);
    
    if( R1 <=0)
    {cout<<"\nRadius must be greater than 0, try again";
        cin>> R1;
    }
    else
    {
        cout<< "\nThe area of a circle with a radius of " << R1 << " is" << area1;
    }

}

Also you can use switch statement as it is more faster than if statements

#include <iostream>
#include <iomanip>
using namespace std;

    float area1, area2, area3, area4;
    int R1;
    int trib;
    int trih;
    int top;
    int bott;
    int traph;
    int majxis;
    int minxis;
    const float PI= 3.14159;

void handle_circle();
void handle_ellipse();
void handle_triangle();
void handle_trapizoid();

int main()
{
    int number;
    cout<< "I'm starting my program ";
    cout<<"\n1.Circle";
    cout<<"\n2.triangle";
    cout<<"\n3.trapezoid";
    cout<<"\n4.ellipse ";
    cout<<"\nEnter the corresponding number of your choice(1-4):";
    cin>> number;
    
    switch(number)
    {
        case 1:
        handle_circle();
        break;
        
        case 2:
        handle_triangle();
        break;
        
        case 3:
        handle_ellipse();
        break;
        
        case 4:
        handle_trapizoid();
        break;
        
        default:
        {
        cout<<"Integer is invalid, try again ";
        }
    }
}

void handle_trapizoid()
{
         cout<<"\nWhat is the length of the top of the trapezoid? ";
     cin>> top;
    
    
     if(top <=0 )
     {
         cout<<"\n Invalid value for top length of trapezoid, try again";
     }
     else
     {
         cout<<"\nWhat is the length of the bottom of the trapezoid? ";
     cin>> bott;
     }
    
    if (bott <= 0)
     {
         cout<<"Invalid value for bottom length of trapezoid, try again: ";
     }
     else
     {
         cout<<"What is the height of the trapezoid? ";
     cin>>traph;
     area3 = (top bott)*0.5 traph;
     }
     if (traph <= 0)
     {
         cout<<"Invalid value for height of trapezoid";
     }
     else
     {
         cout<<"The area of a trapezoid with a bottom of " << bott << "and a top of " << top << "is " << area3;
     }

}

void handle_ellipse()
{
     cout<<"\nWhat is the length of the semi-major axis of the ellipse? ";
     cin>> majxis;
    
     if(majxis <=0)
     {
         cout<<"Invalid value for the semi-major axis, try again: ";
     }
     else
     {
         cout<<"What is the length for the semi-minor axis of the ellipse? ";
     cin>> minxis;
     area4= PI*minxis*majxis;
     }
     if(minxis <=0)
     {
         cout<<"Invalid value for the semi-minor axis, try again: ";
     }
     else
     {
         cout<<"The area of an ellipse with a semi-major axis of " << majxis<< "and a semi-minor axis of " << minxis << "is " << area4;
     }

}

void handle_triangle()
{
    cout<<"\nWhat is the length of the base of the triangle?";
    cin>> trib;
    
    if(trib<=0)
    {
        cout<<"\nInvalid Base of triangle, try again:";
    }
    else
    {
        cout<<"\nWhat is the height of the triangle? ";
    cin>> trih;
    area2 = (0.5*trib)*trih;
    }
    if(trih<=0)
    {
        cout<<"\nInvalid height of triangle, try again:";
    }
    else
    {
        cout<<"\nThe area of a trangle with a base of " << trib << "and a height of "<< trih <<" is " << area2;
    }

}

void handle_circle()
{
    cout<<"\nWhat is the radius of the circle?";
    cin>> R1;
    area1 = PI*(R1 *R1);
    
    if( R1 <=0)
    {cout<<"\nRadius must be greater than 0, try again";
        cin>> R1;
    }
    else
    {
        cout<< "\nThe area of a circle with a radius of " << R1 << " is" << area1;
    }

}
  • Related