something is wrong with my while loop. it prints the last code twice. it takes 3 numbers from a text file and calculates the monthly payment and prints to screen. the problem is it prints the last monthly payment twice.
Code:
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cstdio>
#include <cmath>
using namespace std;
void usage()
{
cout << "Usage: loancalc <filename>" << endl;
exit (1);
}
double monthlyPayment (double amount, int months, double interest)
{
double ratem, expm, mp;
ratem = interest / 1200.00;
expm = 1.0 + ratem;
mp = (ratem * pow(expm, months) * amount) / ( pow(expm, months) -1.0);
return mp;
}
void displayLoanInfo (double amount, int months, double interest)
{
double mp, total;
printf ("%10.2f %8d %10.2f ",amount, months, interest);
if (amount > 0 && months > 0 && interest > 0)
{
mp = monthlyPayment(amount, months, interest);
total = mp * months;
printf ("%11.2f %12.2f \n",mp, total);
}
else printf (" <error> <error>\n");
}
int main (int argc, char **argv)
{
ifstream infile;
double d1,d2;
int i;
if (argc !=2)
{
usage ();
}
infile.open (argv[1]);
printf(" Monthly\n");
printf(" Loan Months Rate Payment Total\n");
printf("-----------------------------------------------------------\n");
if (!infile.fail ())
{
while (!infile.eof ())
{
infile >> d1 >> i >> d2;
displayLoanInfo (d1, i, d2);
}
}
else cout << "No File!" << endl;
infile.close ();
return 0;
}