Here is the mortgage calculation in C++ but I don't know why the table dosn't print out when I make run.
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;
bool exit_p=false;
unsigned short select_opt1();
void cont();
void second_opt1();
void first_opt1();
void print_table(unsigned short mm,unsigned short mn,double p,double l,double c,unsigned short n);
unsigned short int_length(double d);
int main(int argc, char *argv[])
{
/*double d;
cin>>d;
cout<<int_length(d);*/
cout<<"\t\tWelcome to CSC309 Mortgage Calculator System!!\n";
do{
unsigned short int opt1=select_opt1();
switch (opt1){
case 1: first_opt1();
cont();
break;
case 2: second_opt1();
cont();
break;
case 3: exit_p=true;
}
}while(!exit_p);
//system("PAUSE");
return EXIT_SUCCESS;
}
void first_opt1(){
double p,l,c;
unsigned short int n;
bool check_p=true;
char p_report;
cout<<"\tPlease enter the Loan amount in $ : ";
cin>>l;
cout<<"\tPlease enter the interest rate (e.g 4.5 for 4.5%) : ";
cin>>c;
cout<<"\tPlease enter the terms in # of months : ";
cin>>n;
c/=1200;
p= l*((c*pow(1+c,n))/(pow(1+c,n)-1));
cout<<fixed<<setprecision(2);
cout<<"\n\tYour monthly paymnent is $"<<p<<endl;
cout<<"\n\tYour total interest will be $"<<(p*n)-l<<endl;
cout<<"\n\n";
do{
cout<<"\tDo you want to print the monthly report ? (Y/N) ";
cin>>p_report;
}while(!(p_report=='Y'||p_report=='N'));
check_p=(p_report=='N')? false:true;
while(check_p){
char print_more;
unsigned short int opt2,month_m,month_n;
cout<<"\n\tOptions :\n";
cout<<"\t1: Print the entire payment history.\n";
cout<<"\t2: Print the history of the first n payments.\n";
cout<<"\t3: Print the history of the last n payments.\n";
cout<<"\t4: Print the history between m-th and n-th payments.\n\n";
do{
cout<<"\tYour choice is: (1 - 4) ";
cin>>opt2;
}while(!(opt2==1||opt2==2||opt2==3||opt2==4));
//cout<<"opt2 is "<<opt2<<endl;
month_m=1;
month_n=n;
switch (opt2){
case 1:break;
case 2:do{
cout<<"\tPlease enter the number of payments you want to print: ";
cin>>month_n;
}while(!(month_n>0&&month_n<n+1));
break;
case 3:do{
cout<<"\tPlease enter the number of last payments you want to print: ";
cin>>month_m;
month_m=n+1-month_m;
}while(!(month_m>0&&month_m<n+1));
break;
case 4:do{
do{
cout<<"\tPlease enter the starting number of payments you want to print: ";
cin>>month_m;
}while(!(month_m>0&&month_m<n+1));
do{
cout<<"\tPlease enter the ending number of payments you want to print: ";
cin>>month_n;
}while(!(month_n>0&&month_n<n+1));
if(month_n<month_m){
cout<<"\n\tThe starting #payment shouldn't be more than the ending #payment!"<<endl;
cout<<"\tPlease try again!!\n\n";
}
}while(month_n<month_m);
break;
}
cout<<endl;
print_table(month_m,month_n,p,l,c,n);
do{
cout<<"\tDo you want to try other option? (Y/N) ";
cin>>print_more;
}while(!(print_more=='Y'||print_more=='N'));
check_p=(print_more=='Y')? true:false;
}
cout<<endl;
}
void print_table(unsigned short mm,unsigned short mn,double p,double l,double c,unsigned short n){
string format[4]={" | "," |","| ","|"},
head_t[8]={" "," No.","Payment","Interest","Principal","Balance ","Total Paid","Total Int"};
unsigned short int i,j,value_t[3][8]={0,0,0,0,0,0,0,0,
0,4,7,8,9,8,10,9,
0,int_length(n),int_length(p)+4,int_length(p)+4,int_length(p)+4,int_length(l)+4,int_length(p*n)+4,int_length(p*n-l)+4};
for(i=1;i<8;i++){
if(value_t[1][i]+2<value_t[2][i]){
value_t[0][i-1]++;
}
for(j=0;j<(value_t[2][i]-value_t[1][i])/2;j++){
head_t[i]=" "+head_t[i];
}
if(value_t[1][i]<value_t[2][i]){
value_t[0][i]+=2;
value_t[1][i]=value_t[2][i];
}
}
for(i=1;i<8;i++){
if(i==7){
value_t[1][0]=value_t[1][0]+value_t[1][i];
value_t[2][0]=value_t[2][0]+value_t[1][i];
}else{
value_t[1][0]=value_t[1][0]+value_t[1][i]+format[value_t[0][i]].length();
value_t[2][0]=value_t[2][0]+value_t[1][i]+1;
}
}
/*cout<<value_t[1][0]<<", "<<value_t[2][0]<<endl;
//to observe array value_t
for(i=0;i<2;i++){
for(j=1;j<8;j++){
cout<<value_t[i][j]<<", ";
}
cout<<endl;
}
for(i=1;i<8;i++){
if(i==7){
cout<<setw(value_t[1][i])<<left<<head_t[i]<<endl;
}else{
cout<<setw(value_t[1][i])<<left<<head_t[i]<<format[value_t[0][i]];
}
}*/
if(value_t[1][0]>80){
for(i=1;i<8;i++){
if(value_t[0][i]<3){value_t[0][i]=3;}
}
if(value_t[2][0]>80){
cout<<"!!Caution!!\nThis table might not print correctly in column because of too many digits!!\n";
}
}
//to observe array value_t
/*for(i=0;i<2;i++){
for(j=1;j<8;j++){
cout<<value_t[i][j]<<", ";
}
cout<<endl;
}*/
//cout<<"mn="<<mm<<" mn="<<mn<<endl;
for(i=1;i<8;i++){
if(i==7){
cout<<setw(value_t[1][i])<<left<<head_t[i]<<endl;
}else{
cout<<setw(value_t[1][i])<<left<<head_t[i]<<format[value_t[0][i]];
}
}
double table[6]={p,0,0,l,0,0};
for(i=1;i<n+1;i++){
table[1]=table[3]*c;
table[2]=table[0]-table[1];
table[3]-=table[2];
table[4]+=table[0];
table[5]+=table[1];
if(!(i<mm||i>mn)){
cout<<setw(value_t[1][1])<<right<<i<<format[value_t[0][1]];
for(j=0;j<6;j++){
if(j==5){
cout<<"$"<<setw(value_t[1][j+2]-1)<<left<<table[j]<<endl;
}else{
cout<<"$"<<setw(value_t[1][j+2]-1)<<left<<table[j]<<format[value_t[0][j+2]];
}
}
}
}
cout<<endl;
}
unsigned short int_length(double d){
unsigned short int x;
for(x=0;d>=1;x++){d/=10;}
return x;
}
void second_opt1(){
double l,p,c,n;
cout<<"\tPlease enter the monthly payment you feel comfortable with : $";
cin>>p;
cout<<"\tPlease enter the interest rate (e.g 4.5 for 4.5%) : ";
cin>>c;
cout<<"\tPlease enter the terms in # of months : ";
cin>>n;
c/=1200;
l= p*((pow(1+c,n)-1)/(pow(1+c,n)*c));
cout<<"\n\tThe maximum Loan will be $"<<l<<endl;
cout<<endl;
}
void cont(){
char x;
bool check_x=false;
do{
cout<<"\tDo you want to continue? (Y/N) ";
cin>>x;
check_x=(x=='Y'||x=='N') ? true :false;
}while(!check_x);
exit_p= (x=='N')? true: false;
cout<<endl;
}
unsigned short select_opt1(){
unsigned short int x;
bool check_x=false;
cout<<"\tOptions:\n\n";
cout<<"\t1: Determine the monthly payment based on\n";
cout<<"\t\tLoan amount, interest rate and terms.\n\n";
cout<<"\t2: Determine the maximum Loan amount based on\n";
cout<<"\t\tmonthly payment, interest rate and terms.\n\n";
cout<<"\t3: Quit\n\n";
do{
cout<<"\t\tYour choice is: ";
cin>>x;
check_x= (x==1||x==2||x==3) ? true :false;
if(!check_x){
cout<<"\n\t\tPlease select option 1, 2 or 3\n";
}
}while(!check_x);
return x;
}