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 this function call found in line 36

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.

#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;
    
    
}

So.. what's the compilation error message? At what line?

I would probably try to pass in a function pointer instead, since "gset" is an array of function pointers, maybe try to define the "compute" function as:

double compute(double (*f)(double x), double i)

The rest can remain unchanged.

Also, as a matter of habit, you should avoid using a goto statement, because it is generally considered bad practice and off-putting for any programmer who might look at the code. Especially in this case, it is easy to avoid it with:

while(true) {
 
        cout << "Input Please: ";
        cin >> input;
        cout << "Function Please: ";
        cin >> function;
 
        ans = compute(gset[function-1], input);
        cout << ans;
 
    };

Also notice how the indentation makes it clearer that this is a loop. Even if you use goto-statements, you should indent if you can.

Thank you, mike, for your reply and your advice. :D

Using the code that I posted, the compiler error is from line 36:

invalid types 'double(*[2](double)[double]' for array subscript

From what I know, since gset is an array I can simply use the function at an index function - 1.

By making the function call:

compute(gset[function-1], input);

I hoped that gset[function-1] could call the function i needed from the array.

I tried defining function "compute" with this

double compute(double (*f)(double x), double i)

but more errors arose

I'm not sure why there are errors in my original code.

So.. what's the compilation error message? At what line?

I would probably try to pass in a function pointer instead, since "gset" is an array of function pointers, maybe try to define the "compute" function as:

double compute(double (*f)(double x), double i)

The rest can remain unchanged.

Also, as a matter of habit, you should avoid using a goto statement, because it is generally considered bad practice and off-putting for any programmer who might look at the code. Especially in this case, it is easy to avoid it with:

while(true) {
 
        cout << "Input Please: ";
        cin >> input;
        cout << "Function Please: ";
        cin >> function;
 
        ans = compute(gset[function-1], input);
        cout << ans;
 
    };

Also notice how the indentation makes it clearer that this is a loop. Even if you use goto-statements, you should indent if you can.

No worries. Got the code fixed already. I had the foolish mistake of using type double for a variable that's going to be used for an index on line 26 from my original code. I should've used type int for function. Thanks again. :)

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.