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

Add the ability to save data to disk in one or more files. The menu(s) should gi

ID: 3707126 • Letter: A

Question

Add the ability to save data to disk in one or more files. The menu(s) should give the user the option to save or retrieve data.

Here is my code, it's in c++ and I need it to work in visual studio.

#include "stdafx.h"

#include<iostream>

#include<math.h>

using namespace std;

const int SIZE = 5;

int nMonths[SIZE];

float loanAmount[SIZE];

float totalPaid[SIZE];

float interestRate[SIZE];

float monthlyPayment[SIZE];

int id;

//process function

void process() {

float interestToAdd = 0.0f;

float remaining = loanAmount[id];

float toAdd = 0.0f;

cout << endl;

cout << "Month" << " (Interest Added)" << " (Amount Paid)" << " (Debt Remaining)" << endl;

nMonths[id] = 0;

totalPaid[id] = 0;

while (remaining > monthlyPayment[id]) {

interestToAdd = remaining * interestRate[id] / 12;

nMonths[id]++;

remaining += interestToAdd;

if (remaining > monthlyPayment[id]) {

toAdd = monthlyPayment[id];

}

else {

toAdd = remaining;

}

cout << nMonths[id] << " $" << interestToAdd << " $" << toAdd << " $" << remaining << endl;

totalPaid[id] += toAdd;

remaining -= toAdd;

}

}

//display the result function

void displayResult() {

cout << endl << "Statistics about loan. " << endl;

cout << "Initial loan amount: $" << loanAmount[id] << endl;

cout << "Total amount paid: $" << totalPaid[id] << endl;

cout << "Time to pay off loan: " << nMonths[id] << endl;

cout << "Overpay percentage: " << (totalPaid[id] / loanAmount[id]) << endl;

cin.ignore();

}

//choose letterA function

void chooseletterA() {

float Amount, rateOfIntrest, monthlyAmountToPay, totalAmount;

int lengthOfLoan;

cout << "What is the amount of the loan?:$";

cin >> Amount;

cin.ignore();

while (Amount<1) //validate

{

cout << "Amount should be greater than 0..What is the amount of the loan?:$";

cin >> Amount;

}

cout << "What is the interest rate on the loan?";

cin >> rateOfIntrest;

rateOfIntrest /= 100;

cin.ignore();

cout << "What is the length of loan?:$";

cin >> lengthOfLoan;

while (lengthOfLoan<1)

{

cout << "length of loan should be greater than 0..What is the length of loan?:$";

cin >> lengthOfLoan;

}

monthlyAmountToPay = (Amount*rateOfIntrest*pow((1 + rateOfIntrest), lengthOfLoan)) / (pow((1 + rateOfIntrest), lengthOfLoan - 1));

cout << "Your monthly loan payment amount = $" << monthlyAmountToPay << endl;

system("pause");

}

//chooseletterB function

void chooseletterB() {

float Amount, rateOfIntrest, monthlyAmountToPay, totalAmount;

int lengthOfLoan;

cout << "What is the monthly amount of the loan?:$";

cin >> monthlyAmountToPay;

cin.ignore();

cout << "What is the interest rate on the loan?";

cin >> rateOfIntrest;

rateOfIntrest /= 100;

cin.ignore();

cout << "What is the length of loan?:$";

cin >> lengthOfLoan;

cin.ignore();

Amount = monthlyAmountToPay * pow((1 + rateOfIntrest), lengthOfLoan - 1) / (rateOfIntrest*pow((1 + rateOfIntrest), lengthOfLoan));

cout << "Your total loan amount = $" << Amount << endl;

system("pause");

}

//chooseletterc function

void chooseletterC() {

float Amount, rateOfIntrest, monthlyAmountToPay, totalAmount;

int lengthOfLoan;

cout << "What is the amount of the loan?:$";

cin >> Amount;

cin.ignore();

cout << "What is the monthly amount of the loan?:$";

cin >> monthlyAmountToPay;

cin.ignore();

cout << "What is the length of loan?:$";

cin >> lengthOfLoan;

cin.ignore();

rateOfIntrest = ((monthlyAmountToPay *lengthOfLoan) - Amount) / lengthOfLoan;

cout << "Your monthly monthly rate of intrest = $" << rateOfIntrest / 12 << endl;

system("pause");

}

//chooseletter D function

