Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

create a text-based, menu-driven program that allows the user to choose whether

ID: 3553772 • Letter: C

Question

create a text-based, menu-driven program that allows the user to choose whether to add, subtract, multiply or divide two numbers. The program should then input two double values from the use, perform the appropriate calculation, and display the result. Use an array of function pointers in which each pointer represents a function that returns void and receives two double parameters. The corresponding functions should each display messages indicating which calculation was performed, the values of the parameters, and the result of the calculation.

Requirements:

Explanation / Answer

//header file section

#include <iostream>

using namespace std;

//Prototypes

void add(double, double);

void subtract(double, double);

void multiply(double, double);

void divide(double, double);

//Main method

int main() {

    // An array of function pointers to hold 4 functions

    void (* fnptr[4])(double, double) = {

        add, subtract, multiply, divide

    };

    int option;

       //Do-while loop

    do {

        //Menu-driven

        cout << " --------- Menu ----------";

        cout << " 0 - Add";

        cout << " 1 - Subtract";

        cout << " 2 - multiply";

        cout << " 3 - Divide";

        cout << " 4 - Exit ";

       //Output statement to the option

        cout << " Enter your option: ";

        //Get the value from user/standard keyboard

        cin >> option;

      //declaration of double values

        double a, b;

// if the option is in between 0 to 3,this statement will execute and ask two numbes from keyboard/user.

        if (option >= 0 && option <= 3) {

            cout << " Enter two numbers: ";

            cin >> a >> b;

//if option is equal to 4,it will come out of the loop.

        } else if (option == 4) {

            exit(0);

        }

     //if option is greater than 4 or less than -1,the below message will be displayed.

              else {

            cout << " Invalid Option! Enter again: " << endl;

            continue;

        }

      // switch statement

        switch (option) {

            //Addition function pointer

                     case 0: {

                fnptr[0](a, b);

                break;

            }

            //Subtraction function pointer

            case 1: {

                fnptr[1](a, b);

                break;

            }

            //Multiplication function pointer

            case 2: {

                fnptr[2](a, b);

                break;

            }

            //Division function pointer

            case 3: {

                fnptr[3](a, b);

                break;

            }

        }

    } while (option != 4);

        system("pause");

}

// this function will add two numbers

void add(double a, double b) {

    cout << " Add: " << a << " + " << b << " = " << a + b << endl;

}

//This function will substract two numbers.

void subtract(double a, double b) {

    cout << " Subtract: " << a << " - " << b << " = " << a - b << endl;  

}

//This function will multiply two numbers.

void multiply(double a, double b) {

    cout << " Multiply: " << a << " x " << b << " = " << a * b << endl;

}

//This function will divide two numbers.

void divide(double a, double b) {

    if (b == 0.0) {

        cout << "Cannot divide by 0!" << endl;

   } else {

        cout << " Divide: " << a << " / " << b << " = " << a / b << endl;

    }

}


Sample output:

--------- Menu ----------

0 - Add

1 - Subtract

2 - multiply

3 - Divide

4 - Exit

Enter your option: 0

Enter two numbers: 5.2

6.3

Add: 5.2 + 6.3 = 11.5

--------- Menu ----------

0 - Add

1 - Subtract

2 - multiply

3 - Divide

4 - Exit

Enter your option: 1

Enter two numbers: 5.6

2.3

Subtract: 5.6 - 2.3 = 3.3

--------- Menu ----------

0 - Add

1 - Subtract

2 - multiply

3 - Divide

4 - Exit

Enter your option: 2

Enter two numbers: 2.5

3.2

Multiply: 2.5 x 3.2 = 8

--------- Menu ----------

0 - Add

1 - Subtract

2 - multiply

3 - Divide

4 - Exit

Enter your option: 3

Enter two numbers: 6.6

2.2

Divide: 6.6 / 2.2 = 3

--------- Menu ----------

0 - Add

1 - Subtract

2 - multiply

3 - Divide

4 - Exit

Enter your option: