3
\$\begingroup\$

How can I refactor these switch cases?

void selectOption(Shape** shapes, int size, int selection) {
    int obj1;
    int obj2;
    bool init2 = false;
    bool init = false;
    int option = 0;

    do {
        cout << "\n  (1) Rectangle #1"
            << "\n  (2) Rectangle #2"
            << "\n  (3) Circle #1"
            << "\n  (4) Circle #2"
            << "\n  (5) Box #1"
            << "\n  (6) Box #2"
            << "\n  (7) Cylinder #1"
            << "\n  (8) Cylinder #2"

            << "\n\n  Please select two objects -\n"
            << "\n  Object #1 (between 1 through 4): ";
        cin >> obj1;
        switch (obj1) {

        case 1:
            do {
                cout << "  Object #2 (between 1 through 4): ";
                cin >> obj2;

                switch (obj2) {
                case 1:
                    if (selection == 1) {
                        compareArea(&shapes[0], &shapes[0]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[0], &shapes[0]);
                    }
                    else {
                        cout << "\n  " << shapes[0]->getID() << " --";
                        shapes[0]->print();
                        cout << "\n  " << shapes[0]->getID() << " --";
                        shapes[0]->print();
                        cout << "\n";
                    }
                    init2 = true;
                    break;
                case 2:
                    if (selection == 1) {
                        compareArea(&shapes[0], &shapes[1]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[0], &shapes[1]);
                    }
                    else {
                        cout << "\n  " << shapes[0]->getID() << " --";
                        shapes[0]->print();
                        cout << "\n\n  " << shapes[1]->getID() << " --";
                        shapes[1]->print();
                    }
                    init2 = true;
                    break;
                case 3:
                    if (selection == 1) {
                        compareArea(&shapes[0], &shapes[2]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[0], &shapes[2]);
                    }
                    else {
                        cout << shapes[0]->getID() << " --";
                        shapes[0]->print();
                        cout << shapes[2]->getID() << " --";
                        shapes[2]->print();
                    }
                    init2 = true;
                    break;
                case 4:
                    if (selection == 1) {
                        compareArea(&shapes[0], &shapes[3]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[0], &shapes[3]);
                    }
                    else {
                        cout << shapes[0]->getID() << " --";
                        shapes[0]->print();
                        cout << shapes[3]->getID() << " --";
                        shapes[3]->print();
                    }
                    init2 = true;
                    break;
                case 5:
                    if (selection == 1) {
                        compareArea(&shapes[0], &shapes[4]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[0], &shapes[4]);
                    }
                    else {
                        cout << shapes[0]->getID() << " --";
                        shapes[0]->print();
                        cout << shapes[4]->getID() << " --";
                        shapes[4]->print();
                    }
                    init2 = true;
                    break;
                case 6:
                    if (selection == 1) {
                        compareArea(&shapes[0], &shapes[5]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[0], &shapes[5]);
                    }
                    else {
                        cout << shapes[0]->getID() << " --";
                        shapes[0]->print();
                        cout << shapes[5]->getID() << " --";
                        shapes[5]->print();
                    }
                    init2 = true;
                    break;
                case 7:
                    if (selection == 1) {
                        compareArea(&shapes[0], &shapes[6]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[0], &shapes[6]);
                    }
                    else {
                        cout << shapes[0]->getID() << " --";
                        shapes[0]->print();
                        cout << shapes[6]->getID() << " --";
                        shapes[6]->print();
                    }
                    init2 = true;
                    break;
                case 8:
                    if (selection == 1) {
                        compareArea(&shapes[0], &shapes[7]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[0], &shapes[7]);
                    }
                    else {
                        cout << shapes[0]->getID() << " --";
                        shapes[0]->print();
                        cout << shapes[6]->getID() << " --";
                        shapes[7]->print();
                    }
                    init2 = true;
                    break;
                default:
                    cout << "\n  The selected object does not exist! Try again.\n";
                }
            } while (init2 == false);
            init = true;
            break;

        case 2:
            do {
                cout << "  Object #2 (between 1 through 4): ";
                cin >> obj2;

                switch (obj2) {
                case 1:
                    if (selection == 1) {
                        compareArea(&shapes[1], &shapes[0]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[1], &shapes[0]);
                    }
                    else {
                        cout << shapes[1]->getID() << " --";
                        shapes[1]->print();
                        cout << shapes[0]->getID() << " --";
                        shapes[0]->print();
                    }
                    init2 = true;
                    break;
                case 2:
                    if (selection == 1) {
                        compareArea(&shapes[1], &shapes[1]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[1], &shapes[1]);
                    }
                    else {
                        cout << shapes[1]->getID() << " --";
                        shapes[1]->print();
                        cout << shapes[1]->getID() << " --";
                        shapes[1]->print();
                    }
                    init2 = true;
                    break;
                case 3:
                    if (selection == 1) {
                        compareArea(&shapes[1], &shapes[2]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[1], &shapes[2]);
                    }
                    else {
                        cout << shapes[1]->getID() << " --";
                        shapes[1]->print();
                        cout << shapes[2]->getID() << " --";
                        shapes[2]->print();
                    }
                    init2 = true;
                    break;
                case 4:
                    if (selection == 1) {
                        compareArea(&shapes[1], &shapes[3]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[1], &shapes[3]);
                    }
                    else {
                        cout << shapes[1]->getID() << " --";
                        shapes[1]->print();
                        cout << shapes[3]->getID() << " --";
                        shapes[3]->print();
                    }
                    init2 = true;
                    break;
                case 5:
                    if (selection == 1) {
                        compareArea(&shapes[1], &shapes[4]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[1], &shapes[4]);
                    }
                    else {
                        cout << shapes[1]->getID() << " --";
                        shapes[1]->print();
                        cout << shapes[4]->getID() << " --";
                        shapes[4]->print();
                    }
                    init2 = true;
                    break;
                case 6:
                    if (selection == 1) {
                        compareArea(&shapes[1], &shapes[5]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[1], &shapes[5]);
                    }
                    else {
                        cout << shapes[1]->getID() << " --";
                        shapes[1]->print();
                        cout << shapes[5]->getID() << " --";
                        shapes[5]->print();
                    }
                    init2 = true;
                    break;
                case 7:
                    if (selection == 1) {
                        compareArea(&shapes[1], &shapes[6]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[1], &shapes[6]);
                    }
                    else {
                        cout << shapes[1]->getID() << " --";
                        shapes[1]->print();
                        cout << shapes[6]->getID() << " --";
                        shapes[6]->print();
                    }
                    init2 = true;
                    break;
                case 8:
                    if (selection == 1) {
                        compareArea(&shapes[1], &shapes[7]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[1], &shapes[7]);
                    }
                    else {
                        cout << shapes[1]->getID() << " --";
                        shapes[1]->print();
                        cout << shapes[7]->getID() << " --";
                        shapes[7]->print();
                    }
                    init2 = true;
                    break;
                default:
                    cout << "\n  The selected object does not exist! Try again.\n";
                }
            } while (init2 == false);
            init = true;
            break;

        case 3:
            do {
                cout << "  Object #2 (between 1 through 4): ";
                cin >> obj2;

                switch (obj2) {
                case 1:
                    if (selection == 1) {
                        compareArea(&shapes[2], &shapes[0]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[2], &shapes[0]);
                    }
                    else {
                        cout << shapes[2]->getID() << " --";
                        shapes[2]->print();
                        cout << shapes[0]->getID() << " --";
                        shapes[0]->print();
                    }
                    init2 = true;
                    break;
                case 2:
                    if (selection == 1) {
                        compareArea(&shapes[2], &shapes[1]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[2], &shapes[1]);
                    }
                    else {
                        cout << shapes[2]->getID() << " --";
                        shapes[2]->print();
                        cout << shapes[1]->getID() << " --";
                        shapes[1]->print();
                    }
                    init2 = true;
                    break;
                case 3:
                    if (selection == 1) {
                        compareArea(&shapes[2], &shapes[2]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[2], &shapes[2]);
                    }
                    else {
                        cout << shapes[2]->getID() << " --";
                        shapes[2]->print();
                        cout << shapes[2]->getID() << " --";
                        shapes[2]->print();
                    }
                    break;
                case 4:
                    if (selection == 1) {
                        compareArea(&shapes[2], &shapes[3]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[2], &shapes[3]);
                    }
                    else {
                        cout << shapes[2]->getID() << " --";
                        shapes[2]->print();
                        cout << shapes[3]->getID() << " --";
                        shapes[3]->print();
                    }
                    init2 = true;
                    break;
                case 5:
                    if (selection == 1) {
                        compareArea(&shapes[2], &shapes[4]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[2], &shapes[4]);
                    }
                    else {
                        cout << shapes[2]->getID() << " --";
                        shapes[2]->print();
                        cout << shapes[4]->getID() << " --";
                        shapes[4]->print();
                    }
                    init2 = true;
                    break;
                case 6:
                    if (selection == 1) {
                        compareArea(&shapes[2], &shapes[5]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[2], &shapes[5]);
                    }
                    else {
                        cout << shapes[2]->getID() << " --";
                        shapes[2]->print();
                        cout << shapes[5]->getID() << " --";
                        shapes[5]->print();
                    }
                    init2 = true;
                    break;
                case 7:
                    if (selection == 1) {
                        compareArea(&shapes[2], &shapes[6]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[2], &shapes[6]);
                    }
                    else {
                        cout << shapes[2]->getID() << " --";
                        shapes[2]->print();
                        cout << shapes[6]->getID() << " --";
                        shapes[6]->print();
                    }
                    init2 = true;
                    break;
                case 8:
                    if (selection == 1) {
                        compareArea(&shapes[2], &shapes[7]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[2], &shapes[7]);
                    }
                    else {
                        cout << shapes[2]->getID() << " --";
                        shapes[2]->print();
                        cout << shapes[7]->getID() << " --";
                        shapes[7]->print();
                    }
                    init2 = true;
                    break;
                default:
                    cout << "\n  The selected object does not exist! Try again.\n";
                }
            } while (init2 == false);
            init = true;
            break;

        case 4:
            do {
                cout << "  Object #2 (between 1 through 4): ";
                cin >> obj2;

                switch (obj2) {
                case 1:
                    if (selection == 1) {
                        compareArea(&shapes[3], &shapes[0]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[3], &shapes[0]);
                    }
                    else {
                        cout << shapes[3]->getID() << " --";
                        shapes[3]->print();
                        cout << shapes[0]->getID() << " --";
                        shapes[0]->print();
                    }
                    init2 = true;
                    break;
                case 2:
                    if (selection == 1) {
                        compareArea(&shapes[3], &shapes[1]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[3], &shapes[1]);
                    }
                    else {
                        cout << shapes[3]->getID() << " --";
                        shapes[3]->print();
                        cout << shapes[1]->getID() << " --";
                        shapes[1]->print();
                    }
                    init2 = true;
                    break;
                case 3:
                    if (selection == 1) {
                        compareArea(&shapes[3], &shapes[2]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[3], &shapes[2]);
                    }
                    else {
                        cout << shapes[3]->getID() << " --";
                        shapes[3]->print();
                        cout << shapes[2]->getID() << " --";
                        shapes[2]->print();
                    }
                    init2 = true;
                    break;
                case 4:
                    if (selection == 1) {
                        compareArea(&shapes[3], &shapes[3]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[3], &shapes[3]);
                    }
                    else {
                        cout << shapes[3]->getID() << " --";
                        shapes[3]->print();
                        cout << shapes[3]->getID() << " --";
                        shapes[3]->print();
                    }
                    init2 = true;
                    break;
                case 5:
                    if (selection == 1) {
                        compareArea(&shapes[3], &shapes[4]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[3], &shapes[4]);
                    }
                    else {
                        cout << shapes[3]->getID() << " --";
                        shapes[3]->print();
                        cout << shapes[4]->getID() << " --";
                        shapes[4]->print();
                    }
                    init2 = true;
                    break;
                case 6:
                    if (selection == 1) {
                        compareArea(&shapes[3], &shapes[5]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[3], &shapes[5]);
                    }
                    else {
                        cout << shapes[3]->getID() << " --";
                        shapes[3]->print();
                        cout << shapes[5]->getID() << " --";
                        shapes[5]->print();
                    }
                    init2 = true;
                    break;
                case 7:
                    if (selection == 1) {
                        compareArea(&shapes[3], &shapes[6]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[3], &shapes[6]);
                    }
                    else {
                        cout << shapes[3]->getID() << " --";
                        shapes[3]->print();
                        cout << shapes[6]->getID() << " --";
                        shapes[6]->print();
                    }
                    init2 = true;
                    break;
                case 8:
                    if (selection == 1) {
                        compareArea(&shapes[3], &shapes[7]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[3], &shapes[7]);
                    }
                    else {
                        cout << shapes[3]->getID() << " --";
                        shapes[3]->print();
                        cout << shapes[7]->getID() << " --";
                        shapes[7]->print();
                    }
                    init2 = true;
                    break;
                default:
                    cout << "\n  The selected object does not exist! Try again.\n";
                }
            } while (init2 == false);
            init = true;
            break;
        case 5:
            do {
                cout << "  Object #2 (between 1 through 4): ";
                cin >> obj2;

                switch (obj2) {
                case 1:
                    if (selection == 1) {
                        compareArea(&shapes[4], &shapes[0]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[4], &shapes[0]);
                    }
                    else {
                        cout << shapes[4]->getID() << " --";
                        shapes[4]->print();
                        cout << shapes[0]->getID() << " --";
                        shapes[0]->print();
                    }
                    init2 = true;
                    break;
                case 2:
                    if (selection == 1) {
                        compareArea(&shapes[4], &shapes[1]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[4], &shapes[1]);
                    }
                    else {
                        cout << shapes[4]->getID() << " --";
                        shapes[4]->print();
                        cout << shapes[1]->getID() << " --";
                        shapes[1]->print();
                    }
                    init2 = true;
                    break;
                case 3:
                    if (selection == 1) {
                        compareArea(&shapes[4], &shapes[2]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[4], &shapes[2]);
                    }
                    else {
                        cout << shapes[4]->getID() << " --";
                        shapes[4]->print();
                        cout << shapes[2]->getID() << " --";
                        shapes[2]->print();
                    }
                    init2 = true;
                    break;
                case 4:
                    if (selection == 1) {
                        compareArea(&shapes[4], &shapes[3]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[4], &shapes[3]);
                    }
                    else {
                        cout << shapes[4]->getID() << " --";
                        shapes[4]->print();
                        cout << shapes[3]->getID() << " --";
                        shapes[3]->print();
                    }
                    init2 = true;
                    break;
                case 5:
                    if (selection == 1) {
                        compareArea(&shapes[4], &shapes[4]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[4], &shapes[4]);
                    }
                    else {
                        cout << shapes[4]->getID() << " --";
                        shapes[4]->print();
                        cout << shapes[4]->getID() << " --";
                        shapes[4]->print();
                    }
                    init2 = true;
                    break;
                case 6:
                    if (selection == 1) {
                        compareArea(&shapes[4], &shapes[5]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[4], &shapes[5]);
                    }
                    else {
                        cout << shapes[4]->getID() << " --";
                        shapes[4]->print();
                        cout << shapes[5]->getID() << " --";
                        shapes[5]->print();
                    }
                    init2 = true;
                    break;
                case 7:
                    if (selection == 1) {
                        compareArea(&shapes[4], &shapes[6]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[4], &shapes[6]);
                    }
                    else {
                        cout << shapes[4]->getID() << " --";
                        shapes[4]->print();
                        cout << shapes[6]->getID() << " --";
                        shapes[6]->print();
                    }
                    init2 = true;
                    break;
                case 8:
                    if (selection == 1) {
                        compareArea(&shapes[4], &shapes[7]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[4], &shapes[7]);
                    }
                    else {
                        cout << shapes[4]->getID() << " --";
                        shapes[4]->print();
                        cout << shapes[7]->getID() << " --";
                        shapes[7]->print();
                    }
                    init2 = true;
                    break;
                default:
                    cout << "\n  The selected object does not exist! Try again.\n";
                }
            } while (init2 == false);
            init = true;
            break;
        case 6:
            do {
                cout << "  Object #2 (between 1 through 4): ";
                cin >> obj2;

                switch (obj2) {
                case 1:
                    if (selection == 1) {
                        compareArea(&shapes[5], &shapes[0]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[5], &shapes[0]);
                    }
                    else {
                        cout << shapes[5]->getID() << " --";
                        shapes[5]->print();
                        cout << shapes[0]->getID() << " --";
                        shapes[0]->print();
                    }
                    init2 = true;
                    break;
                case 2:
                    if (selection == 1) {
                        compareArea(&shapes[5], &shapes[1]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[5], &shapes[1]);
                    }
                    else {
                        cout << shapes[5]->getID() << " --";
                        shapes[5]->print();
                        cout << shapes[1]->getID() << " --";
                        shapes[1]->print();
                    }
                    init2 = true;
                    break;
                case 3:
                    if (selection == 1) {
                        compareArea(&shapes[5], &shapes[2]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[5], &shapes[2]);
                    }
                    else {
                        cout << shapes[5]->getID() << " --";
                        shapes[5]->print();
                        cout << shapes[2]->getID() << " --";
                        shapes[2]->print();
                    }
                    init2 = true;
                    break;
                case 4:
                    if (selection == 1) {
                        compareArea(&shapes[5], &shapes[3]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[5], &shapes[3]);
                    }
                    else {
                        cout << shapes[5]->getID() << " --";
                        shapes[5]->print();
                        cout << shapes[3]->getID() << " --";
                        shapes[3]->print();
                    }
                    init2 = true;
                    break;
                case 5:
                    if (selection == 1) {
                        compareArea(&shapes[5], &shapes[4]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[5], &shapes[4]);
                    }
                    else {
                        cout << shapes[5]->getID() << " --";
                        shapes[5]->print();
                        cout << shapes[4]->getID() << " --";
                        shapes[4]->print();
                    }
                    init2 = true;
                    break;
                case 6:
                    if (selection == 1) {
                        compareArea(&shapes[5], &shapes[5]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[5], &shapes[5]);
                    }
                    else {
                        cout << shapes[5]->getID() << " --";
                        shapes[5]->print();
                        cout << shapes[5]->getID() << " --";
                        shapes[5]->print();
                    }
                    init2 = true;
                    break;
                case 7:
                    if (selection == 1) {
                        compareArea(&shapes[5], &shapes[6]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[5], &shapes[6]);
                    }
                    else {
                        cout << shapes[5]->getID() << " --";
                        shapes[5]->print();
                        cout << shapes[6]->getID() << " --";
                        shapes[6]->print();
                    }
                    init2 = true;
                    break;
                case 8:
                    if (selection == 1) {
                        compareArea(&shapes[5], &shapes[7]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[5], &shapes[7]);
                    }
                    else {
                        cout << shapes[5]->getID() << " --";
                        shapes[5]->print();
                        cout << shapes[7]->getID() << " --";
                        shapes[7]->print();
                    }
                    init2 = true;
                    break;
                default:
                    cout << "\n  The selected object does not exist! Try again.\n";
                }
            } while (init2 == false);
            init = true;
            break;
        case 7:
            do {
                cout << "  Object #2 (between 1 through 4): ";
                cin >> obj2;

                switch (obj2) {
                case 1:
                    if (selection == 1) {
                        compareArea(&shapes[6], &shapes[0]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[6], &shapes[0]);
                    }
                    else {
                        cout << shapes[6]->getID() << " --";
                        shapes[6]->print();
                        cout << shapes[0]->getID() << " --";
                        shapes[0]->print();
                    }
                    init2 = true;
                    break;
                case 2:
                    if (selection == 1) {
                        compareArea(&shapes[6], &shapes[1]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[6], &shapes[1]);
                    }
                    else {
                        cout << shapes[6]->getID() << " --";
                        shapes[6]->print();
                        cout << shapes[1]->getID() << " --";
                        shapes[1]->print();
                    }
                    init2 = true;
                    break;
                case 3:
                    if (selection == 1) {
                        compareArea(&shapes[6], &shapes[2]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[6], &shapes[2]);
                    }
                    else {
                        cout << shapes[6]->getID() << " --";
                        shapes[6]->print();
                        cout << shapes[2]->getID() << " --";
                        shapes[2]->print();
                    }
                    init2 = true;
                    break;
                case 4:
                    if (selection == 1) {
                        compareArea(&shapes[6], &shapes[3]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[6], &shapes[3]);
                    }
                    else {
                        cout << shapes[6]->getID() << " --";
                        shapes[6]->print();
                        cout << shapes[3]->getID() << " --";
                        shapes[3]->print();
                    }
                    init2 = true;
                    break;
                case 5:
                    if (selection == 1) {
                        compareArea(&shapes[6], &shapes[4]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[6], &shapes[4]);
                    }
                    else {
                        cout << shapes[6]->getID() << " --";
                        shapes[6]->print();
                        cout << shapes[4]->getID() << " --";
                        shapes[4]->print();
                    }
                    init2 = true;
                    break;
                case 6:
                    if (selection == 1) {
                        compareArea(&shapes[6], &shapes[5]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[6], &shapes[5]);
                    }
                    else {
                        cout << shapes[6]->getID() << " --";
                        shapes[6]->print();
                        cout << shapes[5]->getID() << " --";
                        shapes[5]->print();
                    }
                    init2 = true;
                    break;
                case 7:
                    if (selection == 1) {
                        compareArea(&shapes[6], &shapes[6]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[6], &shapes[6]);
                    }
                    else {
                        cout << shapes[6]->getID() << " --";
                        shapes[6]->print();
                        cout << shapes[6]->getID() << " --";
                        shapes[6]->print();
                    }
                    init2 = true;
                    break;
                case 8:
                    if (selection == 1) {
                        compareArea(&shapes[6], &shapes[7]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[6], &shapes[7]);
                    }
                    else {
                        cout << shapes[6]->getID() << " --";
                        shapes[6]->print();
                        cout << shapes[7]->getID() << " --";
                        shapes[7]->print();
                    }
                    init2 = true;
                    break;
                default:
                    cout << "\n  The selected object does not exist! Try again.\n";
                }
            } while (init2 == false);
            init = true;
            break;
        case 8:
            do {
                cout << "  Object #2 (between 1 through 4): ";
                cin >> obj2;

                switch (obj2) {
                case 1:
                    if (selection == 1) {
                        compareArea(&shapes[7], &shapes[0]);
                    }
                    else if (selection == 2) {
                        compareVolume(&shapes[7], &shapes[0]);
                    }
                    else {
                        cout << shapes[7]->getID() << " --";
                        shapes[7]->print();
                        cout << shapes[0]->getID() << " --";
                        shapes[0]->print();
                    }
                    init2 = true;
                    break;
                case 2:
                    if (selection == 1) {
                        compareArea(&shapes[7], &shapes[1]);
                    } else if (selection == 2) {
                        compareVolume(&shapes[7], &shapes[1]);
                    } else {
                        cout << shapes[7]->getID() << " --";
                        shapes[7]->print();
                        cout << shapes[1]->getID() << " --";
                        shapes[1]->print();
                    }
                    init2 = true;
                    break;
                case 3:
                    if (selection == 1) {
                        compareArea(&shapes[7], &shapes[2]);
                    } else if (selection == 2) {
                        compareVolume(&shapes[7], &shapes[2]);
                    } else {
                        cout << shapes[7]->getID() << " --";
                        shapes[7]->print();
                        cout << shapes[2]->getID() << " --";
                        shapes[2]->print();
                    }
                    init2 = true;
                    break;
                case 4:
                    if (selection == 1) {
                        compareArea(&shapes[7], &shapes[3]);
                    } else if (selection == 2) {
                        compareVolume(&shapes[7], &shapes[3]);
                    } else {
                        cout << shapes[7]->getID() << " --";
                        shapes[7]->print();
                        cout << shapes[3]->getID() << " --";
                        shapes[3]->print();
                    }
                    init2 = true;
                    break;
                case 5:
                    if (selection == 1) {
                        compareArea(&shapes[7], &shapes[4]);
                    } else if (selection == 2) {
                        compareVolume(&shapes[7], &shapes[4]);
                    } else {
                        cout << shapes[7]->getID() << " --";
                        shapes[7]->print();
                        cout << shapes[4]->getID() << " --";
                        shapes[4]->print();
                    }
                    init2 = true;
                    break;
                case 6:
                    if (selection == 1) {
                        compareArea(&shapes[7], &shapes[5]);
                    } else if (selection == 2) {
                        compareVolume(&shapes[7], &shapes[5]);
                    } else {
                        cout << shapes[7]->getID() << " --";
                        shapes[7]->print();
                        cout << shapes[5]->getID() << " --";
                        shapes[5]->print();
                    }
                    init2 = true;
                    break;
                case 7:
                    if (selection == 1) {
                        compareArea(&shapes[7], &shapes[6]);
                    } else if (selection == 2) {
                        compareVolume(&shapes[7], &shapes[6]);
                    } else {
                        cout << shapes[7]->getID() << " --";
                        shapes[7]->print();
                        cout << shapes[6]->getID() << " --";
                        shapes[6]->print();
                    }
                    init2 = true;
                    break;
                case 8:
                    if (selection == 1) {
                        compareArea(&shapes[7], &shapes[7]);
                    } else if (selection == 2) {
                        compareVolume(&shapes[7], &shapes[7]);
                    } else {
                        cout << shapes[7]->getID() << " --";
                        shapes[7]->print();
                        cout << shapes[7]->getID() << " --";
                        shapes[7]->print();
                    }
                    init2 = true;
                    break;
                default:
                    cout << "\n  The selected object does not exist! Try again.\n";
                }
            } while (init2 == false);
            init = true;
            break;
        default:
            cout << "\n  The selected object does not exist! Try again.\n";
        }
    } while (init == false);
}
\$\endgroup\$
0
6
\$\begingroup\$

