Subtracting in a loop

Need some help...I have a program I'm writing that is supposed to calculate the amount left on the loan. I've tried to use a do-while loop to get this done and I'm having a few issues. Can someone help me down the right path here? I know you would need to add the months starting at one. I'm having a hard time getting the amount of the loan to go down by the payment applied. I thought about calling a function, but I couldn't get that to work either. Not looking for the answer...just a nudge in the right direction. Thanks.

int main ()
{
int month = 1;
int loanamount, payment, loan;

cout << "Please enter the amount of the loan: ";
cin >> loanamount;

cout << "Please enter the amount of the payment each month: ";
cin >> payment;

loan = loanamount-payment

do
{
cout << "Month " << month++ << " balance is " << loan << " after the payment" << endl;
}

while (loan >=1 )


cout << "The last payment is " << payment << endl;
You are missing some semicolons and you are not updating loan in the loop.
( move loan = loanamount-payment inside the loop )
Thanks....I'll give it a shot...
Makes sense in theory...but when I move it into the loop..it only subtracts it once while the months keep adding up. I've fixed all of the syntax issues and the program executes...the only issue is that the payment is not being subtracted each time the loop is run. Do I need an if-else statement in there somewhere?
Here is the updated code....


int main ()
{
int month = 1;
int loanamount, payment, loan;

cout << "Please enter the amount of the loan: ";
cin >> loanamount;

cout << "Please enter the amount of the payment each month: ";
cin >> payment;

do
{
loan = loanamount-payment;

cout << "Month " << month << " balance is " << loan << " after the payment" << endl;
}

while (loan >=1);


cout << "The last payment is " << payment << endl;
get rid of loan and just use loanamount.

EX: cin loanamount...
in the do loop...loanamount = loanamount - payment...

What's the purpose of the last line? It just tells the user exactly what he already knows his last payment is going to be since all payments are the same amount.
Multiply payment month times in the subtraction.
You removed the ++ from month in the loop, so it won't increase unless you put it back
Yep...my bad on the month thing. Looks like I'm making progress...I'll post the final code here in a bit if I can get it figured out. Thanks again.

BTW...the last line is supposed to say your last payment is whatever is left. So...say your loan balance is $1500 and you pay $140 each time. For your last payment, your balance is only $100, so instead of paying $140, you would pay $100 to pay off the loan. Make sense?

That might be the challenging part.
This should work just fine then.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main ()
{
int month = 1;
int loanamount, payment;

cout << "Please enter the amount of the loan: ";
cin >> loanamount;

cout << "Please enter the amount of the payment each month: ";
cin >> payment;

do
{
loanamount = loanamount - payment;
cout << "Month " << month++ << " balance is " << loanamount << " after the payment" << endl;
}

while (loanamount >= payment);


cout << "The last payment is " << loanamount << endl;
Here is the final code. Thanks for your help All. I placed an "if" statement at the end because I just want the program to end if the ending balance is $0. Appreciate the inputs...huge help.

BTW...still new to this site. How do I have code displayed as the above? Right now I am just doing copy and paste (you can probably tell - :)).

int main ()
{
int month = 1;
int loanamount, payment;

cout << "Please enter the amount of the loan: ";
cin >> loanamount;

cout << "Please enter the amount of the payment each month: ";
cin >> payment;

do
{
loanamount = loanamount-payment;

cout << "Month " << month++ << " balance is $" << loanamount << " after the payment" << endl;
}

while (loanamount >= payment);

if (loanamount > 0)

{
cout << "The last payment is $" << loanamount << endl;
}
}
to put text into code click the little <> button by the editor. Or just enter code and /code surrounded by []'s
Topic archived. No new replies allowed.