Sign in to follow this  
Marz

Big Percentage problem.

Recommended Posts

Marz    150
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 this post


Link to post
Share on other sites
Evil Steve    2017
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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
Evil Steve    2017
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);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this