If you hard-code less data, you can easily compress these:

case 1:
    if (selection == 1) {
        compareArea(&shapes[0], &shapes[0]);
    }
    else if (selection == 2) {
        compareVolume(&shapes[0], &shapes[0]);
    }
    else {
        cout << "\n  " << shapes[0]->getID() << " --";
        shapes[0]->print();
        cout << "\n  " << shapes[0]->getID() << " --";
        shapes[0]->print();
        cout << "\n";
    }
    init2 = true;
    break;
case 2:
    if (selection == 1) {
        compareArea(&shapes[0], &shapes[1]);
    }
    else if (selection == 2) {
        compareVolume(&shapes[0], &shapes[1]);
    }
    else {
        cout << "\n  " << shapes[0]->getID() << " --";
        shapes[0]->print();
        cout << "\n\n  " << shapes[1]->getID() << " --";
        shapes[1]->print();
    }
    init2 = true;
    break;

Here, you are switching over obj2, so you can do something like this:

case 1:
case 2:
    if (selection == 1) {
        compareArea(&shapes[0], &shapes[obj1 - 1]);
    }
    else if (selection == 2) {
        compareVolume(&shapes[0], &shapes[obj1 - 1]);
    }
    else {
        cout << "\n  " << shapes[0]->getID() << " --";
        shapes[0]->print();
        cout << "\n  " << shapes[obj1 - 1]->getID() << " --";
        shapes[obj1 - 1]->print();
        cout << "\n";
    }
    init2 = true;
    break;

If you replace the hard-coded 0's in the selection above with obj1 - 1, you should be able to do away with the switches entirely.


I believe you have a bug in case 8 as it does not match the pattern:

else {
    cout << shapes[0]->getID() << " --";
    shapes[0]->print();
    cout << shapes[6]->getID() << " --";
    shapes[7]->print();
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.