   finding percents in c
  hey everyone i am the author of a test MUD gaming engine.i have a problem im hoping someone will help me with.i am trying to take a number and turn it into the percentage of another number.Example:i have teleportation items set to take a set amount of experience gotten from the configuration file.in version 0.01 it was hardcoded 1% so it was simple.of(cptr->experience) cptr->experience-=(cptr->experience * .01);but in the latest version i have it an admin set option of how much to take for this example lets set it to 12.so:int percent=12;if(cptr->exp) cptr->exp-=(cptr->exp * .percent);how could i make this logic work?another percentage issue im having is i adjust melee combat damage based upon strength/5 with max strength being 50 so the bonus will never be more than 10%.# = (cptr->strength/5)what could be the best way to adjust the damage so it would be like dmg+=(dmg * .#)thanks!   
  I hope I only misunderstood something, but you can't do percentage calculation?Elementary school stuff.If so:if(cptr->exp) cptr->exp-=(cptr->exp * percent/100.0);   ;Quote:int percent=12;You probably want a float. You could do things like this:float rand_float_1(){  static const float invRandMax = 1.0f / (float)RAND_MAX;  return ( (float)rand() * invRandMax );}float damageBase = 10;float scale = 5.0f;float randomDamage = damageBase + rand_float_1()*scale; // get a random damage in the range [10,15)float expScale = 0.35f;float experience = experienceBase * expScale; // scale experience by this creatures exp amount (35%);  The conceptual problem in the OP is that you cannot replace:.01  // in this case, the . and the 01 are part of a single token, as far as the compiler understands it.with.percent   // The compiler is not designed to recognize this in the way you want.C/C++/C# simply don't let you do that (unless you use a macro...but I will personally burn you at the stake if you do that).The other guys have already posted the usual ways of achieving the goal.   ;Quote:Original post by szecsI hope I only misunderstood something, but you can't do percentage calculation?Elementary school stuff.If so:if(cptr->exp) cptr->exp-=(cptr->exp * percent/100.0);/shakingheadyes you are quite correct in your assumption. after sleeping and waking up fresh i noticed i had similiar logic going on in a previous function.//from MAIN.C/* load experience % lost on death */exp_loss = (double)(numopt(EXPLOSS,0,100)); if(exp_loss) exp_loss = exp_loss/100.0;//from DEATH.Cif((cptr->exp)&&(exp_loss)) cptr->exp-=(cptr->exp * exp_loss/100.0);thanks! :)   ;  As a general recommendation, you should probably use fractions (.01) instead of percentages (1) inside your code. A lot of multiplications and divisions by 100 can be saved that way. If you need to display one of these values to a user that is more used to percentages, make the conversion right in the code that displays it.   ;  Moving to For Beginners.   