End loop after answer is 0

I am writing a program to tell how long it will take for a loan to be paid off supposing the desired loan is 100,000, the annual percentage rate is 6, and the monthly payment is 1,000. What happens is after I run the program, I get the output of "-24.60" after the last month is paid off. I'm trying to get it to where the last monthly payment is only enough to make the loan 0. Then I want the program to end.


#include <iostream>
#include <iomanip>
using namespace std;
int main()
{

double balance, interest, monthlyPay, apr;
int month=1;

cout<< "How much would you like to borrow?" << endl;
cin>> balance;
cout<< "What is the annual percentage rate?" << endl;
cin>> apr;
cout<< "How much would you like to pay monthly?" << endl;
cin>> monthlyPay;
cout<< endl;

interest = (apr / (12 * 100)) * balance;

if (monthlyPay > interest) {

cout << setprecision(2) << fixed << showpoint;

cout<< "Month" << " " << "Loan Balance Start" << " " << "Interest" << " "
<< "Payment" << " " << "Loan Balance End" << endl;

cout << setfill('-')
<< setw(5) << "" << " "
<< setw(18) << "" << " "
<< setw(8) << "" << " "
<< setw(7) << "" << " "
<< setw(16) << "" << "\n" << setfill(' ');


while (balance > 0)
{
interest = (apr / (12 * 100)) * balance;
cout << setw(5) << month << " " << setw(18) << balance << " ";
cout << setw(8) << interest << " ";

balance += interest;
balance -= monthlyPay;




cout << setw(7) << monthlyPay << " " << setw(16) << balance << " " << "\n";
month++;

if (balance < monthlyPay) *************
monthlyPay = balance; ************ PROBLEM AREA.
}


}
else
cout<< "You cannot pay this loan off." << endl;

return 0;
}
@ThisGuyIsBrad

Nice program. Here's the modified program, to end when balance becomes 0.

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
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{

	double balance, interest, monthlyPay, apr;
	int month=1;

	cout<< "How much would you like to borrow?" << endl;
	cin>> balance;
	cout<< "What is the annual percentage rate?" << endl;
	cin>> apr;
	cout<< "How much would you like to pay monthly?" << endl;
	cin>> monthlyPay;
	cout<< endl;

	interest = (apr / (12 * 100)) * balance;

	if (monthlyPay > interest) 
	{

		cout << setprecision(2) << fixed << showpoint;

		cout<< "Month" << " " << "Loan Balance Start" << " " << "Interest" << " "
			<< "Payment" << " " << "Loan Balance End" << endl;

		cout << setfill('-')
			<< setw(5) << "" << " "
			<< setw(18) << "" << " "
			<< setw(8) << "" << " "
			<< setw(7) << "" << " "
			<< setw(16) << "" << "\n" << setfill(' ');

		do
		{
			interest = (apr / (12 * 100)) * balance;
			cout << setw(5) << month << " " << setw(18) << balance << " ";
			cout << setw(8) << interest << " ";

			balance += interest;
			//balance -= monthlyPay;
			month++;
			if (balance < monthlyPay)
				monthlyPay = balance;
			balance -= monthlyPay;
			cout << setw(7) << monthlyPay << " " << setw(16) << balance << " " << "\n";
			

		}while(balance >0);
	}
	else
		cout<< "You cannot pay this loan off." << endl;

	return 0;
}
So I actually went another way around doing it. I got the problem fixed. Now my OTHER problem is I have to make it look pretty. I got all of my columns lined up, but when my numbers print, they're all out of wack. Here's what I have in this part:



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
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{

    double balance, interest, monthlyPay, apr;
    int month=1;
    
    cout<< "How much would you like to borrow?" << endl;
    cin>> balance;
    cout<< "What is the annual percentage rate?" << endl;
    cin>> apr;
    cout<< "How much would you like to pay monthly?" << endl;
    cin>> monthlyPay;
    cout<< endl;
    
    interest = (apr / (12 * 100)) * balance;
    
    if (monthlyPay > interest) {
        
        cout << setprecision(2) << fixed << showpoint;
        
        cout<< "Month" << "    " << "Loan Balance Start" << "    " << "Interest" << "    "
        << "Payment" << " " << "Loan Balance End" << endl;
        
        cout << setfill('-')
        << setw(5)  << "" << "    "
        << setw(18) << "" << "    "
        << setw(8)  << "" << "    "
        << setw(7)  << "" << "    "
        << setw(16) << "" << "\n" << setfill(' ');
        
        
        while (balance > 0)
        {
            
            interest = (apr / (12 * 100)) * balance;
            
            cout<< setw(3)<< month << setw(2)<<"";
            cout<< setw(10)<<"    "<< balance <<setw(8)<< "";
            cout<< setw(5) <<"    "<< interest <<setw(3)<< "";
          
            if (balance < monthlyPay){
                monthlyPay = balance + interest;
            }
            
            balance += interest;
            balance -= monthlyPay;
    
            
          
            
            cout<< setw(4) <<"    "<< monthlyPay <<setw(3) << "";
            cout<< setw(16)<<"    "<< balance <<""<< "\n";
            
           
           
            month++;
        }
        
        
    }
    else
        cout<< "You cannot pay this loan off." << endl;
    
    return 0;
}






