-
passing functions
Hello,
Could somebody tell me how to pass something from one function to another? For example, say I had 3 functions, one to get two numbers, one to add the two numbers together, and one to take that total and add tax to it. I'm trying to understand how to take the total from adding 2 numbers in one function and returning it to another to add tax to the total from the other function. I hope this makes sense. I'll post what I'm trying to do. This works ok until I try to add the tax function. Thanks. mcorn
code:--------------------------------------------------------------------------------
Code:
#include<iostream.h>
void GetCookieQty(float *,float *);
float CalcCookiePrice(float,float);
//float GetTaxes(); //new
#define l_price .95;
#define r_price .50;
//#define tx_rate .05; //new
int main(void)
{
float howmany;
float ptr1,ptr2,tax;
GetCookieQty(&ptr1,&ptr2);
howmany=CalcCookiePrice(ptr1,ptr2);
// tax=GetTaxes(); //new
return 0;
}
void GetCookieQty(float *ptr1,float *ptr2)
{
cout<<"\nHow many large cookies? ";
cin>>*ptr1;
cout<<"How many regular cookies? ";
cin>>*ptr2;
}
float CalcCookiePrice(float ptr1,float ptr2)
{
float num1,num2,num3;
cout.precision(2);
cout.setf(ios::showpoint | ios::fixed);
num1= ptr1 * l_price;
num2= ptr2 * r_price;
num3= num1+num2;
cout<<"\n"<< num1 << " for large cookies."
<<"\n"<< num2 << " for regular cookies."
<<"\n\nThe total is " << num3<<endl<<endl;
return num3; //this was return 0;
}
//float GetTaxes(float num4) //new
//{
// return num4=num3*tx_rate;
//cout<<num3; //new
//}
[code][/code]tagged by Salem
-
Use code tags next time please.
You will need to pass it in as a parameter:
Code:
float GetTaxes(float);
...
tax=GetTaxes(howmany);
...
float GetTaxes(float num4)
{
float temp= num4 + num4*tx_rate;
cout<<temp<<endl;
return temp;
}
-
Some quick corrections:
in C++ we use const to declare constants not #define so
Code:
const float i_price .95;
const float r_price .50;
Also, standard C++ does not use the .h for header files so
Code:
#include <iostream>
using namespace std;
Also, why are you using pointers - you have not declared any and use pass by reference using &- it will help.
Also, do you have some book to help with this???
Mr. C.
-
Thanks for the tips! mcorn
-
Hi,
I'm using pointers because the book that I'm learning from stated that in order to return 2 or more values I must use pointers. I guess I could have misunderstood the point. If pointers are not right, then how would you return more than one value? Thanks. mcorn