void chooseletterD() {

float Amount, rateOfIntrest, monthlyAmountToPay, totalAmount;

int lengthOfLoan;

cout << "What is the amount of the loan?:$";

cin >> Amount;

cin.ignore();

cout << "What is the monthly amount of the loan?:$";

cin >> monthlyAmountToPay;

cin.ignore();

cout << "What is the interest rate on the loan?";

cin >> rateOfIntrest;

rateOfIntrest /= 100;

cin.ignore();

lengthOfLoan = (log(Amount) - log(monthlyAmountToPay)) / (log(1 + rateOfIntrest));

cout << "Your length of loan amount = $" << lengthOfLoan << endl;

system("pause");

}

//multiple choice function

void multipleChoice() {

cout << "Welcome to the multiple choice part of the program, please choose a letter: " << endl;

cout << "A: Monthly payment " << endl;

cout << "B: Loan amount " << endl;

cout << "C: Length of loan " << endl;

cout << "D: Interest rate " << endl;

char ch;

cin >> ch;

//validate the input

while (!(ch >= 'A' && ch <= 'D')) {

cout << "Invalid input..please choose a letter:(A - D): ";

cin >> ch;

}

switch (ch)

{

case 'A':

chooseletterA();

cout << "First question: Please solve for monthly payment. " << endl;

break;

case 'B':

chooseletterB();

cout << "Second question: Please solve for loan amount. " << endl;

break;

case 'C':

chooseletterC();

cout << "Third question: Please solve for length of loan. " << endl;

break;

case 'D':

chooseletterD();

cout << "Forth question: Please solve for interest rate. " << endl;

break;

}

}

int main() {

int ch;

for (int i = 0; i < SIZE; i++) {

cout << "What is the amount of the loan?:$";

cin >> loanAmount[i];

cin.ignore();

//validation amount should be greater than 0

while (loanAmount[i] < 0)

{

cout << "loan amount should be greater than 0..What is the amount of the loan?:$";

cin >> loanAmount[i];

}

cout << "What is the interest rate on the loan?:$";

cin >> interestRate[i];

interestRate[i] /= 100;

cin.ignore();

cout << "What is the desired monthly payment?:$";

cin >> monthlyPayment[i]; cin.ignore();

while (monthlyPayment[i] < 0) {

cout << " Monthly payment should be greater than 0..What is the desired monthly payment?:$";

cin >> monthlyPayment[i];

}

while (monthlyPayment[i] < loanAmount[i] * interestRate[i] / 12) {

cout << "Not a valid amount, Amount must be greater than $";

cout << (loanAmount[i] * interestRate[i] / 12) << endl;

cout << "What is the desired monthly payment amount? $";

cin >> monthlyPayment[i]; cin.ignore();

}

}

id = 0;

process();

do {

cout << endl << "************************MENU***************************" << endl;

cout << "1. Process Loan for a customer" << endl;

cout << "2. Display Result" << endl;

cout << "3. Multiple Choice" << endl;

cout << "4. Exit" << endl;

cout << "Enter your choice : ";

cin >> ch;

system("pause");

switch (ch) {

case 1:

cout << "What is the customer id you want process its data [1-5]: ";

cin >> id;

cin.ignore();

//validation amount should be greater than 0

while (id < 1)

{

cout << "The customer id should be whithin [1-5] included, please enter new value: ";

cin >> id;

}

id = id - 1;

process();

break;

case 2:

displayResult();

break;

case 3:

multipleChoice();

break;

//exit function

case 4:

cout << "Thank you";

return 0;

default:

cout << "Entered Wrong Keyword.... Re-enter Again";

break;

}

} while (ch != 4);

return 0;

}

Explanation / Answer

here is your modified program : -------------->>>>>>>>>>>

