all these element of the array 'calc' are functions .
but i guess this is not not allowed so can you provide me with any solution that how can i run those function as the whole program is a menu driven program and the array 'calc' is called inside a for loop but as the functions are not allowed the main things wont run.
#include <iostream>
void foo()
{
std::cout << "foo\n";
}
void bar()
{
std::cout << "bar\n";
}
int main()
{
//Type aliases are recommended when you are dealing with function types
using func_t = void(void);
func_t* ops[] = {foo, bar};
ops[0](); //Call function in array
ops[1]();
}
Here is more complex example using function objects:
#include <iostream>
#include <functional>
#include <array>
int main()
{
std::array<
std::pair<std::string, std::function<double(double, double)>>,
4
> operations {{ //Note that () here creates object, not calls a function
//As all those are function objects, not functions
{"Add", std::plus<double>()},
{"Subtract", std::minus<double>()},
{"Multiply", std::multiplies<double>()},
{"Divide", std::divides<double>()},
}};
std::cout << "Enter operands: ";
double x, y;
std::cin >> x >> y;
std::cout << "What do you want to do with them?\n";
int n = 0;
for(constauto& e: operations)
std::cout << ++n << ' ' << e.first << '\n';
std::cin >> n;
std::cout << operations.at(n-1).second(x, y);
}
If you can, het another compiler. Turbo C++ is non-standard ancient compiler which didnt update for 20 years. You will have to learn C++ again if you ever intend to ever actually use it (and not get credit for it and forget all about C++).
#include <stdio.h>
void foo()
{
printf("foo\n");
}
void bar()
{
printf("bar\n");
}
int main()
{
//Type aliases are recommended when you are dealing with function types
typedefvoid(*func_p)();
func_p ops[2] = {foo, bar};
ops[0](); //Call function in array
ops[1]();
return 0;
}