-
How do you ??????
What I want to do is to insert a blank line every 6 lines in the print out, here is what I have:
// This program produces a loan amortization
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;
int main()
{
ofstream outfile("A:Mort.txt");
char name[31];
float loan, rate, years, balance, term, payment;
cout << "Enter the name of the mort. recipant. ";
cin.getline(name, 31);
cout << "Loan amount: $";
cin >> loan;
cout << "Annual Interest Rate: ";
cin >> rate;
cout << "Years of loan: ";
cin >> years;
term = pow((1 + rate / 12.0), 12.0 * years);
payment = (loan * rate / 12.0 * term) / (term - 1.0);
outfile.precision(2);
outfile.setf(ios::fixed | ios::showpoint | ios::left);
outfile << "The loan belongs to : " << name << endl;
outfile << "Monthly payment: $" << payment << endl;
outfile << endl;
outfile << setw(10) << "Pay. No.";
outfile << setw(10) << "Interest";
outfile << setw(10) << "Principal";
outfile << setw(10) << "Balance" << endl;
outfile << "-------------------------------------\n";
balance = loan;
for (int month = 0; month < (12 * years); month++)
{
float minterest, principal;
minterest = rate / 12 * balance;
principal = payment - minterest;
outfile << setw(10) << (month + 1);
outfile << setw(10) << minterest;
outfile << setw(10) << principal;
outfile << setw(10) << balance << endl;
balance -= principal;
}
outfile.close();
return 0;
}
So I was thinking I would add :
if ((month%6)==0)
outfile << "Question how do you display a blank line?" ;
For some reason I can't remember how to display a blank line
TIA
Bill
-
I don't know much about fstreams, but wouldn't
outfile<<endl<<endl
do the trick?
-
This is where I put the if statement
balance -= principal;
}
if ((month % 6) == 0)
{
outfile << endl<<endl;
}
That didn't seam to work but thanks anyway... Maybe I put it in the wrong place????
Anybody else?????
TIA
Bill
-
> Maybe I put it in the wrong place????
I think you did
balance -= principal;
} <--- this is the closing brace of your for loop
// which is odd, because isn't month now supposed to be out of scope?
// in any event, it only happens once, when the for loop has finished.
if ((month % 6) == 0)
{
outfile << endl<<endl;
}
-
Thanks Salem I got that fixed, however when it printed to my out file it skipped the 1: here let me show you...
1 44.67 72.81 6700.00
2 44.18 73.29 6627.19
3 43.69 73.78 6553.90
4 43.20 74.27 6480.12
5 42.71 74.77 6405.85
6 42.21 75.27 6331.08
7 41.71 75.77 6255.82
8 41.20 76.27 6180.05
9 40.69 76.78 6103.78
10 40.18 77.29 6027.00
11 39.66 77.81 5949.70
12 39.15 78.33 5871.90
13 38.62 78.85 5793.57 \
See it didn't do right, it supposed to leave a blank line after the no. 6...How can I fix this???? Any suggestions????
TIA
Bill
-
Month starts at 0 (in the for loop), so month % 6 will also be 0
The test you need is
if ( (month+1) % 6 ) == 0 )
-
Thanks Salem that did the trick....
Thanks
Bill
-
One last question.... How do I get the totals of principal and interest.... I want to display them every 36th line....
TIA
Bill