It should come out like:

Month  Loan balance start   Interest   Payment  Loan balance end
-----  ------------------   --------   -------  ----------------
   1      100000.00          500.00    1000.00      99500.00
   2       99500.00          497.50    1000.00      98997.50
   3       98997.50          494.99    1000.00      98492.49
Last edited on
@ThisGuyIsBrad

Using the code I showed, this is the prettiest I can get it. It looks to me very close to what you're showing.

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
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{

	double balance, interest, monthlyPay, apr;
	int month=1;

	cout<< "How much would you like to borrow?" << endl;
	cin>> balance;
	cout<< "What is the annual percentage rate?" << endl;
	cin>> apr;
	cout<< "How much would you like to pay monthly?" << endl;
	cin>> monthlyPay;
	cout<< endl;

	interest = (apr / (12 * 100)) * balance;

	if (monthlyPay > interest) 
	{

		cout << setprecision(2) << fixed << showpoint;

		cout<< "Month" << " " << "Loan Balance Start" << " " << "Interest" << " "
			<< "Payment" << " " << "Loan Balance End" << endl;

		cout << setfill('-')
			<< setw(5) << "" << " "
			<< setw(18) << "" << " "
			<< setw(8) << "" << " "
			<< setw(7) << "" << " "
			<< setw(16) << "" << "\n" << setfill(' ');

		do
		{
			interest = (apr / (12 * 100)) * balance;
			cout << setw(4) << month << " " << setw(14) << balance << " ";
			cout << setw(12) << interest << " ";

			balance += interest;
			//balance -= monthlyPay;
			month++;
			if (balance < monthlyPay)
				monthlyPay = balance;
			balance -= monthlyPay;
			cout << setw(8) << monthlyPay << " " << setw(11) << balance << " " << "\n";
			

		}while(balance >0);
	cout<< endl << endl << "Loan is now PAID IN FULL." << endl;
	}
	else
		cout<< "You cannot pay this loan off." << endl;

	return 0;
}
Thank you so much. I got it now. I didn't mean to disregard your do-while loop, by the way. It was required for my course to only use a while loop.

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

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{

    double balance, interest, monthlyPay, apr; //balance of loan, monthly interest, desired payment, APR
    int month=1;    //month starts at 1 so it doesn't include a '0' month
    
    cout<< "How much would you like to borrow?" << endl;
    cin>> balance;                                            //user inputs desired loan
    cout<< "What is the annual percentage rate?" << endl;
    cin>> apr;                                                //user inputs apr
    cout<< "How much would you like to pay monthly?" << endl;
    cin>> monthlyPay;                                         //user inputs desired monthly payment
    cout<< endl;
    
    interest = (apr / (12 * 100)) * balance;                  //calculates interest for each month
    
    
    
    //if it's possible for user to pay off the loan
    
    if (monthlyPay > interest) {
        
        cout << setprecision(2) << fixed << showpoint;
        
        //prints catagories
        
        cout<< "Month" << "    " << "Loan Balance Start" << "    " << "Interest" << "    "
        << "Payment" << "    " << "Loan Balance End" << endl;
        
        
        //prints dashes under catagories
        
        cout << setfill('-')
        << setw(5)  << "" << "    "
        << setw(18) << "" << "    "
        << setw(8)  << "" << "    "
        << setw(7)  << "" << "    "
        << setw(16) << "" << "\n" << setfill(' ');
        
        
        while (balance > 0)                     // as long as user owes money
        {
            
            interest = (apr / (12 * 100)) * balance;        //calculates interest for each month
            cout<<right;
            cout<< setw(3) << month << setw(3);
            cout<< setw(18)<< balance << "       ";
            cout<< setw(8) << interest << "     ";
          
            if (balance < monthlyPay)           //If the remaining balance is less than monthly
               monthlyPay = balance + interest; // payment, the monthly payment is equal to remaining $$
            
            
            
            
            //asigns balance to equal balance + interest and then subtracts the monthly payment
            balance += interest;
            balance -= monthlyPay;
    
            
          
            
            cout<< setw(7) << monthlyPay ;
            cout<< setw(16) << balance <<"\n";
            
           
           //raises month # until the remaining balance = 0
            month++;
        }
        
        
    }
    
            // If loan is too much to pay off with desired monthly payment
    else{
        cout<< "You cannot pay this loan off." << endl;
    }
    return 0;
}

Topic archived. No new replies allowed.