homework help

My program does correctly output the number of months it takes to repay the loan amount. could someone please help me figure this part out. I think its not producing the correct output because I have the wrong information inside the loop I'm trying to use.

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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	double L, I, MP;
	int M;
	int counter;

	cout << fixed << showpoint << setprecision(2);

	cout << "Enter loan amount: ";
	cin >> L;
	cout << endl;

	cout << "Enter interest rate: ";
	cin >> I;
	cout << endl;

	cout << "Enter the monthly payment desired: ";
	cin >> MP;
	cout << endl;

	while (I >= 1)
	
	MP = ((I / 100) * L) / M;

	cout << "Remaining balance is: $" << MP
		<< endl;
	
	while (MP > 0)
	{
		cout << "Remaining balance is: $" << MP
		<< endl;
	MP = MP - L;

	if (MP <= 0)
		cout << "This is the last payment: " << MP
		<< endl;

	else
		cout << "The payment for this month is: $" << MP
		<< endl;
	}

	M = M++;

	return 0;
}
Last edited on
1
2
while (I >= 1)
  MP = ((I / 100) * L) / M;

What do you expect this loop to do?
Either it doesn't execute at all (if i <1), or it will never terminate (if i>= 1) since you never modify I.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Modify "I" like how? How would I make so that the equation is false eventually. This is the real problem I am having with this chapter-- writing an correct LCV.
Last edited on
I think what you could do is make a loop at the beginning of the program and then when I is lower than 1, you just restart the loop and ask the user to input the data again.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
while(true)
{
	cout << "Enter loan amount: ";
	cin >> L;
	cout << endl;

	cout << "Enter interest rate: ";
	cin >> I;
	cout << endl;

	cout << "Enter the monthly payment desired: ";
	cin >> MP;
	cout << endl;
if (I < 1)
{
       continue;
}
else
{
//Rest of your code here, followed by a break
      break;
}



Or if you want to stick to the current structure and only want the user to adjust I, you can do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
while (I < 1)
{
	cout << "Interest rate has to be higher than 1: ";
	cin >> I;
	cout << endl;
}
// after the user finally found out which interger he should not enter for I, your script continues and spits out the balance. 
// I don't think you want to loop the code below, right?
cout << " bla1 ";
MP = ((I / 100) * L) / M;

cout << "Remaining balance is: $" << MP
		<< endl;
Last edited on
No I don't want to loop that code, its just an equation for the percentage.
Topic archived. No new replies allowed.