Big Percentage problem.

Started by
4 comments, last by Evil Steve 19 years, 2 months ago
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;

Advertisement
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.
If you really need that much precision, I'd suggest keeping the double one and then casting it whenever you need to.
Rob Loach [Website] [Projects] [Contact]
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.
Thanks everyone. Can you show me how to cast?
Sorry for being a pain but I can't find any examples in my books.
Quote:Original post by Marz
Thanks 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);

This topic is closed to new replies.

Advertisement