Problems with 'while' loop.

I have included the code I have started along with the output and output expected. My first month keeps starting with my deposit amount of $100 when it is supposed to start with $0. If I do anything with the totAmount = totAmount + deposit line the program keeps timing out. At this point, I am unsure where to go with it. Any assistance is greatly appreciated.

Problem: Ask the user for an amount to save each month. Assuming a 1% per month interest rate, how many MONTHS will it take the user to save $1000. Add the savings at the END of each month.

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
#include <iostream>
using namespace std;

int main () {
   
float deposit = 0;
float totAmount = 0;
int months = 0;
 float savings = 0;

	cin >> deposit;
	cout << "How much do you want to save each month? " << deposit << endl;
   
	while( totAmount < 1000) {
		++months;	
		cout << "Month " << months;
		totAmount = totAmount + deposit;
		cout << ": $" << totAmount; 
		cout << " deposited " << deposit; 
		cout << " interest earned is ";
		cout << (totAmount * 0.01) << endl;
		totAmount = totAmount + (totAmount * 0.01);
	}
	
	cout << "It took " << months << " months, and you now have $";
        cout << totAmount << endl;
   
   
   return 0;
   }   


Output:
How much do you want to save each month? 100
Month 1: $100 deposited 100 interest earned is 1
Month 2: $201 deposited 100 interest earned is 2.01
Month 3: $303.01 deposited 100 interest earned is 3.0301
Month 4: $406.04 deposited 100 interest earned is 4.0604
Month 5: $510.1 deposited 100 interest earned is 5.101
Month 6: $615.201 deposited 100 interest earned is 6.15201
Month 7: $721.354 deposited 100 interest earned is 7.21354
Month 8: $828.567 deposited 100 interest earned is 8.28567
Month 9: $936.853 deposited 100 interest earned is 9.36853
Month 10: $1046.22 deposited 100 interest earned is 10.4622
It took 10 months, and you now have $1056.68

Expected Output:
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
Last edited on
What are lines 11/12 saying?
1
2
cin >> deposit;
	cout << "How much do you want to save each month? " << deposit << endl;


If you want to ask for how much to deposit its done like this:

1
2
3
cout << "How much would you like to deposit?" << endl;
cin >> deposit;
cout << "You would like to deposit  $" << deposit << "?" << endl;


line 14: while( totAmount < 1000) Its bad to compare float with int

line 17: totAmount = totAmount + deposit; you increase month++ at the first loop so month is now 1. Then you make the deposit add to the total amount which is why the first month has $100. To fix this you can do if(month == 1){deposit = 0;}
Last edited on
Why is it bad to compare float with int? All float is saying is that it will be more precise correct?

Also, how are you getting the sections you have to appear like code? I tried using the code tool and it didn't accomplish the goal. Sorry I am new to the forum and just figuring this out.
Last edited on
Why is it bad to compare float with int?
try this ...
1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
    float a = 3.14;
    int b = a;
    std::cout << b << "\n";//prints 3, not 3.14
}

so comparing, assigning float with int we're not doing the operation with like and like and dropping information along the way
use the <> button to the right of this panel
Last edited on
Ok, I follow that makes sense! Thanks pretty easy example.
and for edge cases the problem goes much beyond 3.14 printing as 3, etc: trying to cast to int float max() actually gives a negative number in at least a couple of implementations:
http://coliru.stacked-crooked.com/a/c4364d2eb25cb794
Changed the code to the following and everything is compiling like it should now. Thanks for the input!!!!

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
#include <iostream>
using namespace std;

int main () {
   
double deposit = 0;
double totAmount = 0;
double months = 0;
double savings = 0;

	cin >> deposit;
	cout << "How much do you want to save each month? " << deposit << endl;
   
	while( totAmount < 1000) {
	   ++months;
		cout << "Month " << months;
		cout << ": $" << totAmount; 
		cout << " deposited " << deposit; 
		cout << " interest earned is ";
		totAmount = totAmount + deposit;
		cout << (totAmount * 0.01) << endl;
		totAmount = totAmount + (totAmount * 0.01);
	}
	
	
	cout << "It took " << months << " months, and you now have $";
   cout << totAmount << endl;
   
   
   return 0;
   }


Output:
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
Topic archived. No new replies allowed.