Why am I getting a debug runtime error every time I run this

Why am I getting a debug runtime error every time I run this program in Visual Studio?

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

using namespace std;

//Reads the inputs principal amount, rate of interest, and number of payments.
void readInputs(double &presentValue, double &rateOfInterest, int &numOfPayments, double &tax_pm)
{
cout << \"Enter the loan amount: \";
cin >> presentValue;
cout << \"Enter the yearly rate of interest(as a percentage): \";
cin >> rateOfInterest;
rateOfInterest = (rateOfInterest / 100) / 12;
cout << \"Enter the number of payments (in months): \";
cin >> numOfPayments;
cout << \"What is the tax amount per year? $\";
cin >> tax_pm;
}

//Calculates the EMI per payment, given the inputs.
void EMICalc(double presentValue, double rateOfInterest, int numOfPayments, double &EMI)
{
EMI = (rateOfInterest * presentValue) / (1 - pow((1 + rateOfInterest), (-1 * numOfPayments)));
EMI = floor(EMI * 100 + 0.5) / 100;
}

//Calcualtes the total amount of tax paid each year
void calculateTax(double tax, int numOfPayments)
{
double No_of_Payments_in_years = numOfPayments / 12;
double totalTax = tax * No_of_Payments_in_years;
double tax_pm = totalTax / numOfPayments;
}