//#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
const int SIZE = 5;
int nMonths[SIZE];
float loanAmount[SIZE];
float totalPaid[SIZE];
float interestRate[SIZE];
float monthlyPayment[SIZE];
int id;
void save(){
string file;
ofstream ofs;
cout<<endl;
cout<<"Enter the file name to save customer data : ";
cin>>file;
ofs.open(file.c_str());
if(ofs.is_open()){
  for(int i = 0;i<SIZE;i++){
   ofs<<loanAmount[i]<<" "<<interestRate[i]<<" "<<monthlyPayment[i]<<endl;
  }
  ofs.close();
}
cout<<endl;
cout<<"Enter the file name to store process data of each customer : ";
cin>>file;
ofs.open(file.c_str());
if(ofs.is_open()){
  float interestToAdd = 0.0f;
  float remaining;
  float toAdd = 0.0f;
  for(int i = 0;i<SIZE;i++){
   interestToAdd = 0.0f;
   remaining = loanAmount[i];
   toAdd = 0.0f;
   ofs << endl;
   ofs << "Month" << " (Interest Added)" << " (Amount Paid)" << " (Debt Remaining)" << endl;
   nMonths[i] = 0;
   totalPaid[i] = 0;
   while (remaining > monthlyPayment[i]) {
   interestToAdd = remaining * interestRate[i] / 12;
   nMonths[i]++;
   remaining += interestToAdd;
   if (remaining > monthlyPayment[i]) {
   toAdd = monthlyPayment[i];
   }
   else {
   toAdd = remaining;
   }
   ofs << nMonths[i] << " $" << interestToAdd << " $" << toAdd << " $" << remaining << endl;
   totalPaid[i] += toAdd;
   remaining -= toAdd;
   }
  }
  
  ofs.close();
}
cout<<endl;
cout<<"Enter the name of file to store the statistic of each customer : ";
cin>>file;
ofs.open(file.c_str());
if(ofs.is_open()){
  for(int i = 0;i<SIZE;i++){
   ofs << endl << "Statistics about loan. " << endl;
   ofs << "Initial loan amount: $" << loanAmount[i] << endl;
   ofs << "Total amount paid: $" << totalPaid[i] << endl;
   ofs << "Time to pay off loan: " << nMonths[i] << endl;
   ofs << "Overpay percentage: " << (totalPaid[i] / loanAmount[i]) << endl;
  }
}
}

