Hello,

I am a first year Comp Sci student and i am trying to finish this program. I am almost done with it but i just can't figure out one thing which is that, how do i show the initial principal amount under Principal heading in loop? the initial amount is \$1000. I just need some help in order to finish my assignment (which i am already 99% done?) Thanks in advance and please scroll down for the code and its output, thanks again.

``````#include <iostream>
#include <iomanip>

using namespace std;

int main ()

{
// Variables declraced
double principal, interest, monthlyAmount, monthlyInterest, principalPaid, interestPaid, total = 0;
int month = 0;

// User input screen menu
cout << "Enter the amount of the loan: ";
cin >> principal;
cout << "Enter the yearly interest rate: ";
cin >> interest;
cout << "Enter the monthly amount paid: ";
cin >> monthlyAmount;
cout << "\n";

// Headings for the loan calculation
cout << "Month";
cout << setw(6) << " " << "Principal";
cout << setw(5) << " " << "Interest Paid";
cout << setw(5) << " " << "Principal Paid";
cout << setw(5) << " " << "Remaining Balance\n";
cout << "\n";

while (principal > 0)

{
// Calculations for the loan
month++;
monthlyInterest = (interest/100) /12;
interestPaid = principal * monthlyInterest;
principalPaid = monthlyAmount - interestPaid;
principal = principal - principalPaid;

total += interestPaid;

// Settings for, to show the decimal precision
cout << fixed << showpoint << setprecision(2);

// Calculations output
cout << setw(2) << month;
cout << setw(16) << principal;
cout << setw(17) << interestPaid;
cout << setw(18) << principalPaid;
cout << setw(19) << principal <<  endl;
}

// Shows the result of the calculated loan
cout << "\n";
cout << "Number of months to pay off the loan: " << month << endl;
cout << endl;
cout << "Total interest paid on loan: \$" << total << endl;
cout << endl;
cout << "You have a credit of: \$" << principal << endl;
cout << endl;
cout << "\n";

return 0;
}``````

see under Principal it shows 965.00 but i want to show 1000 like start it from 1000...thats the only problem i m having wit it....

``````Enter the amount of the loan: 1000
Enter the yearly interest rate: 18
Enter the monthly amount paid: 50

Month      [B]Principal[/B]     Interest Paid     Principal Paid     Remaining Balance

1          965.00            15.00             35.00             965.00
2          929.48            14.48             35.52             929.48
3          893.42            13.94             36.06             893.42
4          856.82            13.40             36.60             856.82
5          819.67            12.85             37.15             819.67
6          781.97            12.30             37.70             781.97
7          743.70            11.73             38.27             743.70
8          704.85            11.16             38.84             704.85
9          665.42            10.57             39.43             665.42
10          625.40             9.98             40.02             625.40
11          584.79             9.38             40.62             584.79
12          543.56             8.77             41.23             543.56
13          501.71             8.15             41.85             501.71
14          459.24             7.53             42.47             459.24
15          416.13             6.89             43.11             416.13
16          372.37             6.24             43.76             372.37
17          327.95             5.59             44.41             327.95
18          282.87             4.92             45.08             282.87
19          237.11             4.24             45.76             237.11
20          190.67             3.56             46.44             190.67
21          143.53             2.86             47.14             143.53
22           95.68             2.15             47.85              95.68
23           47.12             1.44             48.56              47.12
24           -2.17             0.71             49.29              -2.17

Number of months to pay off the loan: 24

Total interest paid on loan: \$197.83

You have a credit of: \$-2.17

Press any key to continue . . .``````

## Recommended Answers

Before you print out your first line, you are already doing your calculations. You will need to figure out a way how to print out your first line, then start the math.

``````while (principal = 0)

{

// Settings for, to show the decimal precision
cout << fixed << showpoint << setprecision(2);

// Calculations output
cout << setw(2) << month;
cout << setw(16) << principal;
cout << setw(17) << interestPaid;
cout << setw(18) << principalPaid;
cout << setw(19) << …``````

## All 6 Replies Before you print out your first line, you are already doing your calculations. You will need to figure out a way how to print out your first line, then start the math.

``````while (principal = 0)

{

// Settings for, to show the decimal precision
cout << fixed << showpoint << setprecision(2);

// Calculations output
cout << setw(2) << month;
cout << setw(16) << principal;
cout << setw(17) << interestPaid;
cout << setw(18) << principalPaid;
cout << setw(19) << principal <<  endl;
// Calculations for the loan
month++;
monthlyInterest = (interest/100) /12;
interestPaid = principal * monthlyInterest;
principalPaid = monthlyAmount - interestPaid;
principal = principal - principalPaid;

total += interestPaid;

}``````

So you just need to use the printing code before the calculations, and set the while condition to principal = 0, for the last line to appear as well.

``````while (principal = 0)

{

// Settings for, to show the decimal precision
cout << fixed << showpoint << setprecision(2);

// Calculations output
cout << setw(2) << month;
cout << setw(16) << principal;
cout << setw(17) << interestPaid;
cout << setw(18) << principalPaid;
cout << setw(19) << principal <<  endl;
// Calculations for the loan
month++;
monthlyInterest = (interest/100) /12;
interestPaid = principal * monthlyInterest;
principalPaid = monthlyAmount - interestPaid;
principal = principal - principalPaid;

total += interestPaid;

}``````

So you just need to use the printing code before the calculations, and set the while condition to principal = 0, for the last line to appear as well.

This is a bad loop design. You are performing an assignment in the loop's condition. As a general rule, that should be avoided.

In fact, because a while runs on a true condition, and non-zero values are considered true values while zero (0) is considered false, this loop will never run because you are assigning a false value.

For this loop to work properly, you'll want to use `while (priniciple > 0) {...}` . You could also use simply `while (principle) {...}` but, depending on dataType, that may lead to an issue caused by rounding errors and/or negative numbers.

This is a bad loop design. You are performing an assignment in the loop's condition. As a general rule, that should be avoided.

In fact, because a while runs on a true condition, and non-zero values are considered true values while zero (0) is considered false, this loop will never run because you are assigning a false value.

For this loop to work properly, you'll want to use `while (priniciple > 0) {...}` . You could also use simply `while (principle) {...}` but, depending on dataType, that may lead to an issue caused by rounding errors and/or negative numbers.

Oh sorry for the typo. What i meant was

``while (principle >= 0)``

hmm... but wait, since the last line has a negative principal, it will still not get printed.
So i think there needs to be an if condition after the while loop, which checks whether the last principle is negative. If it is then it will print all the data for that line again.

Thanks guys, i figured it out how to show "1000" under principal. Thanks for the help.

However, another question is that if i want to use Input Validation in this code for the user menu, i use

``IF statement``

right?

Thanks guys, i figured it out how to show "1000" under principal. Thanks for the help.

That was not your only error. The principal balance at the beginning of a month should be equal to the remaining balance at the end of the prior month. Your output does not show that. You have the same principal amounts at the beginning and end of the same month for the entire 24 month period.

I suggest creating a new variable called 'principalBegin' and change the 'principal' variable to 'principalEnd'. Initialize 'principalBegin' to 1,000 and change line 39 to 'principalEnd = principalBegin - principalPaid'.

Since you are a ComSci major and not a Finance major, I will only point out, and not offer any correction to, your conceptually erroneous calculations. To compute a loan amortization table (your exercise) one must use the present value interest factor of an annuity corresponding to the interest rate, payment frequency, and life of the loan. Ask a finance professor at your university about it because your C++ professor likely won't have a clue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.