while loop inside while loop

Hi everyone, I posted this on the General C++ board but I have a feeling that might have been the wrong place.

If this is a double post or not allowed please just delete.

--

I'm creating an Interest Compounding Calculator, and I'm trying to have it execute a while loop inside a while loop.

So far it will give you the interest and the yield from the highest possible interest rate (which the user inputs) and 1. I want it also to repeat this for every year leading upto the number of years the user wants it to run. so eg: if the user wants it to run for 5 years and an interest rate of 5.

I want it to have an output for year 5, and the yields for percents 5, 4, 3, 2, 1
Year 4 and the yields for percents 5, 4, 3, 2, 1
and etc.

I've set up the two while loops but only one of them runs.

Here's my code, thanks for your 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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

void welcome()
     {
     	cout << " Hello and Welcome to the Compounding Calculator\n"
	   		    " -----------------------------------------------\n"
		     	" Here you can calculate how much your investment will yield\n"
				" in various scenarios.\n"
				" Lets Get Started!" << endl;
		cout << "-------------------------------------------------\n";
		cout << " ***Please enter all values numerically***\n" << endl;
     }

void interestCalculation(double& totalValue, double& investmentInt, double& interest, double& length)
     {
    	
     while (interest>0)
		   {			
	        
            totalValue = ((investmentInt*(interest/100))+investmentInt)*length;
            cout << interest << "% = $" << totalValue << "   ";															
		    --interest;
           }
     cout << endl;   
     
     }

void investEval(double& investInt)
{
    		cout << " Please enter the amount of your initial investment: $";
			cin  >> investInt;
}

void interestEval(double& interestRate)
{
			cout << " Please enter the highest amount of interest you approximate you will get: ";
			cin  >> interestRate;
}

void lengthEval(double& length)
{
			cout << " For how many years do you want to calculate? " ;
			cin  >> length;
}
								

int investmentProgram()
{
	double s;
welcome();

while(true)
{
           char choice;
           double investment, interestR, n, value;
           
           investEval(investment);
           interestEval(interestR);
           lengthEval(n);
                  
           for (int x=n; x>0; --x)
           {
           interestCalculation(value, investment, interestR, n);
           cout << value;
           --n;
           }
        
           	
            
	cout << " Would you like to make another calculation?" << endl;
			cout << " Enter 'y' for yes and 'n' for no: ";
			cin >> choice;
			if (choice == 'y')
			{
				continue;
			}
			else 
			{
				break;
				cout << endl;
			}
		

}
}

int main()
{
	investmentProgram();
	
	system("pause");	
	
	return 0;	
}
I changed line 65 to
for (double x=n; x>0; --x)

and all loops were accessed.

There seems to be a problem with the program logic or the math, as the output does not look right for totalValue. If I put in 1000 10 10
your output shows 1% = $10100
and I think that should be 1100
changing that line to
totalValue = (((investmentInt*(interest/10))*length)+investmentInt);
Looks right to me but you be the judge.

The next problem i see if you put in 100 10 1 the output looks good now
but if you put in 2 years, the interest would be on 110 not on the initial 100.

What I would do now is put in more cout commands to dump each value before and after you change it, to see where the problem lies.

PS. you have no comments, it's hard to tell what you want the program to do, what the values represent and so forth. I may have completely screwed up having to guess at what it's doing but I hope this helps.

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

void welcome()
     {
     	cout << " Hello and Welcome to the Compounding Calculator\n"
	   		    " -----------------------------------------------\n"
		     	" Here you can calculate how much your investment will yield\n"
				" in various scenarios.\n"
				" Lets Get Started!" << endl;
		cout << "-------------------------------------------------\n";
		cout << " ***Please enter all values numerically***\n" << endl;
     }

void interestCalculation(double& totalValue, double& investmentInt, double& interest, double& length)
     {
    	
     while (interest>0)
		   {			
	        cout << " --Test1" << endl;  // Test
            totalValue = (((investmentInt*(interest/10))*length)+investmentInt);
            cout << interest << "% = $" << totalValue << "   ";															
		    --interest;
           }
     cout << endl;   
     }

