### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138`` ``````// 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 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 <> 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 <> 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.