This program is supposed to run through the menus to eventually display the area or volume of a shape depending on the user's menu choices and input. However, it won't run the final menu and I'm not sure why. There aren't any problems when it goes through the debugger, it just doesn't run the whole program. I tried to change my last level switches to if statements but it didn't change anything. Any ideas?

#include <iostream>

using std::cout;
using std::cin;

int main()
{
    int menuitem = 0,
        areamenuitem = 0,
        volumemenuitem = 0,
        base = 0,
        height = 0,
        radius = 0,
        rectangle = 0,
        circle = 0,
        triangle = 0,
        cylinder = 0,
        sphere = 0;
    const float PI = 3.14;

    rectangle = 1/2 * base * height;
    circle = (radius * radius) * PI;
    triangle = 1/2 * base * height;
    cylinder = PI * (radius * radius) * height;
    sphere = 4/3 * PI * (radius * radius * radius);

    cout <<"-- Main Menu --"<<'\n';
    cout <<"1. Calculate Area"<<'\n';
    cout <<"2. Calculate Volume"<<'\n';
    cout <<"Enter Menu Number Choice: ";
    cin >> menuitem;

    switch ( menuitem )
    {
    case 1:
        cout <<"-- Area Menu --"<<'\n';
        cout <<"1. Retangle"<<'\n';
        cout <<"2. Circle"<<'\n';
        cout <<"3. Right Triangle"<<'\n';
        cout <<"Enter Menu Number Choice: ";
        cin >> areamenuitem;
        break;

        switch ( areamenuitem )
        {
        case 1:
            cout <<"--Rectangle--" <<'\n';
            cout <<"Enter base: " <<'\n';
            cin >> base;
            cout <<"Enter height: "<<'\n';
            cin >> height;
            cout << "Rectangle Area: " << rectangle <<'\n';
            break;
        case 2:
            cout <<"--Circle--"<<'\n';
            cout <<"Enter radius: " <<'\n';
            cin >> radius;
            cout <<"Circle Area: " << circle <<'\n';
            break;
        case 3:
            cout <<"--Right Triangle--" <<'\n';
            cout <<"Enter base: " <<'\n';
            cin >> base;
            cout <<"Enter height: " <<'\n';
            cin >> height;
            cout <<"Right Triangle Area: " << triangle <<'\n';
            break;
        default:
            cout <<"Error, invalid entry." <<'\n';
        }

    case 2:
        cout <<"-- Volume Menu --"<<'\n';
        cout <<"1. Cylinder"<<'\n';
        cout <<"2. Sphere"<<'\n';
        cout <<"Enter Menu Number Choice: ";
        cin >> volumemenuitem;
        break;

        switch ( volumemenuitem )
        {
        case 1:
            cout <<"--Cylinder Formula--"<<'\n';
            cout <<"Enter radius: " <<'\n';
            cin >> radius;
            cout <<"Enter height: " <<'\n';
            cin >> height;
            cout <<"Cylinder Volume: " << cylinder << '\n';
            break;
        case 2:
            cout <<"Sphere Formula: 4/3 * pi * radius^3"<<'\n';
            cout <<"Enter radius: " <<'\n';
            cin >> radius;
            cout <<"Sphere Volume: "<< sphere <<'\n';
            break;
        default:
            cout <<"Error, please choose again." <<'\n';
        }
    }

    return 0;
}

Recommended Answers

All 3 Replies

You have break statements in the wrong place.

Move the break statement on line 42 down to line 71.

Move the break statement on line 78 down to just below line 98.

The break; statements in the outer switch statements should be moved to after the inner switch statements. The break instruction marks the end of the code within a switch-case. That's why your inner switches don't ever execute. So, Line 42 should be moved to Line 71, and Line 78 should be moved to between Line 98 and 99.

Any case like this:

case 1:
        cout <<"-- Area Menu --"<<'\n';
        cout <<"1. Retangle"<<'\n';
        cout <<"2. Circle"<<'\n';
        cout <<"3. Right Triangle"<<'\n';
        cout <<"Enter Menu Number Choice: ";
        cin >> areamenuitem;
        break;

need write in one line like:

case 1: cout <<"-- Area Menu --\n1. Retangle\n2. Circle\n3. Right Triangle\nEnter Menu Number Choice: "; cin >> areamenuitem; break;

or write in brace:

case 1: {
        cout <<"-- Area Menu --"<<'\n';
        cout <<"1. Retangle"<<'\n';
        cout <<"2. Circle"<<'\n';
        cout <<"3. Right Triangle"<<'\n';
        cout <<"Enter Menu Number Choice: ";
        cin >> areamenuitem;
        break;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.