void investEval(double& investInt)
{
    		cout << " Please enter the amount of your initial investment: $";
			cin  >> investInt;
}

void interestEval(double& interestRate)
{
			cout << " Please enter the highest amount of interest you approximate you will get: ";
			cin  >> interestRate;
}

void lengthEval(double& length)
{
			cout << " For how many years do you want to calculate? " ;
			cin  >> length;
}
								

int investmentProgram()
{
double s;
welcome();

while(true)
{
           cout << " --Test2" << endl;  // Test
           char choice;
           double investment, interestR, n, value;

           investEval(investment);
           interestEval(interestR);
           lengthEval(n);
                  
           for (double x=n; x>0; --x)
           {
	        cout << " --Test3" << endl;  // Test
           interestCalculation(value, investment, interestR, n);
           cout << value;
           --n;
           }
            
	cout << " Would you like to make another calculation?" << endl;
			cout << " Enter 'y' for yes and 'n' for no: ";
			cin >> choice;
			if (choice == 'y')
			{
				continue;
			}
			else 
			{
				break;
				cout << endl;
			}
}
}

int main()
{
	investmentProgram();
//	system("pause");	
	return 0;	
}
Last edited on
Ok I made a mistake and see what it is now

Your statement :
totalValue = ((investmentInt*(interest/100))+investmentInt)*length;
is correct for the first year, DO NOT USE MINE above.

However the Length part is incorrect, assuming length is # of years, that is wrong. You'll have to take total value and multiply it for each year, or maybe each month depending on if the interest is paid monthly, quarterly or yearly.

It might look like this
1
2
3
4
5
6
// for (int numcycles=0; numcycles <= length*12; numcycles++) // for months
for (int numcycles=0; numcycles <= length; numcycles++)  // for years
{
// calculate interest each interest cycle based on new totalvalue + interest
totalValue = ((investmentInt*(interest/100))+investmentInt)
}
Last edited on
Thank you thank you.

I realized I hadn't put comments. I was just working in my head and wanted to jot it all down.

you were really helpful, i combined your ideas, and what I figured out the problem was. The reason it was only displaying the outputs for the first year, was because the interest rate was then 0. It needed to reset for every year so that each year it has all the possible interest rate yields.

I've accomplished that below, combining your for loop and my while loop.

Thanks again.

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

using namespace std;

int main()
{

double investmentLong, value, n, interest, investment, c, interestTemp;

    		cout << " investment: $";
			cin  >> investment;

			cout << " interest";
			cin  >> interest;

			cout << " n? " ;
			cin  >> n;

//setting starting year as 1
c=1;

//setting interestTemp as equal to interest
interestTemp=interest;

//for loop for each yeah, starting at 1 leading upto, user inputed max years of 'n'
for (; c<=n; c++)
{		
cout << "In year " << c << "your yields will be: " << endl;

//function determing interest for each value counting down from user inputed value of 'interest'
while(interestTemp>0)
{
value=((investment*(interestTemp/100))+investment);
cout << interestTemp << "% = $" << value;
--interestTemp;
}
cout << endl;

//setting the new investment for the next year as the total value of the current year
investment = value;

//resetting the interest rate to the initial user input.
if (interestTemp==0)
{
interestTemp=interest;
}
else
{
}

}

system("pause");	

return 0;	
    
}
The concept it self works, but I'm not going to be able to do it the way I wanted. For the math part of it, its only giving me year 2 with only one of the gains. Mathematically it'd need to produce data squared each time so 5^2 for the first year. 5% gain first year, 5% gain second year, 5 first, 4 second etc.

but thanks again I get the logic of it.
Last edited on
Great, I put a link to this post from the one in General C++, so people know that it's being worked on here.
Topic archived. No new replies allowed.