I'm having issues with subtracting a value and iterating during a loop.

I'm trying to create a loop that calculates interest, subtracts it from the balance, and continues until the balance is equal to 0. For some reason, the loop doesn't subtract after the first loop. Rather, it gives a random list of interest values and the balance stays the same. This is for an assignment in my intro to C++ class and can't figure this part out. The calculation does have to be in another function and has to be called from int main(). Thanks in the advance!


output is as follows and repeats forever.
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
Enter the your balance: 624
Enter your interest rate: 24
Enter your minimum payment: 4
1       599.04       24.96
2       598.8       0.239616
3       598.798       0.0023952
4       598.798       2.39519e-05
5       598.798       2.39519e-07
6       598.798       2.39519e-09
7       598.798       2.39519e-11
8       598.798       2.39519e-13
9       598.798       2.39519e-15
10       598.798       2.39519e-17
11       598.798       2.39519e-19
12       598.798       2.39519e-21
13       598.798       2.39519e-23
14       598.798       2.39519e-25
15       598.798       2.39519e-27
16       598.798       2.39519e-29
17       598.798       2.39519e-31
18       598.798       2.39519e-33
19       598.798       2.39519e-35
20       598.798       2.39519e-37
21       598.798       2.39519e-39
22       598.798       2.39519e-41
23       598.798       2.39519e-43
24       598.798       2.39519e-45
25       598.798       2.39519e-47
26       598.798       2.39519e-49
27       598.798       2.39519e-51
28       598.798       2.39519e-53
29       598.798       2.39519e-55
30       598.798       2.39519e-57
31       598.798       2.39519e-59
32       598.798       2.39519e-61
33       598.798       2.39519e-63
34       598.798       2.39519e-65
35       598.798       2.39519e-67
36       598.798       2.39519e-69
37       598.798       2.39519e-71
38       598.798       2.39519e-73
39       598.798       2.39519e-75
40       598.798       2.39519e-77
41       598.798       2.39519e-79
42       598.798       2.39519e-81
43       598.798       2.39519e-83
44       598.798       2.39519e-85
45       598.798       2.39519e-87
46       598.798       2.39519e-89
47       598.798       2.39519e-91
48       598.798       2.39519e-93
49       598.798       2.39519e-95
50       598.798       2.39519e-97
51       598.798       2.39519e-99
52       598.798       2.39519e-101
53       598.798       2.39519e-103
54       598.798       2.39519e-105
55       598.798       2.39519e-107
56       598.798       2.39519e-109
57       598.798       2.39519e-111
58       598.798       2.39519e-113
59       598.798       2.39519e-115
60       598.798       2.39519e-117
61       598.798       2.39519e-119
62       598.798       2.39519e-121
63       598.798       2.39519e-123
64       598.798       2.39519e-125
65       598.798       2.39519e-127
66       598.798       2.39519e-129
67       598.798       2.39519e-131
68       598.798       2.39519e-133
69       598.798       2.39519e-135
70       598.798       2.39519e-137
71       598.798       2.39519e-139
72       598.798       2.39519e-141
73       598.798       2.39519e-143
74       598.798       2.39519e-145
75       598.798       2.39519e-147
76       598.798       2.39519e-149
77       598.798       2.39519e-151
78       598.798       2.39519e-153
79       598.798       2.39519e-155
80       598.798       2.39519e-157
81       598.798       2.39519e-159
82       598.798       2.39519e-161
83       598.798       2.39519e-163
84       598.798       2.39519e-165
85       598.798       2.39519e-167
86       598.798       2.39519e-169
87       598.798       2.39519e-171
88       598.798       2.39519e-173
89       598.798       2.39519e-175
90       598.798       2.39519e-177
91       598.798       2.39519e-179
92       598.798       2.39519e-181
93       598.798       2.39519e-183
94       598.798       2.39519e-185
95       598.798       2.39519e-187
96       598.798       2.39519e-189
97       598.798       2.39519e-191
98       598.798       2.39519e-193
99       598.798       2.39519e-195
100       598.798       2.39519e-197
101       598.798       2.39519e-199
102       598.798       2.39519e-201
103       598.798       2.39519e-203
104       598.798       2.39519e-205
105       598.798       2.39519e-207
106       598.798       2.39519e-209
107       598.798       2.39519e-211
108       598.798       2.39519e-213
109       598.798       2.39519e-215
110       598.798       2.39519e-217
111       598.798       2.39519e-219
112       598.798       2.39519e-221
113       598.798       2.39519e-223
114       598.798       2.39519e-225
115       598.798       2.39519e-227
116       598.798       2.39519e-229
117       598.798       2.39519e-231
118       598.798       2.39519e-233
119       598.798       2.39519e-235
120       598.798       2.39519e-237
121       598.798       2.39519e-239
122       598.798       2.39519e-241
123       598.798       2.39519e-243
124       598.798       2.39519e-245
125       598.798       2.39519e-247
126       598.798       2.39519e-249
127       598.798       2.39519e-251
128       598.798       2.39519e-253
129       598.798       2.39519e-255
130       598.798       2.39519e-257
131       598.798       2.39519e-259
132       598.798       2.39519e-261
133       598.798       2.39519e-263
134       598.798       2.39519e-265
135       598.798       2.39519e-267
136       598.798       2.39519e-269
137       598.798       2.39519e-271
138       598.798       2.39519e-273
139       598.798       2.39519e-275
140       598.798       2.39519e-277
141       598.798       2.39519e-279
142       598.798       2.39519e-281
143       598.798       2.39519e-283
144       598.798       2.39519e-285
145       598.798       2.39519e-287
146       598.798       2.39519e-289
147       598.798       2.39519e-291
148       598.798       2.39519e-293
149       598.798       2.39519e-295
150       598.798       2.39519e-297
151       598.798       2.39519e-299
152       598.798       2.39519e-301
153       598.798       2.39519e-303
154       598.798       2.39519e-305
155       598.798       2.39519e-307
156       598.798       2.39519e-309
157       598.798       2.39519e-311
158       598.798       2.39519e-313
159       598.798       2.39519e-315
160       598.798       2.39517e-317
161       598.798       2.39637e-319
162       598.798       2.95945e-321
163       598.798       0
164       598.798       0
165       598.798       0
166       598.798       0
167       598.798       0
[code]