void printTable(double pV, double roI, int noP, double emi, double tax, double tax_pm)
{
//For including thousand separator
locale system_locale(\"\");
cout.imbue(system_locale);
cin.imbue(system_locale);

ofstream outFile;

//For including thousand separator
outFile.imbue(system_locale);

outFile.open(\"MortgageTablePgmV2_Results.txt\");
outFile << \"Principal \\t $\" << pV << \" \\t\\t Payment \\t $\" << emi << endl;
outFile << \"Annual Interest \\t \" << fixed << setprecision(1) << roI * 12 * 100 << \"% \\t Term \\t \" << noP << \" Months\" << endl << endl;

//Printing table header with proper formatting
outFile << left << setw(15) << \" Payment\" << setw(16) << \" Amount \" << setw(18) << \" Principal \" << setw(18) << \" Interest \" << setw(15) << \" Tax \" << setw(10) << \" PrincipalBalance \" << endl;
double interest, principal;
for (int i = 0; i < noP; i++)
{
  interest = pV * roI;
  if (interest < 0)
  {
   interest = 0;
  }

  principal = emi - interest;
  if (principal < 0)
  {
   principal = 0;
  }
  pV = pV - principal + tax;
  if (pV < 0)
  {
   pV = 0;
  }

  //Writing calculated values to file with required format
  outFile << right << setw(5) << (i + 1) << setw(10) << fixed << setprecision(2) << \"$\" << (principal + interest);
  outFile << setw(10) << fixed << setprecision(2) << \"$\" << principal << setw(10) << fixed << setprecision(2) << \"$\" << interest;
  outFile << setw(10) << fixed << setprecision(2) << \"$\" << tax_pm << setw(10) << fixed << setprecision(2) << \"$\" << pV << endl;
}
outFile << \"Final Payment\\t\" << interest + principal << endl;
}

int main()
{
double presentValue, rateOfInterest, EMI, tax, tax_pm;
int numOfPayments;
readInputs(presentValue, rateOfInterest, numOfPayments, tax_pm);
EMICalc(presentValue, rateOfInterest, numOfPayments, EMI);
calculateTax(tax, numOfPayments);
printTable(presentValue, rateOfInterest, numOfPayments, EMI, tax, tax_pm);
cout << \"The monthly EMI is: \" << EMI << endl;
double totalInterest = EMI * numOfPayments - presentValue;
cout << \"Total interest paid: \" << totalInterest << endl;
}

Microsoft Visual C Runtime Library Debug Error! Program: ...k4Mortgage Table Pgm V2-1 Debug Mortgage Table Pgm V2-1.exe Module k 4Mortgage Table Pgm V2-1\\Debug Mortgage Table Pgm V2-1.exe File: Run-Time Check Failure #3 T (Press Retry to debug the application) Abot Betry gnore

Solution

Hi,

I dont see any issues with code. it is working. Issue must be with your debuggin settings in IDE. To get ride of this issue you may stop your debug settings.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
//Reads the inputs principal amount, rate of interest, and number of payments.
void readInputs(double &presentValue, double &rateOfInterest, int &numOfPayments, double &tax_pm)
{
cout << \"Enter the loan amount: \";
cin >> presentValue;
cout << \"Enter the yearly rate of interest(as a percentage): \";
cin >> rateOfInterest;
rateOfInterest = (rateOfInterest / 100) / 12;
cout << \"Enter the number of payments (in months): \";
cin >> numOfPayments;
cout << \"What is the tax amount per year? $\";
cin >> tax_pm;
}
//Calculates the EMI per payment, given the inputs.
void EMICalc(double presentValue, double rateOfInterest, int numOfPayments, double &EMI)
{
EMI = (rateOfInterest * presentValue) / (1 - pow((1 + rateOfInterest), (-1 * numOfPayments)));
EMI = floor(EMI * 100 + 0.5) / 100;
}
//Calcualtes the total amount of tax paid each year
void calculateTax(double tax, int numOfPayments)
{
double No_of_Payments_in_years = numOfPayments / 12;
double totalTax = tax * No_of_Payments_in_years;
double tax_pm = totalTax / numOfPayments;
}
void printTable(double pV, double roI, int noP, double emi, double tax, double tax_pm)
{
//For including thousand separator
locale system_locale(\"\");
cout.imbue(system_locale);
cin.imbue(system_locale);
ofstream outFile;
//For including thousand separator
outFile.imbue(system_locale);
outFile.open(\"MortgageTablePgmV2_Results.txt\");
outFile << \"Principal \\t $\" << pV << \" \\t\\t Payment \\t $\" << emi << endl;
outFile << \"Annual Interest \\t \" << fixed << setprecision(1) << roI * 12 * 100 << \"% \\t Term \\t \" << noP << \" Months\" << endl << endl;
//Printing table header with proper formatting
outFile << left << setw(15) << \" Payment\" << setw(16) << \" Amount \" << setw(18) << \" Principal \" << setw(18) << \" Interest \" << setw(15) << \" Tax \" << setw(10) << \" PrincipalBalance \" << endl;
double interest, principal;
for (int i = 0; i < noP; i++)
{
interest = pV * roI;
if (interest < 0)
{
interest = 0;
}
principal = emi - interest;
if (principal < 0)
{
principal = 0;
}
pV = pV - principal + tax;
if (pV < 0)
{
pV = 0;
}
//Writing calculated values to file with required format
outFile << right << setw(5) << (i + 1) << setw(10) << fixed << setprecision(2) << \"$\" << (principal + interest);
outFile << setw(10) << fixed << setprecision(2) << \"$\" << principal << setw(10) << fixed << setprecision(2) << \"$\" << interest;
outFile << setw(10) << fixed << setprecision(2) << \"$\" << tax_pm << setw(10) << fixed << setprecision(2) << \"$\" << pV << endl;
}
outFile << \"Final Payment\\t\" << interest + principal << endl;
}
int main()
{
double presentValue, rateOfInterest, EMI, tax, tax_pm;
int numOfPayments;
readInputs(presentValue, rateOfInterest, numOfPayments, tax_pm);
EMICalc(presentValue, rateOfInterest, numOfPayments, EMI);
calculateTax(tax, numOfPayments);
printTable(presentValue, rateOfInterest, numOfPayments, EMI, tax, tax_pm);
cout << \"The monthly EMI is: \" << EMI << endl;
double totalInterest = EMI * numOfPayments - presentValue;
cout << \"Total interest paid: \" << totalInterest << endl;
}

Output:

sh-4.2$  g++ -o main *.cpp                                                                                                                                                                                                                             

sh-4.2$ main                                                                                                                                                                                                                                           

Enter the loan amount: 10000                                                                                                                                                                                                                           

Enter the yearly rate of interest(as a percentage): 10                                                                                                                                                                                                 

Enter the number of payments (in months): 12                                                                                                                                                                                                           

What is the tax amount per year? $100                                                                                                                                                                                                                  

The monthly EMI is: 879.16                                                                                                                                                                                                                             

Total interest paid: 549.92

Why am I getting a debug runtime error every time I run this program in Visual Studio? #include <iostream> #include <iomanip> #include <fstream&g
Why am I getting a debug runtime error every time I run this program in Visual Studio? #include <iostream> #include <iomanip> #include <fstream&g
Why am I getting a debug runtime error every time I run this program in Visual Studio? #include <iostream> #include <iomanip> #include <fstream&g
Why am I getting a debug runtime error every time I run this program in Visual Studio? #include <iostream> #include <iomanip> #include <fstream&g

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site