I
I
Hello,
Question: How do I create a n array of pointer - to - functions to be filled by a user?
My goal is to create the calculate function, which takes two values and passes them to a array of pointer-to-functions, which calculates something from those 2 values and returns it.
I am not getting any build errors. It is definitly not running though.
I am guessing my problem is in one of 3 places:
1.) the calculate protoype
2.) the calculate function
3.) the defenition of double calculate(double y, double x, int z,double (*pt[])(double a,double b))
but I am running out of ideas (and still find pointers confusing). Any hints would be welcome and appreciated.
Thanks
I
Here is my code:
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <cmath>
#include <limits>
using namespace std;
//int size;
//functions below here
double calculate(double y, double x, int z,double (*pt[])(double a,double b)); //changed 4 to [] in hopes of creating a blank array
double add(double a, double b);
double subtract(double a, double b);
double mult(double a, double b);
double divide(double a, double b);
double mean(double a, double b);
double pythag(double a, double b);
/*
* begin main
*/
int main()
{
int choice,size_choice;
double (*pt[size_choice])(double a, double b);//={add,subtract,mult,divide};
double a,b,test;
cout<<"Enter two values: \n";
if(!(cin>>a>>b))
cout<<"Catastrophic Error!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
//Now going to attempt to write a switch that allow users to choose up to 5 functions to operate on their numbers
cout<<"Choose the # of functions you wish to use (only six functions currently available) \n";
while(1)
{
if(!(cin>>size_choice))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout<<"Failure\n";
}
else
break;
}
cout<<"Choose your functions: \n"
"1.) add 2.) subtract 3.) mult\n"
"4.) divide 5.) mean 6.) pythag\n";
//cin>>choice;
for(int i=0;i<size_choice;i++)
{
cin>>choice;
switch(choice)
{
case 1: pt=add;
break;
case 2: pt=subtract;
break;
case 3: pt=mult;
break;
case 4: pt=divide;
break;
case 5: pt=mean;
break;
case 6: pt=pythag;
break;
}
}
calculate(a,b,size_choice,pt);
return 0;
}
/*
*Define your functions after break!
*/
double calculate(double y, double x,int z, double (*pt[])(double,double))
{
double * temp = new double [z];
//double a,b,test=0;
for(int i=0;i<z;i++)
{
temp=(*pt)(x,y);
cout<<temp<<" next \n";
}
delete [] temp;
}
double add(double a, double b)
{
return a+b;
}
double subtract(double a, double b)
{
double total=a-b;
return total;
}
double mult(double a, double b)
{
return a*b;
}
double divide(double a, double b)
{
double total=a/b;
//double remainder=a%b;
return total;
}
double mean(double a, double b)
{
return (a+b)/2;
}
double pythag(double a, double b)
{
return sqrt((a*a)+(b*b));
}
Question: How do I create a n array of pointer - to - functions to be filled by a user?
My goal is to create the calculate function, which takes two values and passes them to a array of pointer-to-functions, which calculates something from those 2 values and returns it.
I am not getting any build errors. It is definitly not running though.
I am guessing my problem is in one of 3 places:
1.) the calculate protoype
2.) the calculate function
3.) the defenition of double calculate(double y, double x, int z,double (*pt[])(double a,double b))
but I am running out of ideas (and still find pointers confusing). Any hints would be welcome and appreciated.
Thanks
I
Here is my code:
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <cmath>
#include <limits>
using namespace std;
//int size;
//functions below here
double calculate(double y, double x, int z,double (*pt[])(double a,double b)); //changed 4 to [] in hopes of creating a blank array
double add(double a, double b);
double subtract(double a, double b);
double mult(double a, double b);
double divide(double a, double b);
double mean(double a, double b);
double pythag(double a, double b);
/*
* begin main
*/
int main()
{
int choice,size_choice;
double (*pt[size_choice])(double a, double b);//={add,subtract,mult,divide};
double a,b,test;
cout<<"Enter two values: \n";
if(!(cin>>a>>b))
cout<<"Catastrophic Error!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
//Now going to attempt to write a switch that allow users to choose up to 5 functions to operate on their numbers
cout<<"Choose the # of functions you wish to use (only six functions currently available) \n";
while(1)
{
if(!(cin>>size_choice))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout<<"Failure\n";
}
else
break;
}
cout<<"Choose your functions: \n"
"1.) add 2.) subtract 3.) mult\n"
"4.) divide 5.) mean 6.) pythag\n";
//cin>>choice;
for(int i=0;i<size_choice;i++)
{
cin>>choice;
switch(choice)
{
case 1: pt=add;
break;
case 2: pt=subtract;
break;
case 3: pt=mult;
break;
case 4: pt=divide;
break;
case 5: pt=mean;
break;
case 6: pt=pythag;
break;
}
}
calculate(a,b,size_choice,pt);
return 0;
}
/*
*Define your functions after break!
*/
double calculate(double y, double x,int z, double (*pt[])(double,double))
{
double * temp = new double [z];
//double a,b,test=0;
for(int i=0;i<z;i++)
{
temp=(*pt)(x,y);
cout<<temp<<" next \n";
}
delete [] temp;
}
double add(double a, double b)
{
return a+b;
}
double subtract(double a, double b)
{
double total=a-b;
return total;
}
double mult(double a, double b)
{
return a*b;
}
double divide(double a, double b)
{
double total=a/b;
//double remainder=a%b;
return total;
}
double mean(double a, double b)
{
return (a+b)/2;
}
double pythag(double a, double b)
{
return sqrt((a*a)+(b*b));
}