'Trying to build menu in C++ that will take functions

I want the program to ask the user to enter a number and then that function in the array will display on the screen

e.g

case 0 = displayNums (displays numbers entered by the user)
case 2 = getAverage (gets average of numbers entered)

I tried to code the menu to do that, but it only shows up. Nothing happens when the number for the specific function is entered.

#include <iostream>
#define integer 12
int ShowMenu(void);

double displayNums(double[], int);
double GetTotal(double[], int);
double getAverage(double[], int);
double getLargest(double[], int, int*);
double getSmallest(double[], int, int*);
int getNumOccurence(double[], int, int n);
double scaleUp(double[], int);

using namespace std;
int main() {
  cout << "enter 12 integers 1 by 1:\n";
  int data;
  int n = 1;

  // array to hold the integers
  double arr[integer];
  // get the integers

  for (int i = 0; i < 12; i++) {
    cin >> arr[i];
  }

  int option;
  do {
    option = ShowMenu();
    switch (option) {
      case 0:
        double displayNums();
        break;
      case 1:
        double GetTotal();
        break;
      case 2:
        double getAverage();
        break;
      case 3:
        double getLargest();
        break;
      case 4:
        double getSmallest();
        break;
      case 5:
        int getNumOccurence();
        break;
      case 6:
        double scaleUp();
        break;
      case 7:
        break;
      default:
        cout << "invalid option\n";
    }
  } while (option != 7);

  // displays numbers entered by user
  cout << displayNums(arr, integer) << endl;
  // displays sum of numbers entered
  cout << GetTotal(arr, integer) << endl;
  // displays the average
  cout << "Average integer is:" << getAverage(arr, integer) << endl;
  // displays the largest
  cout << "Largest integer is: " << getLargest(arr, integer, &data) << endl;
  // display the smallest integer
  cout << "Smallest integer is:" << getSmallest(arr, integer, &data) << endl;
  // display the occurence of the num
  cout << "Occurence integer is:" << getNumOccurence(arr, integer, n) << endl;
  // display the scale up integers
  cout << "Scaled up integers are:" << scaleUp(arr, integer) << endl;
  return 0;
}

int ShowMenu(void) {
  int option;
  cout << "\t0.     Display Numbers\n";
  cout << "\t1.     Get Total of numbers\n";
  cout << "\t2.     Get Average\n";
  cout << "\t3.     Get Largest\n";
  cout << "\t4.     Get Smallest\n";
  cout << "\t5.     Get Number Occurences\n";
  cout << "\t6.     Scale Up\n";
  cout << "\t7.     Quit\n";
  cout << "\t\t\tOption ? ";
  cin >> option;
  return option;
}

double displayNums(double arr[], int size) {
  double display = 0;
  for (int i = 0; i < size; i++) {  // for loop
  }
  cout << "the numbers that you have entered into the array are:" << endl;
  for (int i = 0; i < size; i++) {
    cout << arr[i] << endl;  // displays numbers entered
  }
  return display;
}

double GetTotal(double arr[], int size) {
  double sum = 0;
  for (int i = 0; i < size; i++) {
  }
  cout << "" << endl;
  for (int i = 0; i < 12; i++) {
    sum += arr[i];
  }
  cout << "The sum of all numbers entered is:" << sum << endl;
  return sum;
}

double getAverage(double arr[], int size) {
  double sum = 0.0, avg;
  for (int i = 0; i < size; i++) {
    sum = sum + arr[i];
  }
  avg = sum / size;
  return avg;
}

double getLargest(double arr[], int size, int* data) {
  double large = 0;
  for (int i = 0; i < size; i++) {
    if (arr[i] > large) {
      large = arr[i];
      *data = i + 1;
    }
  }
  return large;
}

double getSmallest(double arr[], int size, int* data) {
  double small = 10000;
  for (int i = 0; i < size; i++) {
    if (arr[i] < small) {
      small = arr[i];
      *data = i + 1;
    }
  }
  return small;
}

int getNumOccurence(double arr[], int size, int n) {
  // only works when you insert  repetable number 1
  int count = 0;

  for (int i = 0; i < size; i++) {
    if (n == arr[i]) {
      count++;
    }
  }

  return count;
}
double scaleUp(double arr[], int size) {
  int factor = 0;

  cout << "enter the scale up factor";
  cin >> factor;

  for (int i = 0; i < size; i++) {
    arr[i] *= factor;

    cout << arr[i] << endl;
  }
  return factor;
}


Solution 1:[1]

You could use a std::map instead of all those cases.

// Declare a synonym for a function pointer
typedef double (*Function_Pointer)();

// Declare an abbreviation for the map:
typedef std::map<int, Function_Pointer> Function_Display_Table;

// Initialize the function table:
Function_Display_Table display_table;
display_table[0] = display_nums;
display_table[1] = GetTotal;
display_table[2] = getAverage();
//...

// To process your selection:  
Function_Pointer fp = display_table.at(selection);
double return_value = fp();

To expand your menu selection, you only have to add rows to the display_table.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thomas Matthews