Loop Issue

I need month #1's balance to be zero, but when I run the program, it is 100...Any suggestions?

Here is my code:
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
#include <iostream>

using namespace std;

int main()
{
	int down_payment, month, cnum;
	
	double  balance, payment, interest, shortage, total_interest;
	
	cout << "How much is your down payment for your car? ";
	cin >> down_payment;
	
	month = 1;
	balance = 0 ;
	total_interest = 0; 
	payment = 100;
	
		
	while (balance<=down_payment)
	{
		interest = balance * .001;
		balance = balance + payment + interest ;
		shortage = down_payment - balance;
		total_interest += interest;
		
		if (balance + payment >= down_payment)
		{
			payment = down_payment - balance ;
		}

		cout << "\nMONTH: \tBALANCE: \tPAYMENT: \tINTEREST: \tSHORTAGE:" ;
		cout << "\n" << month << "\t" << balance << "\t\t" << payment << "\t\t" << interest << "\t\t" << shortage ;
		month ++ ;
	}
	
	cout << "\nTotal number of months needed: " << month ;
	cout << "\nTotal amount of interest earned: " << total_interest ;
	
	cin >> cnum;

   return 0;
}
balance = balance + payment + interest ;

Payment = 100.
Last edited on
thats the thing. Every month the payment is 100, starting from the first month. but the balance of the first month is zero.

how do i get the balance of the first month to be zero and the rest of the months to be "balance = balance + payment + interest" ? thanks
Why not just initialize payment @ 0? And you should probably use a for loop not a while loop...
http://www.cplusplus.com/doc/tutorial/control/
ex:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* I would initialize them all @ 0...
balance=0;
payment=0;
total_interest=0;
month=0;
*/

 for(months=0;balance<=downpayment;months++) //for(initializer;condition;increment)statement;
{ //Since you calculate your payment with the if statement you can initialize payment @ 0...
                interest = balance * .001; //You didn't initialize interest, I don't know if that's what you want...
		balance = balance + payment + interest ;
		shortage = down_payment - balance;
		total_interest += interest;
		
		if (balance + payment >= down_payment)
		{
			payment = down_payment - balance ;
		}
                else payment=100; // here's where you could put your 100...

		cout << "\nMONTH: \tBALANCE: \tPAYMENT: \tINTEREST: \tSHORTAGE:" ;
		cout << "\n" << month << "\t" << balance << "\t\t" << payment << "\t\t" << interest << "\t\t" << shortage ;
		// you can get rid of: month ++ ;
	}


Also what is with cnum? Why input with no output?
Anyways, I probably missed stuff but you get the general idea.

Sean
how do i get the balance of the first month to be zero and the rest of the months to be "balance = balance + payment + interest" ? thanks


You need to rethink the logic of your operations, so that your reporting of the first month's balance happens before it's adjusted for the first payment.
Topic archived. No new replies allowed.