// Zeinalipour-Yazti Demetrios // csyiazti // cs10xx - // Lab Sec#24 // lab8 /* A program that will compute the monthly payment of a fully amortized loan. */ #include #include #include #include #include using namespace std; const int COLSPAN = 30; /* defines the width of the first column (on output) */ const int COLSPANB = 14; /* defines the width of all subsequent columns (on output) */ /* returns true if the given character is an integer */ bool isInt(char c) { return (c>=48 && c<=57); } /* returns true if the given string is a real number */ bool isReal(string instring) { int size = instring.size(); int dotcount = 0; char c; for (int i=0; i122)) ) { return false; } } return true; } /* This function asks the user to input the value: type[] It then returns this value as a string. */ string getName(string type) { /* defining local scope variables*/ string stringvalue = ""; while (true) { cout << "\nPlease enter '" << type << "' : "; cin >> stringvalue; if (isName(stringvalue)) { break; } else { cout << "#Error - The name has to be a valid name!\n"; } } return stringvalue; } /* This function asks the user to input the value: type[] repetively until a * correct (>0) double value is given. It then returns this value */ double getRealNumber(string type) { /* defining local scope variables - although we are seeking for a double value we will read the users entry as a 'string' (to avoid any exceptions) and if the value is a 'double' value we will proceed. */ string stringvalue; double value; /* define functions & procedures used */ bool isReal(string instring); while (true) { cout << "\nEnter the " << type ; cin >> stringvalue; if (isReal(stringvalue)) { /* Converting the string to char array[] (with c_str()) and then * convert to floating value variable with (stdlib::atof()) */ value = atof(stringvalue.c_str()); if (value>0) { break; } } cout << "#Error - The value entered MUST be positive double value!\n"; } return value; } /* this function computes the Payment according to the formula. We have split it into three * segments for readability. */ double computePayment(double principal, double interest, double length) { double mpr = interest / (100 * 12); double sub_calc = 1 / ( pow(1+mpr, length*12) ); return (mpr * principal / (1 - sub_calc)); } /* This procedure outputs a row (with the dollar sign) for the given values */ void printMoneyRow(string label, double value1, double value2) { int shift_val = COLSPAN - label.size(); //Sets the format flags. 2 decimal points precision. cout << setprecision(2) << setiosflags(ios::fixed | ios::showpoint); /* Print first column - add spaces required to align 2nd column */ cout << label << setw(shift_val); cout << '$'; // Set extra format flag. Sets a value to be used as the field width for the next insertion operation. cout << setw(COLSPANB) << value1 << ' ' << " $" << setw(COLSPANB) << value2 << ' ' << endl; //Reset the format flags. cout << resetiosflags (ios::fixed | ios::showpoint); cout << setprecision(0); } /* This procedure outputs a row (with the percentage sign) for the given values */ void printPercentageRow(string label, double value1, double value2) { int shift_val = COLSPAN - label.size(); /* Print first column - add spaces required to align 2nd column */ cout << label << setw(shift_val); cout << ' '; // Set extra format flag. Sets a value to be used as the field width for the next insertion operation. cout << setw(COLSPANB-1) << value1 << '%' << " " << setw(COLSPANB) << value2 << '%' << endl; } /* This procedure outputs a row (without any sign) for the given values */ void printNormalRow(string label, double value1, double value2) { int shift_val = COLSPAN - label.size(); /* Print first column - add spaces required to align 2nd column */ cout << label << setw(shift_val); cout << ' '; // Set extra format flag. Sets a value to be used as the field width for the next insertion operation. cout << setw(COLSPANB) << value1 << ' ' << " " << setw(COLSPANB) << value2 << ' ' << endl; } /* Output the data & results */ void printOutput(string firstname, string lastname, double principal1, double principal2, double interest1, double interest2, double length1, double length2, double result1, double result2) { /* define functions & procedures used */ void printNormalRow(string label, double value1, double value2); void printPercentageRow(string label, double value1, double value2); void printMoneyRow(string label, double value1, double value2); /* print-out intro */ cout << "\n\n" << firstname << " " << lastname << " here is your comparison data \n\n"; /* print-out data */ printMoneyRow("Principal", principal1, principal2); printPercentageRow("Annual interest rate", interest1, interest2); printNormalRow("Years", length1, length2); cout << endl; /* print-out results */ printMoneyRow("Monthly payment", result1, result2); } void main() { /* define variables */ string firstname; /* The firstame of the user */ string lastname; /* The lastname of the user */ double principal1, principal2; /* The amounts borrowed */ double interest1, interest2; /* The yearly interest */ double length1, length2; /* The length of loan */ double result1, result2; /* The payment */ /* define functions & procedures used */ string getName(string type); double getRealNumber(string type); void printOutput(string firstname, string lastname, double principal1, double principal2, double interest1, double interest2, double length1, double length2amount, double result1, double result2); /* print intro */ cout << "Calculator of the monthly payment of a fully amortized loan.\n"; cout << "by Demetris Zeinalipour : cs10 #24.\n\n"; /* get Values from user */ firstname = getName("First Name"); lastname = getName("Last Name"); principal1 = getRealNumber("The #1 Principal Amount borrowed : $"); principal2 = getRealNumber("The #2 Principal Amount borrowed : $"); interest1 = getRealNumber("The #1 Annual interest rate : %"); interest2 = getRealNumber("The #2 Annual interest rate : %"); length1 = getRealNumber("The #1 Length of the loan (in years) : "); length2 = getRealNumber("The #2 Length of the loan (in years) : "); /* making the calculation of the payment */ result1 = computePayment(principal1, interest1, length1); result2 = computePayment(principal2, interest2, length2); //prints the output of the program printOutput(firstname, lastname, principal1, principal2, interest1, interest2, length1, length2, result1, result2); char empty; cout << "Press 'q' to exit!"; cin >> empty; }