Do While loop returns INF

I'm trying to make a compound interest program for an assignment that tells me at what point Bob's compound interest will catch up with Alice's flat, higher interest rate. Right now, when I enter in their beginning amount, it simply returns that value, prints INF twice, and then some really high number for the year, in the 20,000s, depending on what my starting amount was. Can anyone help me find out why it's doing this?

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
62
63
64
65
66
67
68
  #include <iostream>
using namespace std;

int main()
{
	double alice_money;
	double bob_money;
	double alice_interest;
	double alice_interestgained;
	double bob_interest = 0.03;
	double bob_interestgained;
	int year = 0;

	cout << "How much money did Alice and Bob invest?\n";
	cin >> alice_money;
	bob_money = alice_money;

	if (alice_money >= 1 && alice_money <= 99)
	{
		alice_interest = 0.07;
	}

	else if (alice_money >= 100 && alice_money <= 299)
	{
		alice_interest = 0.08;
	}

	else if (alice_money >= 300 && alice_money <= 499)
	{
		alice_interest = 0.09;
	}

	else if (alice_money >= 500 && alice_money <= 1000)
	{
		alice_interest = 0.10;
	}

	else if (alice_money > 1000)
	{
		alice_interest = 0.11;
	}

	do
	{
		alice_interestgained = (alice_money * alice_interest);
		bob_interestgained = (bob_money * bob_interest);
		alice_money = (alice_money + alice_interestgained);
		bob_money = (bob_money + bob_interestgained);
		year = year + 1;
	}

	while (bob_money < alice_money);

	/*while (bob_money < alice_money)
	{
		alice_interestgained = (alice_money * alice_interest);
		bob_interestgained = (bob_money * bob_interest);
		alice_money = alice_money + alice_interestgained;
		bob_money = bob_money + bob_interestgained;
		year = year + 1;
	}
	*/
	cout << bob_money << "\n";
	cout << alice_money << "\n";
	cout << year << "\n";

	return 0;
}
Alice will always have a higher percentage rate than Bob. Bob's interest rate is defined on line 10. Take a look at your while condition on line 52. We already know Bob will always the lower amount of money, so this is always going to perform whatever is in the inside the do brackets, which they will always be getting more and more money, leading to infinity.

As your code has defined, Alice will ALWAYS have more money than Bob until both reach infinity.
@fiji885 is right.

You are compounding the interest for both Alice and Bob - yet you said in the statement of your problem that Alice had a "flat" interest rate (presumably simple, rather than compound interest). Alice's interest should be based on the principal (the initial amount invested), not the accumulated amount with interest added every year.


Incidentally, apropos of nothing, since alice_money is a double, what interest rate applies if it is input as 99.5? or 199.5 etc.?
Last edited on
fiji885 was correct, it was that I forgot to set up Alice's interest as non-compounding. I have the correct setup now, and it provides accurate outputs. The only problem left is that I'm supposed to set it up that any value less than 1 is set to 1. I added an if statement before the if/else if section stating that:

if (alice_deposit < 1)
{
alice_deposit = 1)
}

But when I enter -1 into the program for the initial deposit (as per the instructor's request), it doesn't continue the program. It simply stops there.
@Elarionus,
I think you had better post your modified code. We aren't psychic.
Registered users can post here. Sign in or register to post.