bool retrieve(){
ifstream ifs;
string file;
cout<<" Enter the file name to retreive customer data ";
cin>>file;
ifs.open(file.c_str());
if(ifs.is_open()){
  for(int i = 0;i<SIZE;i++){
   ifs>>loanAmount[i]>>interestRate[i]>>monthlyPayment[i];
   if(ifs.eof()){
    cout<<" Data Error in Reading ";
    return false;
   }
  }
  ifs.close();
  return true;
}

cout<<" File opening error !!! ";
return false;
}
//process function
void process() {
float interestToAdd = 0.0f;
float remaining = loanAmount[id];
float toAdd = 0.0f;
cout << endl;
cout << "Month" << " (Interest Added)" << " (Amount Paid)" << " (Debt Remaining)" << endl;
nMonths[id] = 0;
totalPaid[id] = 0;
while (remaining > monthlyPayment[id]) {
interestToAdd = remaining * interestRate[id] / 12;
nMonths[id]++;
remaining += interestToAdd;
if (remaining > monthlyPayment[id]) {
toAdd = monthlyPayment[id];
}
else {
toAdd = remaining;
}
cout << nMonths[id] << " $" << interestToAdd << " $" << toAdd << " $" << remaining << endl;
totalPaid[id] += toAdd;
remaining -= toAdd;
}
}
//display the result function
void displayResult() {
cout << endl << "Statistics about loan. " << endl;
cout << "Initial loan amount: $" << loanAmount[id] << endl;
cout << "Total amount paid: $" << totalPaid[id] << endl;
cout << "Time to pay off loan: " << nMonths[id] << endl;
cout << "Overpay percentage: " << (totalPaid[id] / loanAmount[id]) << endl;
cin.ignore();
}
//choose letterA function
void chooseletterA() {
float Amount, rateOfIntrest, monthlyAmountToPay, totalAmount;
int lengthOfLoan;
cout << "What is the amount of the loan?:$";
cin >> Amount;
cin.ignore();
while (Amount<1) //validate
{
cout << "Amount should be greater than 0..What is the amount of the loan?:$";
cin >> Amount;
}
cout << "What is the interest rate on the loan?";
cin >> rateOfIntrest;
rateOfIntrest /= 100;
cin.ignore();
cout << "What is the length of loan?:$";
cin >> lengthOfLoan;
while (lengthOfLoan<1)
{
cout << "length of loan should be greater than 0..What is the length of loan?:$";
cin >> lengthOfLoan;
}
monthlyAmountToPay = (Amount*rateOfIntrest*pow((1 + rateOfIntrest), lengthOfLoan)) / (pow((1 + rateOfIntrest), lengthOfLoan - 1));
cout << "Your monthly loan payment amount = $" << monthlyAmountToPay << endl;
system("pause");
}
//chooseletterB function
void chooseletterB() {
float Amount, rateOfIntrest, monthlyAmountToPay, totalAmount;
int lengthOfLoan;
cout << "What is the monthly amount of the loan?:$";
cin >> monthlyAmountToPay;
cin.ignore();
cout << "What is the interest rate on the loan?";
cin >> rateOfIntrest;
rateOfIntrest /= 100;
cin.ignore();
cout << "What is the length of loan?:$";
cin >> lengthOfLoan;
cin.ignore();
Amount = monthlyAmountToPay * pow((1 + rateOfIntrest), lengthOfLoan - 1) / (rateOfIntrest*pow((1 + rateOfIntrest), lengthOfLoan));
cout << "Your total loan amount = $" << Amount << endl;
system("pause");
}
//chooseletterc function
void chooseletterC() {
float Amount, rateOfIntrest, monthlyAmountToPay, totalAmount;
int lengthOfLoan;
cout << "What is the amount of the loan?:$";
cin >> Amount;
cin.ignore();
cout << "What is the monthly amount of the loan?:$";
cin >> monthlyAmountToPay;
cin.ignore();
cout << "What is the length of loan?:$";
cin >> lengthOfLoan;
cin.ignore();
rateOfIntrest = ((monthlyAmountToPay *lengthOfLoan) - Amount) / lengthOfLoan;
cout << "Your monthly monthly rate of intrest = $" << rateOfIntrest / 12 << endl;
system("pause");
}
//chooseletter D function
void chooseletterD() {
float Amount, rateOfIntrest, monthlyAmountToPay, totalAmount;
int lengthOfLoan;
cout << "What is the amount of the loan?:$";
cin >> Amount;
cin.ignore();
cout << "What is the monthly amount of the loan?:$";
cin >> monthlyAmountToPay;
cin.ignore();
cout << "What is the interest rate on the loan?";
cin >> rateOfIntrest;
rateOfIntrest /= 100;
cin.ignore();
lengthOfLoan = (log(Amount) - log(monthlyAmountToPay)) / (log(1 + rateOfIntrest));
cout << "Your length of loan amount = $" << lengthOfLoan << endl;
system("pause");
}
//multiple choice function
void multipleChoice() {
cout << "Welcome to the multiple choice part of the program, please choose a letter: " << endl;
cout << "A: Monthly payment " << endl;
cout << "B: Loan amount " << endl;
cout << "C: Length of loan " << endl;
cout << "D: Interest rate " << endl;
char ch;
cin >> ch;
//validate the input
while (!(ch >= 'A' && ch <= 'D')) {
cout << "Invalid input..please choose a letter:(A - D): ";
cin >> ch;
}
switch (ch)
{
case 'A':
chooseletterA();
cout << "First question: Please solve for monthly payment. " << endl;
break;
case 'B':
chooseletterB();
cout << "Second question: Please solve for loan amount. " << endl;
break;
case 'C':
chooseletterC();
cout << "Third question: Please solve for length of loan. " << endl;
break;
case 'D':
chooseletterD();
cout << "Forth question: Please solve for interest rate. " << endl;
break;
}
}
int main() {
int ch;
if(!retrieve())
for (int i = 0; i < SIZE; i++) {
cout << "What is the amount of the loan?:$";
cin >> loanAmount[i];
cin.ignore();
//validation amount should be greater than 0
while (loanAmount[i] < 0)
{
cout << "loan amount should be greater than 0..What is the amount of the loan?:$";
cin >> loanAmount[i];
}
cout << "What is the interest rate on the loan?:$";
cin >> interestRate[i];
interestRate[i] /= 100;
cin.ignore();
cout << "What is the desired monthly payment?:$";
cin >> monthlyPayment[i]; cin.ignore();
while (monthlyPayment[i] < 0) {
cout << " Monthly payment should be greater than 0..What is the desired monthly payment?:$";
cin >> monthlyPayment[i];
}
while (monthlyPayment[i] < loanAmount[i] * interestRate[i] / 12) {
cout << "Not a valid amount, Amount must be greater than $";
cout << (loanAmount[i] * interestRate[i] / 12) << endl;
cout << "What is the desired monthly payment amount? $";
cin >> monthlyPayment[i]; cin.ignore();
}
}
id = 0;
process();
do {
cout << endl << "************************MENU***************************" << endl;
cout << "1. Process Loan for a customer" << endl;
cout << "2. Display Result" << endl;
cout << "3. Multiple Choice" << endl;
cout << "4. Save Results To File" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice : ";
cin >> ch;
system("pause");
switch (ch) {
case 1:
cout << "What is the customer id you want process its data [1-5]: ";
cin >> id;
cin.ignore();
//validation amount should be greater than 0
while (id < 1)
{
cout << "The customer id should be whithin [1-5] included, please enter new value: ";
cin >> id;
}
id = id - 1;
process();
break;
case 2:
displayResult();
break;
case 3:
multipleChoice();
break;
case 4:
save();
break;
//exit function
case 5:
cout << "Thank you";
return 0;
default:
cout << "Entered Wrong Keyword.... Re-enter Again";
break;
}
} while (ch != 5);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote