c++ - How can I make this function run more than once? -
let me first note absolute beginner c++. please, go easy on me.
i've been writing code below part of assignment programming methodology course summer. it's meant bank program takes user input calculate number of months (n), interest rate (i) , monthly payment user's loan. then, program supposed take payment amount user , calculate new balance. here, supposed print amortization report delineates beginning balance, interest paid, principle paid, , ending balance. of works well, next part having trouble with. program supposed able take multiple payments , add additional lines amortization report, , cannot figure out how run make payment function second time additional payments. help, please!!
also, know parameters set member functions needless, replaced user input, they're required instructor in assignment instructions.
thanks again advice can give!
#ifndef loan_data_h #define loan_data_h class loan_data { private: double bal; double n; double i; double a; double p; public: loan_data(double p, double n, double i); void makepayment(double pay); void printamortizationschedule(); }; #endif /* loan_data_h */ #include <cstdlib> #include <cmath> #include <iostream> #include "loan_data.h" using namespace std; loan_data::loan_data(double p, double n, double i) { cout << "enter loan amount: $"; cin >> this->p; cout << "enter loan length: "; cin >> this->n; cout << "enter credit score: "; cin >> this->i; this->i = this->i / 100; this->i = this->i / 12; this->n = this->n * 12; bal = this->p; = (this->p * ((this->i * pow(1 + this->i, n)) / (pow(1 + this->i, n) - 1))); cout << "a is: " << << endl; cout << "bal is: " << bal << endl; cout << "i is: " << this->i << endl; } void loan_data::makepayment(double pay) { cout << "i is: " << << endl; cout << "bal is: " << bal << endl; cout << "enter payment first payment amount: $"; cin >> pay; cout << "bal is: " << bal << endl; bal = ((i + 1) * bal) - pay; = pay; } void loan_data::printamortizationschedule() { double ip = (i * bal); double pp = (a - (i*bal)); double endingbalance = ((1 + i)*bal - a); double payment2 = (i + 1)*bal; cout << "beginning bal." << "\t""\t" << cout << "interest paid" << "\t""\t" << cout << "principle paid" << "\t""\t" << cout << "ending bal." << "\t""\t" << endl; if ((i + 1)*bal > a) { cout << p << "\t""\t""\t""\t" << ip << "\t""\t""\t""\t" << pp << "\t\t""\t""\t" << endingbalance << endl; endingbalance = bal; } else if (bal < a) { cout << bal << "\t""\t""\t""\t" << ip << "\t""\t""\t""\t" << (payment2 - (i*bal)) << "\t\t""\t""\t" << ((1 + i)*bal - payment2) << endl; bal = ((1 + i)*bal - payment2); } else if (bal == 0) { cout << "0" << "\t""\t""\t""\t""\t" << "0" << "\t""\t""\t""\t""\t" << "0" << "\t\t""\t""\t""\t" << "0" << endl; } } int main(int argc, char *argv[]) { double bal; double p; double n; double i; double pay; double a; loan_data loan1(p, n, i); loan1.makepayment(pay); loan1.printamortizationschedule(); return 0; }
modify main using do while loops --
int main(int argc, char *argv[]) { char ch='n'; { double bal; double p; double n; double i; double pay; double a; loan_data loan1(p, n, i); loan1.makepayment(pay); loan1.printamortizationschedule(); printf("do want continue "); ch=getchar(); }while(ch=='y'); return 0; }
do while loop repeats code enclosed while condition true i.e, if user enters y @ end of 1st time program continue else exit.
Comments
Post a Comment