# Big Percentage problem.

## Recommended Posts

As I use percentages in a lot of my game ideas I thought it would be great to creat 1 overloaded function to handle the problem, 1 for returning int and one double. My code is flawed. Can you please tell me what is wrong and how I can go about fixing it. If there is a preprocesor function I can call, what is it and what header do I need to include. I appreciate any and all help. Thank you.
/* Create a function to deal with percentages.
PERCENT(int vPer, int vQuan) where vPer is a percentage
of vQuan the quantity. i.e. PERCENT(10,50) asks for 10 percent of 50 and
returns 5. Function needs to be overloaded to deal with 20 percent of 5 and so on.*/

#include <iostream>
using namespace std;
int PERCENT(int vPer, int vQuan);
double PERCENT(int vPer, int vQuan);

int main() {
int x, y;

cout << "\n\t\t\tEnter an amount:\n\n";
cin >> x;
cout << "\n\t\t\tEnter a percentage:\n\n";
cin >> y;
cout << "\n\t\t\t" << y << " percent of " <<x << "is: " << PERCENT(y,x)<< "\n\n";

system("PAUSE");
return 0;
}
// int function
int PERCENT(int vPer, int vQuan){
double vHold;
int vFin;

vHold = vQuan*vPer;

vFin = vHold/100;

return vFin;
}
//Same for double
double PERCENT(int vPer, int vQuan){
double vHold;
double vFin;

vHold = vQuan*vPer;
vFin = vHold/100;



##### Share on other sites
The problem is that the compiler doesn't know what function to call. You can't create 2 functions with the same name and parameters, but different return types. You'll need to rename one of the functions, or change the parameters.

##### Share on other sites
If you really need that much precision, I'd suggest keeping the double one and then casting it whenever you need to.

##### Share on other sites
Just return double. Cast to int whenever necessary.

Oh, and change your parameters to doubles, too, for greater accuracy. Integer division can really trip you up.

##### Share on other sites
Thanks everyone. Can you show me how to cast?
Sorry for being a pain but I can't find any examples in my books.

##### Share on other sites
Quote:
 Original post by MarzThanks everyone. Can you show me how to cast?Sorry for being a pain but I can't find any examples in my books.

int percent = (int)PERCENT(10,50);

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628379
• Total Posts
2982353

• 10
• 9
• 15
• 24
• 11