Code Problem - not getting the expected results

Ok, I recently posted a question and thx for you guys, my program is compiling now.
However it is not working as it should be. This program is a calculator designed to calculate the cast time of skills from the game Ragnarok Online.

There is something wrong with my calculation (computeCastTime function).
In the game, the higher the dexterity and intelligence or a character, the faster he should cast a skill.

This is the formula that I'm trying to make work:
castTime = (1 - SQRT(((DEX * 2) + INT) / 530)) * (1 - (variableReduction/100%)) * baseCast * 0.8) + ((1 - (fixedReduction/100%)) * baseCast * 0.2)

Example: supposing that a character in the game has these numbers/stats:
1
2
3
4
5
6
7
8
9
10
99 Int, 
99 Dex, 
base = 12 (takes 12 seconds to cast spell x), 
variable = 20 (there is a 20% of reduction of the variable cast time)
fixed = 0 (supposing that the character has none fixed reduction)

castTime = (1 - SQRT((99 * 2 + 99) / 530)) * (1 - 20/100%) * 12 * 0.8 + ((1 - 0/100%) * baseCast * 0.2)
castTime = (0.251416 * 0.8 * 12 * 0.8) + (1 * 12 * 0.2) = 4.33

therefore, this spell that is originally 12 seconds, with these stats and reductions, takes only 4.33s to be cast. That is a 63.9% of reduction.


In my program, every time that I run it, the output for castTime is always the same as base. castTime should always be lesser than the base cast time.

So here is the code, if you'd like to help me out =)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Cast Time Calculator
#include <iostream>
#include <cmath>
using namespace std;

float computeCastTime(int dexterity, int intelligence, int variable, int base, int fixed);
float computeReductionPercentage(float castTime, int base) ;
void display (int base, float castTime, float reduction);


// This will prompt the user for DEX, INT, Variable Cast Time Reduction (in %), Base Cast Time and Fixed Cast Time Redution (highest modifier)
int main ()
{

   // declaration of variables
   int dexterity = 0;
   int intelligence = 0;
   int variable = 0;
   int base = 0;
   int fixed = 0;
   float castTime = 0;
   float reduction = 0; 

   // Prompt
   cout << "Please insert DEX: ";
   cin >> dexterity;

   cout << "\nPlease insert INT: ";
   cin >> intelligence;

   cout << "\nPlease insert Variable Cast Time Reduction (in %): ";
   cin >> variable;

   cout << "\nPlease insert Fixed Cast Time Reduction (highest reduction): ";
   cin >> fixed;

   cout << "\nPlease insert Base Cast Time: ";
   cin >> base;

   // calculate the total cast time
   castTime = computeCastTime(dexterity, intelligence, variable, base, fixed);

   // calculate the reduction in %
   reduction = computeReductionPercentage(castTime, base);

   // display table with results
   display(base, castTime, reduction);

   // I inserted this so that the program window would not quit right after showing the results of the calculation
   cin >> intelligence;

   return 0;
}


// calculates the cast time reduction with the following formula:
// castTime = [(1 - SQRT((DEX * 2 + INT) / 530)) * (1 - sum_castReduction/100%) * baseCast * 0.8] + [(1 - max_fixedReduction/100%) * baseCast * 0.2]

float computeCastTime(int dexterity, int intelligence, int variable, int base, int fixed)
{
   float castTime = 0;

   castTime = (1 - (sqrt (((dexterity * 2) + intelligence) / 530))) * (1 - (variable / 100)) * (base * 0.8) + ((1 - (fixed / 100)) * base * 0.2);

   return castTime;
}


// reduction = 1- (castTime / base )
float computeReductionPercentage(float castTime, int base)
{
   float reduction = 0;
   reduction = (1- (castTime / base)) * 100;
   return reduction;
}



// displays the Base time, adjusted base time and reduction %
void display (int base, float castTime, float reduction)
{
cout << "\nBASE TIME\tCAST TIME\tREDUCTION\n";
cout << base << " seconds" << '\t' << castTime << " seconds\t" << reduction << " %\n";
}


I'm guessing you have an order of operations error somewhere...I'd break that up into several lines since it won't be any slower; Then you can comment each of the lines and say why you are doing it.
thx firedraco. that really helped me. My problem were the 2 divisions by 100 in the formula. They were int numbers being divided so I had to cast float on them.
Here is the new working function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
float computeCastTime(int dexterity, int intelligence, int variable, int base, int fixed)
{
   float castTimeVar = 0;
   float castTimeFix = 0;
   float castTime = 0;
   
   float reductionVar = 0;
   float reductionFix = 0;

//   castTime = (1 - (sqrt (((dexterity * 2) + intelligence) / 530))) * (1 - (variable / 100)) * (base * 0.8) + ((1 - (fixed / 100)) * base * 0.2);
   castTimeVar = dexterity * 2;
   castTimeVar += intelligence;
   castTimeVar /= 530;
   castTimeVar = sqrt (castTimeVar);
   castTimeVar = 1 - castTimeVar;

   reductionVar = float(variable) / 100;
   reductionVar = 1 - reductionVar;
  
   castTimeVar *=  reductionVar;
   castTimeVar *= base * 0.8;

   reductionFix = float(fixed) / 100;
   reductionFix = 1- reductionFix;

   castTimeFix = reductionFix *base * 0.2;
   castTime = castTimeVar + castTimeFix;

   return castTime;
}
Topic archived. No new replies allowed.