HW/Assignment Issue While Loop Misbehavin'

Hello, I have been asked to create the following output using a loop:
How much do you want to save each month? 100
Month 1: $0 deposited 100 interest earned is 1
Month 2: $101 deposited 100 interest earned is 2.01
Month 3: $203.01 deposited 100 interest earned is 3.0301 Month 4: $306.04 deposited 100 interest earned is 4.0604
Month 5: $410.101 deposited 100 interest earned is 5.10101
Month 6: $515.202 deposited 100 interest earned is 6.15202
Month 7: $621.354 deposited 100 interest earned is 7.21354
Month 8: $728.567 deposited 100 interest earned is 8.28567
Month 9: $836.853 deposited 100 interest earned is 9.36853
Month 10: $946.221 deposited 100 interest earned is 10.4622
It took 10 months, and you now have $1056.68

Now, this is the code I have written:
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
// Enter your program here
#include <iostream>
using namespace std;


// Enter your program here
#include <iostream>
using namespace std;

int main(){
  float balance = 0.0;
  int month = 1;
   float interest = 0.0;
   float newBalance = 0.0;
   float newInterest = 0.0;
   
cin >> balance;   
cout << "How much do you want to save each month? " << balance << endl;
 

while (newBalance < 1000.00){  


cout << "Month " << month << ": " << "$" << newBalance << " deposited " << balance << " interest earned is " << interest << endl;  
month++;
newBalance = balance + newBalance;
interest = (0.01 * newBalance);
newBalance = newBalance + interest;
}
    
    cout << "It took " << month << " months, and you now have $" << newBalance << endl;
return 0;   
}


My output is this:
How much do you want to save each month? 100
Month 1: $0 deposited 100 interest earned is 0
Month 2: $101 deposited 100 interest earned is 1
Month 3: $203.01 deposited 100 interest earned is 2.01
Month 4: $306.04 deposited 100 interest earned is 3.0301
Month 5: $410.1 deposited 100 interest earned is 4.0604
Month 6: $515.201 deposited 100 interest earned is 5.10101
Month 7: $621.354 deposited 100 interest earned is 6.15201
Month 8: $728.567 deposited 100 interest earned is 7.21354
Month 9: $836.853 deposited 100 interest earned is 8.28567
Month 10: $946.221 deposited 100 interest earned is 9.36853
It took 11 months, and you now have $1056.68

The interest is correct I think, but there is something off about the looping sequence. I have been up all night trying to do this, I'm beginning to suspect I'm not very good at this. Please help.

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
  
// Enter your program here
#include <iostream>
using namespace std;

int main(){
  float balance = 0.0;
  int month = 1;
   float interest = 0.0;
   float newBalance = 0.0;
   float newInterest = 0.0;
   
cin >> balance;   
cout << "How much do you want to save each month? " << balance << endl;
 

while (newBalance < 1000.00){  


cout << "Month " << month << ": " << "$" << newBalance << " deposited " << balance << " interest earned is " << interest << endl;  
month++;
newBalance = balance + newBalance;
interest = (0.01 * newBalance);
newBalance = newBalance + interest;
}
    
    cout << "It took " << month << " months, and you now have $" << newBalance << endl;
return 0;   
}
start with 'month' variable = 0;

 
int month = 0 ;


exchange line 21 with line20 ;
Last edited on
Hello alexelliott83,

Notice the way I rearranged your program:

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

int main()
{
	double balance = 0.0;
	int month = 1;
	double interest = 0.0;
	double newBalance = 0.0;
	double newInterest = 0.0;

	std::cout << "How much do you want to save each month? ";
	std::cin >> balance;
	std::cout << "How much do you want to save each month? " << balance << std::endl;  // <--- Could use a new message.

	std::cout << std::fixed << std::showpoint << std::setprecision(2);  // <--- Will print later doubles with two decimals. Needs header file "iomanip".

	while (newBalance < 1000.00)
	{

		newBalance += balance;
		interest = (0.01 * newBalance);
		newBalance += interest;
		std::cout << "Month " << month << ": " << "$" << newBalance << " deposited " << balance << " interest earned is " << interest << std::endl;
		month++;
	}

	std::cout << "It took " << month - 1 << " months, and you now have $" << newBalance << std::endl;

// May need something here to keep the console window open before program ends.
//  See http://www.cplusplus.com/forum/beginner/1988/ for suggestions.

return 0;
}


"doubles" are preferred more often than"floats" and a better choice.

Notice the change in line 28.

Your original program works, but some lines are not in the correct order.

Hope that helps,

Andy
Hello alexelliott83,

Using "iomanip" you can use this for line 24:

std::cout << "Month " << std::setw(2) << month << ": " << "$" << std::setw(7) << newBalance << " deposited " << balance << " interest earned is " << std::setw(5) << interest << std::endl;

It just gives a nicer looking output.

Andy
Hello alexelliott83,

After looking into ar2007's suggestion I found that it will work,but you will have to swap the order of lines 24 and 25, from my earlier message, and remove the "- 1" from line 28 for it to work.

Hope that helps,

Andy
Let's look at the output and be clear about what's getting printed:
- The month number. Duh.
- The "old balance" - the balance at the end of the previous month
- The amount deposited at the beginning of the month.
- The interest earned at the end of the month - 1% of (the old balance plus the deposit).

It's important to get all this straight before you start writing the code.

Here is code. Thanks to Handy Andy for the code to set the format. I have changed the "balance" variable to represent... the balance! Before it represented the deposit amount, which undoubtedly contributed to the confusion over the code. The new "deposit" variable represents the monthly deposit.
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
#include <iostream>
using namespace std;

int
main()
{
    double deposit = 0.0;
    int month = 0;
    double interest = 0.0;
    double balance = 0.0;
    
    cout << "How much do you want to save each month? " << flush;
    cin >> deposit;
    cout << fixed << showpoint;
    cout.precision(2);
    while (balance < 1000.00) {
	month++;
	interest = 0.01 * (balance + deposit); // interest earned at the end of the month
	cout << "Month " << month << ": " << "$" << balance << " deposited " << deposit
	    << " interest earned is " << interest << endl;
	balance += deposit + interest; // add deposit and interest to balance.
    }

    cout << "It took " << month << " months, and you now have $" << balance << endl;
    return 0;
}


Topic archived. No new replies allowed.