Value returning functions
Oct 18, 2012 at 3:47am UTC
I need to modify the program so it uses a value returning function to determine commission. I am not sure what to do. Any help would be nice. I have started to modify it and here's what I have so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <iostream>
#include <iomanip>
using namespace std;
//function prototype
int getCommission(int sales, double commission);
int main()
{
//delcare variables
int sales = 0;
double commission = 0.0;
//enter input
cout << "Sales: " ;
cin >> sales;
//determine commission
if (sales < 0)
commission = -1;
else if (sales <= 100000)
commission = sales * .02;
else if (sales <= 400000)
commission = (sales - 100000) * .05 + 2000;
else
commission = (sales - 400000) *.1 + 17000;
//end if
//display commission or error message
if (commission != -1)
{
cout << fixed << setprecision(2);
cout << "Commission: $" << commission << endl;
}
else
cout << "The sales cannot be less than 0." << endl;
//end if
system("pause" );
return 0;
} //end of main function
//*****function defifintions*****
int getCommission(int sales, double commission)
{
Oct 18, 2012 at 4:24am UTC
I'm slightly confused on what you're looking for. Isn't the function int main() used for determining commission? I mean to say that it accepts an input (sales) and then gives you the output (commission) in return.
If you're looking to condense this into a single reusable function this forum post may be of some use:
http://www.cplusplus.com/forum/beginner/5234/
Last edited on Oct 18, 2012 at 4:40am UTC
Oct 18, 2012 at 4:40am UTC
We have to modify a program from an earlier assignment. I guess thats why youre confused. The code I posted is the original with some small modifications that I made.
Oct 18, 2012 at 4:49am UTC
instead of
return 0;
should you use
return commission;
Oct 18, 2012 at 5:53pm UTC
Okay I did that but still not getting anything. I changed some stuff heres what I have now...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <iostream>
#include <iomanip>
using namespace std;
//function prototype
int getCommission(int sales, int commission);
int main()
{
//delcare variables
int sales = 0;
int commission = 0.0;
//enter input
cout << "Sales: " ;
cin >> sales;
//determine commission
if (sales < 0)
commission = -1;
else if (sales <= 100000)
commission = sales * .02;
else if (sales <= 400000)
commission = (sales - 100000) * .05 + 2000;
else
commission = (sales - 400000) *.1 + 17000;
//end if
if (commission != -1)
{
cout << fixed << setprecision(2);
cout << "Commission: $" << commission << endl;
}
else
cout << "The sales cannot be less than 0." << endl;
commission = getCommission (sales, commission);
system("pause" );
return commission;
} //end of main function
//*****function defifintions*****
int getCommission(int sales, double commission)
I'm pretty sure I need more below line 43 but do not know what goes there
Last edited on Oct 18, 2012 at 5:54pm UTC
Oct 18, 2012 at 6:28pm UTC
Sorry that I keep bugging you guys with this problem, I am confused. I added more here's what I have...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <iostream>
#include <iomanip>
using namespace std;
//function prototype
int getCommission(int sales, int commission);
int main()
{
//delcare variables
int sales = 0;
int commission = 0.0;
//enter input
cout << "Sales: " ;
cin >> sales;
commission = getCommission (sales, commission);
//display commission
cout << "Commission: " << commission << endl;
system("pause" );
return 0;
} //end of main function
//*****function defifintions*****
int getCommission(int sales, double commission)
{
//determine commission
if (sales < 0)
commission = -1;
else if (sales <= 100000)
commission = sales * .02;
else if (sales <= 400000)
commission = (sales - 100000) * .05 + 2000;
else
commission = (sales - 400000) *.1 + 17000;
//end if
if (commission != -1)
{
cout << fixed << setprecision(2);
cout << "Commission: $" << commission << endl;
}
else
cout << "The sales cannot be less than 0." << endl;
return getCommission;
} //end of getCommission function
I get an error saying illegal operator on pointer to member function expression
What do i do to fix that?
Last edited on Oct 18, 2012 at 6:29pm UTC
Oct 18, 2012 at 6:50pm UTC
I have not tested this or looked at it in detail but this is the gist i suggest you read up on functions while loops and for loops. also when talking about money use the double or float type as money very rarely end on the dollar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
int main() {
double commission = getCommission;
cout << " your commission is : $" << commission << endl;
return 0;
} //end of main function
//*****function defifintions*****
double getCommission()
{
bool finished = false ;
while (finished == false ) {
cout << "Sales: " ;
cin >> sales;
//determine commission
if (sales < 0)
commission = -1;
finished = true ;
else if (sales <= 100000)
commission = sales * .02;
finished = true ;
else if (sales <= 400000)
commission = (sales - 100000) * .05 + 2000;
finished = true ;
else
commission = (sales - 400000) *.1 + 17000;
finished = true ;
//end if
if (commission < 0)
{
cout << "The sales cannot be less than 0. Please try again" << endl;
}
return commission;
} //end of getCommission function
Last edited on Oct 18, 2012 at 6:57pm UTC
Topic archived. No new replies allowed.