Passing functions as parameters

I am trying to experiment with C++ to make the command prompt function like MatLab or Octave.

This is a practice file I've been doing so I could pass functions as parameters to other functions.

My problem is with my function call
compute(gset[function-1], input);

This is supposed to be correct according to what I know and according to another program that uses this similar snippet with the difference being the implementation of the function that I am passing a function to is in a separate header file.

So far I only have one compilation error and if somebody can help me with this, I'd truly be grateful.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
//#include <math.h>
//#include <iomanip>
//#include <stdio.h>
using namespace std;
   
double f1(double x)
{
    return(2*x);
}

double f2(double x)
{
    return(x*x);
}

double compute(double f(double x), double i)
{
       double answer;
       answer = f(i);
       return (answer);       
}

int main()
{
    double input, function, ans;
    
    double (*gset[])(double)={f1,f2};
    Input:
    
    cout << "Input Please: ";
    cin >> input;
    cout << "Function Please: ";
    cin >> function;
    
    ans = compute(gset[function-1], input);
    cout << ans;
    
    goto Input;
    
    
}
"function" is a double. Array index has to be int. How would you expect gset[0.5] to work?
The rest is good. See http://ideone.com/cpwmZ
By the way, that goto should be replaced by a loop. It's just a while(true) loop in this case.
Thank you hamsterman! That was really careless of me to use type double for index. I'll take your advice of using a while loop instead as well. I'm very grateful for your help.
Topic archived. No new replies allowed.