Jump to content
  • Advertisement
Sign in to follow this  
Marz

Big Percentage problem.

This topic is 4873 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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


Link to post
Share on other sites
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.

Share this post


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


Link to post
Share on other sites
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!