Doing a Salary Structure Program but While Loop isn't doing what I want it to do

I am working on a salary restructuring program with the aim of moving all current salaries to a unified structure. The following are the parameters for the new structure:

Basic is 35% of Total Fixed Pay (TFP)
HRA is 85% of Basic
LTA is 8.33% (1/12) of Basic
Conveyance is same amount as LTA
PF is 12% of Basic
Variable Pay is 15% of Total Cost to Company (CTC)

Here comes the tricky part. There is a Special Allowance which is a residual figure with no intrinsic property per se, i.e. all salary that doesn't fit into the other components is added to Special. There is also a Gratuity component. The gratuity value is calculated as:

Gratuity = (5/104) * Inclusions

Where Inclusions = Basic + LTA + Special.

As you can see, this is a typical circular argument since gratuity is dependent on a changing and undefined variable Special. I wanted to automate this using a While Loop. This is the code I came up with:

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
85
86
87
88
89
90
91
  #include <iostream>
#include <algorithm>
using namespace std;

int& max(int& a, int& b)
{
	return a > b ? a : b;
}

int main()
{
	int basic1, hra1, spl1, lta1, conv1, pf1, grat1, varpay1;

	cout << "Current Basic Salary: \n";
	cin >> basic1;
	cout << "Current HRA: \n";
	cin >> hra1;
	cout << "Current Special Allowance: \n";
	cin >> spl1;
	cout << "Current LTA: \n";
	cin >> lta1;
	cout << "Current Conveyance/Transport Allowance: \n";
	cin >> conv1;
	cout << "Current PF: \n";
	cin >> pf1;
	cout << "Current Gratuity: \n";
	cin >> grat1;
	cout << "Current Variable Pay: \n";
	cin >> varpay1;

	int ctc1, tfp1, thp1;

	tfp1 = basic1 + hra1 + lta1 + spl1 + conv1 + pf1 + grat1;
	ctc1 = tfp1 + varpay1;
	thp1 = tfp1 - 2 * (pf1 + grat1);

	double basic1_pc, varpay1_pc;
	basic1_pc = (static_cast<double>(basic1) / tfp1) * 100;
	varpay1_pc = (static_cast<double>(varpay1) / ctc1) * 100;

	cout << "Current CTC is " << ctc1 << "\n";
	cout << "Current TFP is " << tfp1 << "\n";
	cout << "Current Basic is " << basic1_pc << "% of Current TFP \n";
	cout << "Current Variable Pay is " << varpay1_pc << "% of Current CTC \n";
	cout << "Current Fixed Pre-Tax Take-Home Pay is " << thp1 << "\n";

	double increment;

	cout << "Increment (input as % value, eg. if 6%, input 6):\n";
	cin >> increment;

	int ctc2, varpay2, tfp2, basic2, hra2, lta2, pf2, conv2, spl2;
	int basic2_plch;

	ctc2 = ctc1 * (1 + (increment / 100));
	varpay2 = 0.15 * ctc2;
	tfp2 = ctc2 - varpay2;
	spl2 = 0;

	basic2_plch = 0.35 * tfp2;
	basic2 = max(basic2_plch, basic1);
	hra2 = 0.85 * basic2;
	lta2 = basic2 / 12;
	pf2 = 0.12 * basic2;
	conv2 = basic2 / 12;

	cout << "New CTC will be " << ctc2 << "\n";
	cout << "New TFP will be " << tfp2 << "\n";
	cout << "New Basic Salary will be " << basic2 << "\n";
	cout << "New HRA will be " << hra2 << "\n";
	cout << "New LTA will be " << lta2 << "\n";
	cout << "New Conveyance Allowance will be " << conv2 << "\n";
	cout << "New PF will be " << pf2 << "\n";

	int wage2_plch, remainder2;
	wage2_plch = 0.5 * tfp2;
	remainder2 = tfp2 - basic2 - hra2 - lta2 - conv2 - pf2;

	while (remainder2 > 0)
	{
		int incl2 = basic2 + spl2 + lta2;
		int grat2 = max(incl2, wage2_plch) * (5 / 104);
		remainder2 = remainder2 - spl2 - grat2;
		spl2++;
	}

	cout << "New Special Allowance will be " << spl2 << "\n";
	cout << "New Variable Pay will be " << varpay2 << "\n";

	return 0;
}


My issue is that the While Loop isn't working properly. It's iterating for some time but always ends up ending the loop way earlier than required. Please point out what I'm doing wrong and how to get around this issue.
Line 82 is effectively int grat2 = 0; because (5/104) == 0.

However, that does not explain why the loop would end "early". Rather it should take longer, because you deduct "only" spl2 on each iteration.


Note also that ctc2 == ctc1, if increment < 100 (on line 55.)
Last edited on
I finally got that, so I circumvented that by creating a new function on top just to calculate the grat bit. I also converted all variables into double, since it became clear that some form of precision was being required in my calculation. This has had the unintended effect of making all my output in exponential form, which I'm rectifying.
Topic archived. No new replies allowed.