[code]
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

void input(double &balance, double &interestRate, double &minPayment);
double calcfuturebalance(double &interestRate, double &balance, double &minPayment, int &month);

int main()
{
	double balance, interestRate, minPayment, interest;
	int month = 1;

	input(balance, interestRate, minPayment);

	while (balance > 0)
	{
		interest = calcfuturebalance(interestRate, balance, minPayment, month);

		// function to output to file
		cout << month << "       " << balance << "       " << interest << endl;
		month++;
	}
}
void input(double &balance, double &interestRate, double &minPayment)
{
	cout << "Enter the your balance: ";
	cin >> balance;

	cout << "Enter your interest rate: ";
	cin >> interestRate;

	cout << "Enter your minimum payment: ";
	cin >> minPayment;
}

double calcfuturebalance(double &interestRate, double &balance, double &minPayment, int &month)
{
	double interest;

	interestRate = interestRate / 100;
	minPayment = minPayment / 100;

	interest = balance * minPayment;
	balance = balance - interest;

	return interest;
}

Last edited on
> double calcfuturebalance(double &interestRate, double &balance, double &minPayment, int &month);
The only reference parameter that seems necessary would be balance.

Try passing all the others by value.
Hello kadengolda,

It would help if you post the Specs, (specifications), i.e., instructions that you were given for this assignment. I have a feeling that you are missing something.

You have balance = balance - interest;. It seems to me that you should be adding "interest" here before you subtract the payment. Unless you were given the formula(s) to use I believe you are going about it wrong.

I'm trying to create a loop that calculates interest, subtracts it from the balance, and continues until the balance is equal to 0.
At month 272 I found that "balance" is "0.0097878824153285002" and "interest" is " 0.00039151529661314002" and "minPayment" is "0.040000000000000001" not that you use "minPayment" in the "calcfuturebalance" function or anywhere else.

By what you are doing: "0.00978" minus "0.00039" is "0.00939" and that is at month 272 or 22 years.

The real question here is if this program is for credit card debt or for a conventional bank loan. This will most likely make a difference in how interest is calculated.

To understand how credit card interest is figured I found this site https://www.nerdwallet.com/blog/credit-cards/how-credit-card-interest-calculated/ to be useful.

I admit I am now well versed in the math for this, but my simple understand is that (interest = balance * interestRate) then interest is added to balance before you subtract the payment.

Then there is "minPayment". I am not sure if you intend this to be a percentage of the balance or a dollar amount.

Since you included "iomanip" put this before the while loop in "main". std::cout << std::fixed << std::showpoint << std::setprecision(2);

Then for the "cout" in the while loop try this: cout << std::setw(3) << month << " " << std::setw(7) << balance << " " << std::setw(5) << interest << endl;. These two lines will line up the output better.

I also noticed you included the header file "cmath". Simple "* / + -" does not require this header file.

Hope that helps,

Andy
Topic archived. No new replies allowed.