Stuck on HW Assignment

Hey everyone, I'm new to C++, new to programming, and new to this forum. I have a HW assignment that I am stuck on. The program "kind of" works correctly, but it has a few issues. The big issue that I am worried about, is I do not always get the correct output.

Low input numbers work. Example:
Item price: 2000, Down payment: 1000, Interest rate: 18, Monthly payment: 50

High input numbers do not work. Example:
Item price: 10000, Down payment: 2000, Interest rate: 18, Monthly Payment 50

For the larger numbers it looks like there is a point where the output increases instead of decreasing and it becomes an infinite loop (I think). I have been working on this problem for over a week now, and I'm surely out of ideas on how to fix this. Any help or hints would be greatly appreciated.

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
// File Name: Project9.cpp
// Author:
// Email Address:
// Assignment: Chapter 2 - Project 9
/* Description: Interest Calculator - User inputs item price, down payment amount, the interest rate (compounded monthly) and the
monthly payment amount. The program outputs the total amount of months to pay off the line of credit, the interest charged each
month, the last months charged interest, and the amount of the last payment */
// Last Changed: 10-5-12

#include <iostream>
using namespace std;

int main()
{
double principal; // Principal or loan amount
double interestRate; // Interest rate
double monthlyPayment; // Monthly payment
double interest = principal * (interestRate / 12); // Calculates monthly interest
double finalPayment; // Last months payment
double finalInterest; // Last months interest
double downPayment; // Down payment
double price; // Initial cost of item
int months; // Months
char exit; // Rerun program or exit program


cout.setf(ios::fixed);	// Converts numbers into currency format
cout.setf(ios::showpoint); // Converts numbers into currency format
cout.precision(2); // Converts numbers into currency format


	do // Always do this
	{
		months = 1; // Months value assigned to 1
		cout << "Enter the cost of the item you wish to purchase: $"; // Prompts user to input the cost of the item
		cin >> price;
		cout << "Enter the down payment amount: $"; // Prompts user for the down payment amount
		cin >> downPayment;
		cout << "Enter the interest rate: "; // Prompts user for the interest rate
		cin >> interestRate;
		cout << "Enter the monthly payment: $"; // Prompts user to input the monthly payment amount
		cin >> monthlyPayment;
		principal = price - downPayment; // Calculates original principal or credit line amount


		while (principal <= 0) /* If the principal amount is less than or equal to zero, do this. Prevents user from making the
		down payment greater than the principal amount*/
		{
		cout << " " << endl; // Empty line for spacing
		cout << "The down payment can not be greater than the price of the item." << endl; /* Tells the user that the item price
		must be greater than the down payment */
		cout << "Enter the cost of the item you wish to purchase: $"; // Prompts the user to enter the cost of the item
		cin >> price;
		cout << "Enter the down payment amount: $"; // Prompts user to enter the down payment amount
		cin >> downPayment;
		principal = price - downPayment; // Calculates original principal amount once correct inputs are entered by user
		}

		while(interestRate < 0) /* If user inputs an interest rate value below 0, the user will be asked to enter the interest
		rate again. This prevents the user from having the program calculate a negative interest rate */
		{
		cout << " " << endl; // Empty line for spacing
		cout << "The interest rate must be greater than zero." << endl; 
		cout << "Enter the interest rate: ";
		cin >> interestRate;
		}

		while(monthlyPayment <= 0) /* If the user inputs a number less than or equal to zero for the monthly payment, do this.
		This will prevent user from creating an infinite loop by entering 0 as the value for the monthly payment */
		{
		cout << " " << endl;
		cout << "Monthly payment must be greater than zero." << endl;
		cout << "Enter the monthly payment: $";
		cin >> monthlyPayment;
		}

		if(interestRate >= 1) // If the user inputs an interest rate greater than 1(not in decimal form) do this
		{ 
		interestRate = interestRate / 100; // Converts interest from percentage form to decimal form
		}

		if(monthlyPayment >= principal) /* If the montly payment entered by user is equal to or larger than the principal amount
		entered by user, do this. If the principal amount is lower than the monthly payment, this gives the correct output */
		{
		finalInterest = principal * (interestRate /12); // Calculates the final months interest
		cout << " " << endl; // Empty line for spacing
		cout << "MONTHS" << "\t" << "INTEREST" << "\t" << "PRINCIPAL" << endl; // Column Headers
		cout << "------" << "\t" << "--------" << "\t" << "---------" << endl; // Column dividers
		cout << months << "\t $" << finalInterest << "\t" "\t $" << principal << endl; /* Output: Count of months, monthly
		interest and monthly principal. \t is used to tab columns for spacing */ 
		cout << " " << endl; // Empty line used for spacing
		cout << "Number of payments: " << months << endl; /* Output of the total number of payments or
		the total number of months to pay off line of credit */
		cout << "Last months interest: $" << finalInterest<< endl; // Output of last months interest
		finalPayment = principal = principal * (1 + interestRate / 12); // Calculates the amount of the final payment
		cout << "Last payment: $" << finalPayment << endl <<endl; // Ouputs final payment amount
		cout << "Press Y to run again or any other key to exit: "; // Prompts user to rerun or exit the program
		cin >> exit;
		system("CLS"); // Clears screen
		}

		else // If the "if statement" above is false do this
		{
		interest = principal * (interestRate / 12); // Calculates interest
		principal = principal * (1 + interestRate / 12) - monthlyPayment; // Calculates principal
		cout << " " << endl; // An empty line for spacing
		cout << "MONTHS" << "\t" << "INTEREST" << "\t" << "PRINCIPAL" << endl; /* Output of column headers:
		MONTHS, INTEREST, and PRINCIPAl */
		cout << "------" << "\t" << "--------" << "\t" << "---------" << endl; // Dashes to separate column header from output below
		cout << months << "\t $" << interest << "\t" "\t $" << principal << endl; /* Output: Count of months, monthly interest, and
		monthly principal */

		while(principal > monthlyPayment) /* As long as the principal is greater than the monthly payment, do this. This prevents the
		principal amount from becomming a negative number */
		{
		interest = principal * (interestRate / 12); // Calculates interest
		principal = principal * (1 + interestRate / 12) - monthlyPayment; // Calculates principal
		months = months++; // Counter for months
		cout << months << "\t $" << interest << "\t" "\t $" << principal << endl; /* Output: Count of months, monthly interest, and
		monthly principal */
		}

		cout << " " << endl; // Empty line for spacing
		cout << "Number of payments: " << months+1 << endl; /* Adds 1 to months to give the total amount of months it took to pay
		off debt*/
		finalInterest = principal * (interestRate /12); // Calculates last months interest
		cout << "Last months interest: $" << finalInterest<< endl; // Output of last months interest
		finalPayment = principal * (1 + interestRate /12); // Calculates final payment
		cout << "Last payment: $" << finalPayment << endl <<endl; // Output of final payment
		cout << "Press Y to run again or any other character to exit: "; // Prompts user to run program again or exit
		cin >> exit;
		system("CLS"); // Clears screen
		}
	}
while(exit == 'y' || exit == 'Y'); /* If the user inputs Y or y for exit, the program runs again. Any other character ends the
program */
return 0;
}
Last edited on
For the larger numbers it looks like there is a point where the output increases instead of decreasing


Yep. That's the point at which you supply a monthly payment that doesn't cover the monthly interest charged.
Thank you for the reply. I was killing my brain trying to figure that out! Now I have to find a solution, which is not easy to do for a noob lol
Topic archived. No new replies allowed.