Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I had to write a loan calculator in C++, I need to add the ability to save data

ID: 3851760 • Letter: I

Question

I had to write a loan calculator in C++, I need to add the ability to save data to a disk in one or more files. The menu(s) should give the user the option to save or retrieve data. This is my code so far.

#include <iostream>

#include <iomanip>

#include <cmath>

#include <fstream>

using namespace std;

int main()

{

float Principal;

float Interest;

int Months;

float AccInterest;

float monPayment;

float appPrincipal;

float endBalance;

cout << "Welcome to the loan calculator!" << endl;

cout << "Please enter the amount of your loan: ";

cin >> Principal;

cout << "Please enter the interest rate: ";

cin >> Interest;

cout << "please enter the amount of months the loan is for: ";

cin >> Months;

for (int x = 0; x < Months; x++)

{

AccInterest = (Principal) * (Interest / 100) / 12;

monPayment = (Principal * (Interest / 100) / 12) * (pow(1 + (Interest / 100) / 12, Months) / (pow(1 + (Interest / 100) / 12, Months) - 1));

appPrincipal = monPayment - AccInterest;

endBalance = Principal - monPayment + AccInterest;

cout << "Month " << x + 1 << " of " << Months << endl;

cout << "Starting Principal: $" << Principal << endl;

cout << "Payment is: $" << monPayment << endl;

cout << "Accured interest is: $" << AccInterest << endl;

cout << "Applied to Principal is: $" << appPrincipal << endl;

cout << "End balance: $" << endBalance << endl;

Principal -= appPrincipal;

cin.get();

}

return 0;

}

Explanation / Answer

#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>

using namespace std;

int main()
{


float Principal;
float Interest;
int Months;
int Option;
float AccInterest;
float monPayment;
float appPrincipal;
float endBalance;

cout << "Welcome to the loan calculator!" << endl;

cout << "Do you want to 1. Save Data, 2. Retrieve Data ? Please enter an option.";
cin >> Option;

if(Option==1)
{
cout << "Please enter the amount of your loan: ";
cin >> Principal;
cout << "Please enter the interest rate: ";
cin >> Interest;
cout << "please enter the amount of months the loan is for: ";
cin >> Months;

ofstream myfile;
myfile.open ("example.txt");

for (int x = 0; x < Months; x++)
{
AccInterest = (Principal) * (Interest / 100) / 12;
monPayment = (Principal * (Interest / 100) / 12) * (pow(1 + (Interest / 100) / 12, Months) / (pow(1 + (Interest / 100) / 12, Months) - 1));
appPrincipal = monPayment - AccInterest;
endBalance = Principal - monPayment + AccInterest;

cout << "Month " << x + 1 << " of " << Months << endl;
cout << "Starting Principal: $" << Principal << endl;
cout << "Payment is: $" << monPayment << endl;
cout << "Accured interest is: $" << AccInterest << endl;
cout << "Applied to Principal is: $" << appPrincipal << endl;
cout << "End balance: $" << endBalance << endl;

myfile << "Month " << x + 1 << " of " << Months << endl;
myfile << "Starting Principal: $" << Principal << endl;
myfile << "Payment is: $" << monPayment << endl;
myfile << "Accured interest is: $" << AccInterest << endl;
myfile << "Applied to Principal is: $" << appPrincipal << endl;
myfile << "End balance: $" << endBalance << endl;
myfile.close();

Principal -= appPrincipal;
cin.get();
}
}

else
{
ifstream myfile;
myfile.open ("example.txt");
while ( getline (myfile,line) )
{
//value retrieved is saved in "line" variable. Process it according to your file format
}
}

return 0;
}