Changing monthly payment to match balance

Hello! First time poster, and I was wondering if anyone could help me figure out what to do at the end of this assignment. The whole point is that I am constantly putting down payments of 100 dollars towards $5000 in a savings account that increases in interest by 1%. As time goes by, the interest and balance increases and the shortage decreases. The problem I am having is that at the end, I can no longer put down 100 dollars to reach exactly 5000 in my balance. How would I go about changing it so that I get the exact amount of interest and payment needed? This is what I have so far:

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
  #include <iostream>
using namespace std;
int main()
{
    int month = 0;
    double balance = 0;
    double payment = 100;
    double interest = 0;
    double shortage = 5000;

    while(shortage > 0){
        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(2);
        cout << month << "     " << balance << "     " << payment << "     " << interest << "     " << shortage << endl;
        interest = .001*balance;
        shortage = shortage - payment - interest;
        balance += (payment + interest);
        month = month++;
        if (shortage < 100)
        {
            payment = shortage - interest;
            cout << month << "     " << balance << "     " << payment << "     " << interest << "     " << shortage << endl;
        }
    }
return 0;
}
so if i understand you then your trying to get you balance to 5000 and then end you code, right?

on line 17 why are you taking away the interest from the shortage?
1
2
3
4
5
//this is how i would have the loop
while(balance<5000){

shortage = shortage - payment; // line 17

this was the last 2 lines of output i got when i made the changes, i also took out ur if statement

47 ... 4809.74.... 100.00.... 4.71..... 300.00
48 ... 4914.55..... 100.00.... 4.81..... 200.00

once you get this output then you can write a code that checks what is missing from the balance
1
2
3
4
5
interest = .001*balance;
balance+=interest  // bal now = 4924.22 (somthing like that)
missing = balance - 5000 // missing = 75.78

//now add missing to balance and take a way missing from the shortage 


let me know if this helps
Topic archived. No new replies allowed.