I have been having trouble outputing the correct code in my program. It is for school and the teacher specified that i not use setw. The program is a mortgage calulator, cannot get the right out put.My balance is not coming out correclty on the first line and I think that is throwing things off. Everything else comes our correctly but that comes out as a 0.00 instead of the inputed number and this makes the first row of information not lined up correctly. Any help would be appreciated [code]

#include <iostream>

#include <iomanip>using namespace std;

double Mortgage(int payCounter, double morg, double payment, double years, double rate)

{

double morgBalance, interPaid;

interPaid = (morg - payment) * rate/12;

cout<< ""<<payCounter<<"\t"<<morgBalance<<"\t"<<payment<<"\t\t"<<interPaid<<endl;

morgBalance = (morg - payment) + interPaid;

return morgBalance;

}int main()

{

double morg;

double rate;

double years;

double months;

double payment;

int payCounter;cout<<"Please enter a mortage amount:";

cin>> morg;cout<<" Please enter an annual interest rate:";

cin>> rate;

rate = (rate/100);

double monthRate = rate/12;cout<<" Please enter a payment amount:";

cin>> payment;cout<<" Please enter number of years for the loan:";

cin>> years;double interPaid = (morg - payment) * rate/12;

double morgBalance = (morg - payment) + interPaid;

double numberPay = years * 12;cout<< setiosflags(ios::fixed | ios::showpoint) << setprecision(2);

cout<<"\n";

cout<<"Month\tBalance\t\tPayment\t\tInterest"<<endl;for (payCounter = 1; payCounter <= numberPay; payCounter++)

{

morg = Mortgage (payCounter, morg, payment, interPaid, rate);

}

if (morg > 0){

cout << "Ballooon payment:"<<morg<< endl;

}}

[/code]

You display the mortgage balance before calculating it, in the function Mortgage, so it's a matter of chance what it will display each time. You have to calculate the balance before displaying it.

Thank you for the reply. I did do that and the columns do align now. But the balance output from the user is incorrect on the first row.. Ex. (myoutput)

Month Balance Payment Interest

1 99412.50 1000.00 412.50

2 98822.55 1000.00 410.05

3 98230.15 1000.00 407.59Correct Output:

Month Balance Payment Interest

1 100000.00 1000.00 412.50

2 99412.50 1000.00 410.05

3 98822.55 1000.00 407.59

What you need to do is to run the program in a debugger and single-step through it. Think about what the values of the variables should be at each stage and then compare them with what is actually happening. It should then be obvious where the error is. An alternative is to work through the program on paper, again checking what happens at each step. I could do this for you, but your going to learn more if you try to debug your own programs. You have to think about what your program is meant to do and what it actually is doing. I think if you do this you will find that the error is trivial.

I have tried that several times and am still not getting it..

I guess it all depends upon what you mean by "mortgage balance". When you print the monthly figures, do you want to display the balance before the payment and interest have been taken into account, or after? You are displaying the latter (which to me seems correct), but if you want the former it's a trivial calculation.

Ask Your Question

Weekly Poll