I have written a piece of code in cpp which does some calculations after taking multiple inputs from a user.
Below is my working code:
#include<iostream>
#include<string>
#include<math.h>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int a ;
cout<<"Enter a number of entries to calculate: ";
cin>>a;
for(int j=1;j<=a;j )
{
string name;
cout<<"Enter the name of the borrower: ";
cin>>name;
float mortage_blanace;
cout<<"Enter the mortage balance: ";
cin>>mortage_blanace;
float interest_rate;
cout<<"Enter the annual interest rate: ";
cin>>interest_rate;
float current_monthly_payment;
cout<<"Enter the current monthly payment: ";
cin>>current_monthly_payment;
float extra_monthly_payment;
cout<<"Enter the extra monthly payment: ";
cin>>extra_monthly_payment;
cout<<"\n";
float new_payment = current_monthly_payment extra_monthly_payment;
float i = (interest_rate/100)/12;
int current_duration_in_months = ceil((log((current_monthly_payment/i)/((current_monthly_payment/i)-mortage_blanace)))/(log(1 i)));
int new_duration_in_months = ceil((log((new_payment/i)/((new_payment/i)-mortage_blanace)))/(log(1 i)));
float current_interest = (current_monthly_payment*current_duration_in_months)-mortage_blanace;
float new_interest = (new_payment*new_duration_in_months)-mortage_blanace;
int current_duration_years = current_duration_in_months/12;
int current_duration_months = current_duration_in_months % 12;
int new_duration_years = new_duration_in_months/12;
int new_duration_months = new_duration_in_months%12;
float savings = current_interest-new_interest;
string fees;
if(new_duration_in_months<=current_duration_in_months/2)
{
fees="Extra fees";
}
else
{
fees="No Fee";
}
cout<<"Name |MortageBalance |Interest Rate | Current | New |Savings |Fees ";
cout<<"\n";
cout<<" Payment Duration Interest | Payment Duration Interest ";
cout<<"---------------------------------------------------------------------------------------------------------------------------------";
std::cout << std::fixed;
cout<<name << " " << "$" << setprecision(2) << mortage_blanace << " " << interest_rate << "%" << " " << "$" << current_monthly_payment << " " << current_duration_years << "yrs" << " " << current_duration_months << "mo" << " " << "$" << current_interest << " " << "$" << new_payment << " " << new_duration_years << "yrs" << " " << new_duration_months << " mo" << " " << "$" << new_interest << " " << "$" << savings << " " << fees;
cout<<"\n";
}
return 0;
}
My output:
But I want output in below format where I take all the inputs one at a time and then print outputs in one go:
Any help is appreciated.
Thanks.
CodePudding user response:
One possible way would be to use struct
as shown below:
#include<iostream>
#include<string>
#include<math.h>
#include <iomanip>
#include <cmath>
#include <vector>
struct Details
{
std::string name,fees;
float mortage_blanace =0,interest_rate =0,current_monthly_payment=0,extra_monthly_payment=0,new_payment=0;
float current_interest=0,new_interest=0,savings=0;
int current_duration_in_months=0,new_duration_in_months=0,current_duration_years=0,current_duration_months=0,new_duration_years=0,new_duration_months=0;
};
using namespace std;
int main()
{
int a ;
cout<<"Enter a number of entries to calculate: ";
cin>>a;
float i;
//create vector of size a
std::vector<Details> myVec(a);
//iterate through the vector and fill in details
for(Details &person: myVec)
{
cout<<"Enter the name of the borrower: ";
cin>>person.name;
cout<<"Enter the mortage balance: ";
cin>>person.mortage_blanace;
cout<<"Enter the annual interest rate: ";
cin>>person.interest_rate;
cout<<"Enter the current monthly payment: ";
cin>>person.current_monthly_payment;
cout<<"Enter the extra monthly payment: ";
cin>>person.extra_monthly_payment;
cout<<"\n";
person.new_payment = person.current_monthly_payment person.extra_monthly_payment;
i = (person.interest_rate/100)/12;
person.current_duration_in_months = ceil((log((person.current_monthly_payment/i)/((person.current_monthly_payment/i)-person.mortage_blanace)))/(log(1 i)));
person.new_duration_in_months = ceil((log((person.new_payment/i)/((person.new_payment/i)-person.mortage_blanace)))/(log(1 i)));
person.current_interest = (person.current_monthly_payment*person.current_duration_in_months)-person.mortage_blanace;
person.new_interest = (person.new_payment*person.new_duration_in_months)-person.mortage_blanace;
person.current_duration_years = person.current_duration_in_months/12;
person.current_duration_months = person.current_duration_in_months % 12;
person.new_duration_years = person.new_duration_in_months/12;
person.new_duration_months = person.new_duration_in_months%12;
person.savings = person.current_interest-person.new_interest;
if(person.new_duration_in_months<=person.current_duration_in_months/2)
{
person.fees="Extra fees";
}
else
{
person.fees="No Fee";
}
}
cout<<"Name |MortageBalance |Interest Rate | Current | New |Savings |Fees ";
cout<<"\n";
cout<<" Payment Duration Interest | Payment Duration Interest ";
cout<<"---------------------------------------------------------------------------------------------------------------------------------";
//print all the details
for(const Details &person: myVec)
{
std::cout << std::fixed;
cout<<person.name << " " << "$" << setprecision(2) << person.mortage_blanace << " " << person.interest_rate << "%" << " " << "$" << person.current_monthly_payment << " " << person.current_duration_years << "yrs" << " " << person.current_duration_months << "mo" << " " << "$" << person.current_interest << " " << "$" << person.new_payment << " " << person.new_duration_years << "yrs" << " " << person.new_duration_months << " mo" << " " << "$" << person.new_interest << " " << "$" << person.savings << " " << person.fees;
cout<<"\n";
}
return 0;
}