Help with program -Beginner

Hello, I have been working on my c++ code for quite sometime and have not been able to get the output correct in it. It is a mortgage calculator that asks the user for the mortgage amount, interest, payment amount, and loan duration. It then outputs it into columns.I have gotten help from several people but am having trouble with my first row, the columns do not align correctly. Any input or 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<<"\t"<<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);
morgBalance = (morg - payment) + interPaid;
}
if (morg > 0)

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

}
[code]

the second code wrap needs to be [/ code] but with no space.


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
55
56
57
58
59
60
61

#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<<"\t"<<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);
morgBalance = (morg - payment) + interPaid;
}
if (morg > 0)

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

}
Here is my real answer. You should just use the setw() function in your cout statements. It will set the answers to a specific width of chars. but in order to keep it left justified, it would have to look something like this.

cout << left << setw(4) << "1" << setw(4) << "2" << endl;
Thank you for your reply. I did have that previously but the teacher has specified that I not use setw, since it is not in the lesson plan. Thats why im stuck
Well if you're not allowed to use that. On line 11 remove your first tab.
Topic archived. No new replies allowed.