Skip to content

Instantly share code, notes, and snippets.

@Duta

Duta/rewrite Secret

Created March 24, 2013 22:45
Show Gist options
  • Save Duta/ee2ae39d32cc75ec9d4f to your computer and use it in GitHub Desktop.
Save Duta/ee2ae39d32cc75ec9d4f to your computer and use it in GitHub Desktop.
#include <iostream>
#include <iomanip>
#include <cstdlib>
// Prototypes
void printHead();
void calcPieces();
float loanAmt = 0; // Total loan amount
double intRate = 0; // Interest rate as xx.xxx%
float pAmt = 0; // Payment amount
int pNum = 1; // Payment number counter
float tPrincipal; // Total principal
float tInterest; // Total interest
double principal; // Current principal
double interest; // Current interest
float balance; // Present value of loan
int main() {
// Collect information
while (loanAmt <= 0) {
std::cout << "Please input loan amount: ";
std::cin >> loanAmt;
if (loanAmt <= 0) {
std::cout << "Invalid amount entered, please enter another one: ";
}
}
while (intRate <= 0) {
std::cout << "Please input interest rate as xx.xxx%: ";
std::cin >> intRate;
if (intRate <= 0) {
std::cout << "Invalid amount entered, please enter another one: ";
}
intRate /= 100;
}
while (pAmt <= 0) {
std::cout << "Please input monthly payment amount: ";
std::cin >> pAmt;
if (pAmt <= 0) {
std::cout << "Invalid amount entered, please enter another one: ";
}
}
balance = loanAmt;
interest = 0.0;
// This needs to be the error check
while (balance > 0.0) {
printHead();
while(pAmt <= balance) {
calcPieces();
if (pNum % 20 == 1) {
system("pause");
printHead();
}
}
calcPieces();
balance = 0;
system("pause");
}
return 0;
}
void printHead() {
std::cout << "PAMT # PAMT AMT PRINCIPAL INTEREST BALANCE TTL INT TTL PRIN" << std::endl;
std::cout << "==============================================================================" << std::endl;
}
void calcPieces() {
// Calc interest and principal, add to the totals, also balance
interest = balance * (intRate/12);
principal = pAmt - interest;
tInterest += interest;
tPrincipal += principal;
balance -= pAmt - interest;
std::cout << setprecision(2) << std::fixed;
std::cout << std::right;
std::cout << std::setw(5);
std::cout << pNum;
std::cout << std::right;
std::cout << setw(9);
std::cout << pAmt;
std::cout << std::right;
std::cout << setw(14);
std::cout << principal;
std::cout << std::right;
std::cout << setw(14);
std::cout << interest;
std::cout << std::right;
std::cout << setw(14);
std::cout << balance;
std::cout << std::right;
std::cout << setw(10);
std::cout << tInterest;
std::cout << std::right;
std::cout << setw(14);
std::cout << tPrincipal << std::endl;
pNum++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment