Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

pls help me to solve this geometry calculator pls use normal method pls don't use switch structure

0 votes
asked May 11, 2018 by Mahmud Muhammad (230 points)

Geometry Calculator

Write a program that displays the following menu:

Geometry Calculator

1. Calculate the Area of a Circle

2. Calculate the Area of a Rectangle

3. Calculate the Area of a Triangle

4. Quit

Enter your choice (1-4):

1 Answer

0 votes
answered May 17, 2018 by muha
why no switch structure ?

switch is the best thing you can use for a console menu

here is my solution with switch:

#include <iostream>

using namespace std;

int main()
{
    int Menu;
    double pi = 3.14159265;
    double A, r, h, g, a, b;
    
    do{
        
        
        cout << "\nChoose an option: ";
        cin >> Menu;
        
        switch(Menu){
            
            case 1:
                cout << "\nCalculate the Area of a circle";
                cout << "\nType in the value for r: ";
                cin >> r;
                A = r * r * pi;
                cout << "The Area of the circle is: " << A;
                break;
                
            case 2:
                cout << "\nCalculate the Area of a rectangle: ";
                cout << "\nType in the value of a: ";
                cin >> a;
                cout << "Type in the value of b: ";
                cin >> b;
                A = a * b;
                cout << "The Area of the rectangle is: " << A;
                break;
                
            case 3:
                cout << "\nCalculate the Area of a Triangle: ";
                cout << "\nType in the value for the height h: ";
                cin >> h;
                cout << "Type in the value for g: ";
                cin >> g;
                A = 0.5 * g * h;
                cout << "The Area of the triangle is: " << A;
                break;
                
            case 4:
                cout << "\nThe End!";
                break;
                
            default:
                cout << "Error! Not a valid option!";
                break;
        }
        
    }while(Menu != 4);

    return 0;
}

if you dont want to use switch just do it with if statements for example:

if(Menu == 1){

    cout << "Area of a circle";

}
commented May 17, 2018 by Mahmud Muhammad (230 points)
pls help me and do it
commented May 18, 2018 by muha
hahhaaha you should learn to do things like this. this is really basic stuff that you can easily learn in a week at most...
well here is the solution:

#include <iostream>

using namespace std;

int main()
{
    int Menu;
    double pi = 3.14159265;
    double A, r, h, g, a, b;
    // A = Area
    // r = radius
    // h = height
    // g = baseline of the Triangle
    // a = 1st side of the Rectangle
    // b = 2nd side of the Rectangle
    
    do{
        
        
        cout << "\nChoose an option: ";
        cout << "\n***********************";
        cout << "\n**** 1 = Circle    ****";
        cout << "\n**** 2 = Rectangle ****";
        cout << "\n**** 3 = Triangle  ****";
        cout << "\n**** 4 = End       ****";
        cout << "\n***********************" << endl;
        cin >> Menu;
        
        if(Menu == 1){
            cout << "\nCalculate the Area of a circle";
            cout << "\nType in the value for r: ";
            cin >> r;
            A = r * r * pi;
            cout << "The Area of the circle is: " << A;
        }
                
        if(Menu == 2){
            cout << "\nCalculate the Area of a rectangle: ";
            cout << "\nType in the value of a: ";
            cin >> a;
            cout << "Type in the value of b: ";
            cin >> b;
            A = a * b;
            cout << "The Area of the rectangle is: " << A;
        }
                
        if(Menu == 3){
            cout << "\nCalculate the Area of a Triangle: ";
            cout << "\nType in the value for the height h: ";
            cin >> h;
            cout << "Type in the value for g: ";
            cin >> g;
            A = 0.5 * g * h;
            cout << "The Area of the triangle is: " << A;
        }
                
        if(Menu == 4){
            cout << "\nThe End!";
        }    
        
        if(Menu > 4){
            cout << "Error! Not a valid option!";
        }
        
    }while(Menu != 4);

    return 0;
}
commented May 20, 2018 by Mahmud Muhammad (230 points)
tanx alot mr moha pls can you explain it step by step
commented May 22, 2018 by muha
#include <iostream>
//-> including the standard libraries

using namespace std;
//-> this is useful for beginners you dont need to write std:: e.g.: std::cout<<" ";



int main()  //-> this is where your main function starts all your code goes inside of it you will learn more about functions as you progress
{
    int Menu;                   //Variable for the Menu
    double pi = 3.14159265;     //Variable for pi
    double A, r, h, g, a, b;
    // Variable for:
    // A = Area
    // r = radius
    // h = height
    // g = baseline of the Triangle
    // a = 1st side of the Rectangle
    // b = 2nd side of the Rectangle
    
    do{             //-> this starts a loop as long as the user doesnt input 4 for the variable Menu it will always stay in this loop see the while statement at the end
        
        //cout is printing the following lines to the console window
        cout << "\nChoose an option: ";
        cout << "";
        cout << " 1 = Circle";
        cout << " 2 = Rectangle ";
        cout << " 3 = Triangle  ";
        cout << " 4 = End       ";
        cout << "" << endl;
        cin >> Menu;        //cin reads an input from the console to a variable (here the Variable is Menu)
        
        if(Menu == 1){
            //if Variable Menu has the Value 1 (if the user inputs 1), it will run the following code:
            cout << "\nCalculate the Area of a circle";
            cout << "\nType in the value for r: ";
            cin >> r;
            A = r * r * pi;
            cout << "The Area of the circle is: " << A;
            //Calculates the Area of a circle and prints the result onto the screen
        }
                
        if(Menu == 2){
            //if Variable Menu has the Value 2 (if the user inputs 2), it will run the following code:
            cout << "\nCalculate the Area of a rectangle: ";
            cout << "\nType in the value of a: ";
            cin >> a;
            cout << "Type in the value of b: ";
            cin >> b;
            A = a * b;
            cout << "The Area of the rectangle is: " << A;
            //Calculates the Area of a rectangle and prints the result onto the screen
        }
                
        if(Menu == 3){
            //if Variable Menu has the Value 3 (if the user inputs 3), it will run the following code:
            cout << "\nCalculate the Area of a Triangle: ";
            cout << "\nType in the value for the height h: ";
            cin >> h;
            cout << "Type in the value for g: ";
            cin >> g;
            A = 0.5 * g * h;
            cout << "The Area of the triangle is: " << A;
            //Calculates the Area of a triangle and prints the result onto the screen
        }
                
        if(Menu == 4){
            //if Variable Menu has the Value 4 (if the user inputs 4), it will run the following code:
            cout << "\nThe End!";
            //Prints The End to the Screen
        }    
        
        if(Menu > 4){
            //if Variable Menu is bigger than 4 (if the user inputs something bigger than 4), it will run the following code:
            cout << "Error! Not a valid option!";
            //this part is basic error handling
        }
        
    }while(Menu != 4);
    //the code in the do{}while(); will run as long as the user doenst input the value 4 for the Variable Menu

    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...