Having problem returning function....sigh pls help!

My code below will not return the value from the input in the first function. Sorry i am complete "noob" but the question i am busy with requires the layout below. When the second function runs (float calculateTax(float Rands, float taxRate)) the values of Rand and taxRate are not the numbers i inputted from the first function "float tax( float rands, float taxRate)". Grrr...please assist, code is below many thanks!!


# include <iostream>
using namespace std;

float tax( float rands, float taxRate)
{
cout << "Enter Rands:" << endl;
cin >> rands;
cout << "Enter tax rate:" << endl;
cin >> taxRate;
cout << "The Rands are R" << rands << " " << "with tax rate selected as %" << taxRate << " " << endl;

}

float calculateTax(float Rands, float taxRate)
{
return(Rands + taxRate);
}

void display(float answer)
{

cout << "The amount is:" << answer << endl;
}

int main()

{

float Rands1, tax1;
float taxResults;

tax(Rands1, tax1);
calculateTax(Rands1, tax1);
taxResults = calculateTax(Rands1, tax1);
display(taxResults);

return 0;
Hello. Please put all of your code between [code]code tags[/code] - http://www.cplusplus.com/articles/jEywvCM9/

1
2
float Rands1, tax1;
float taxResults;


Start by initializing these to 0.

When you do this -

tax(Rands1, tax1);

You're not actually sending in Rands1 and tax1, but copies. So once that function is done and it leaves the tax function, the rands and taxrate you send in to calculateTax are still 0, becuase you only changed some copies of it and not the actual thing. So you want to send it in by reference instead

float tax( float& rands, float& taxRate)

1
2
3
4
tax(Rands1, tax1);
calculateTax(Rands1, tax1); // remove this one
taxResults = calculateTax(Rands1, tax1); // keep this one
display(taxResults);


I also dont get why you are calling the calculateTax function twice, doesnt serve any purpose. Remove the first one and keep this one.

Your tax function is of type float, but it doesnt return anything. If you dont want a function to return anything then change it to a void function -

void tax(float& rands, float& taxRate);

Once you do all of these things, you're program will run fine.
Ah, thanks!! My code is working, i really had no idea about the & in the ( float& rands, float& taxRate). Your a life saver!
My pleasuer, would appricaite if you could edit your post and use the code tags as I mentioned so if someone finds this thread in the future they'll be able to read it properly.
Topic archived. No new replies allowed.