For loop not outputting table to file

I'm currently writing a mortgage calculator for a class project, but I'm having an issue with outputting it to a file. I need to output monthly payments in a table format(which I believe I have the formatting correct), but the for loop is not outputting anything. It should be outputting as long as i is less than t which is the term of the loan from years to months.

Any ideas on where I go from here? Apologies for the messy code by the way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;

int main() {
	ofstream outputFile;
	outputFile.open("mortgage.txt");

	double  P, R, T, t, i, MP, month;
	month = 0;

	/* Input data for calculation */
	cout << "Please enter the total amount borrowed: ";
	cin >> P;

	cout  << "Enter the annual interest rate(percent form): ";
	cin >> R;

	cout << "Enter the term of the loan in years: ";
	cin >> T;

	/*Convert data to useable form for the formula */
		t = T * 12; // loan in MONTHS
		i = (R / 100) / 12; // i = monthly interest rate. convert interest rate to decimal then calculate for monthy interest rate

	/*Monthly payment output*/
	MP = ((P * i) * pow((1 + i), t)) / (pow((1 + i), t) - 1);


	/*Outputting data to file*/
	outputFile << "Principle: " << P << "\n"
		<< "APR: " << R << '%' << "\n"
		<< "Years: " << T << "\n"
		<< "Payment: " << '$' << MP << endl;
	outputFile << "Month #" << setw(15) << setfill(' ') << "Balanced Owed" << setw(20) << setfill(' ')<<  "Interested Accrued" << setw(15)<< setfill(' ') << "Payment Made" << endl;

	/*For loop: output data for each month while i is less than t(months of loan)*/

	for (int i = 0; i <= t, i++;) {
		outputFile << "Month " << month << setw(15) << setfill(' ') << P << setw(20) << setfill(' ') << "put interested accrued here soon" << setw(15) << setfill(' ') << MP << endl;

		month++;
		P = P - MP;
	}
	outputFile << "\t" << "Total Interest: " << ((MP *t) -P) << "\n"
		<< "\t" << "Total Cost: " << (MP * t) << endl;

	outputFile.close();

	system("pause");
	return 0;
}
closed account (48T7M4Gy)
for (int i = 0; i <= t, i++)
Ah thanks kenmort, didn't even notice that mistake.

I'm having an additional problem that might be an easy fix, but I'm not sure where to go with it.
My total interest is outputting as higher than P (it's P + monthly payment).

How do I calculate Total Interest: (MP * T) - P (where P is the initial value, not the value after the loop)?
Last edited on
closed account (48T7M4Gy)
As a guess, because I haven't looked at you methodology all that closely, I would reckon that in pseudocode:
a) total interest = total payments - principal

b) total payments = monthly payments * no of months ( assuming payments per month are constant )

So it appears we might agree.

a) is more general and b) requires you to keep a running total if payments in lieu if they aren't the same each month, or alternatively you can use the standard equations for summing geometric progressions where the payments are based on the principal owing and therefore vary from payment to payment etc :)
Last edited on
Thanks for the help, got everything working properly now :).
closed account (48T7M4Gy)
Show me what you expect the output to be in this case please.
Topic archived. No new replies allowed.