What is wrong with this for loop?

For an in class assignment we wrote a short program to practice pass by reference functions. Everything compiled and worked, but the final output interestEarned, is giving me a weird number. The teacher looked through it and we did a little bit of debugging but couldn't figure out what wasn't correct about the for loop. Any ideas? Even though I still got credit, this is driving me nuts not knowing whats wrong. 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
  /**
* @author ***** *****
* @file inClass08.cpp */

#include <iostream>
using namespace std;
bool findInterest(double, double, int, double &);

// write a function that will have FOUR parameters:
//	 a double	- amount invested
//	 a double	- annual compound interest rate
//	 an integer	- number of years saved
//	 a double	- total interest earned (pass-by-ref)


//  write a driver main to test your function
//  go to the web site  http://www.csgnetwork.com/compoundsavingsintcalc.html
//  to test your answers - you may not get the exact answer, but it should be close



int main()
{
double amountInvested, rate, interestEarned;
int years;

cout << "Enter amount invested\n";
cin >> amountInvested;

cout << "Enter anual interest rate \n";
cin >> rate;

cout << "Enter number of years Saved\n";
cin >> years;

if( findInterest(amountInvested, rate, years, interestEarned) )
cout << "The interest is " << interestEarned << endl;
else
cout << "The amount invested is invalid" << endl;

// system("pause"); 

return 0;  
} // end main


// - inside your function you will need
//	 - a double variable - totalSaved
//	 - totalSaved will start with invested
//	 - a for loop to count years
//	 - each cycle in the for loop
//	 totalSaved will be adjusted by interest earned

// the value returned in total interest earned will be totalSaved - invested

bool findInterest(double invested, double rate, int years , double & interest)
{

bool result = true;
if( invested > 0)
{
double totalSaved = invested;

for(int i=1; i <= years; i++)
{
totalSaved += totalSaved * rate;
}
interest = totalSaved - interest;

}
else
result= false;

return result;   // the function will return a bool - 
				//	 true if amount invested is positive
			    //	 false otherwise (interest cannot be calculated)
}

/*
Enter amount invested
100
Enter annual interest rate
.1
Enter number of years Saved
2
The interest is 9.25596e+061
Press any key to continue . . .
*/
Last edited on by admin
Your problem is you are using a variable before defining it. Specifically you are using double interestEarned; before giving it a value which will cause undefined behavior. I would really look for a better professor ;p.

Anyways lets step through the problem.

1) We define the interestEarned variable on line 24 but never assign a initial value to it.

double amountInvested, rate, interestEarned;

2) On line 36 we pass the interestEarned variable into the findInterest function by reference (So we are referring directly to the interestEarned variable in the function. And we still haven't given it a value.).

if( findInterest(amountInvested, rate, years, interestEarned) )

3) Line 68 is where the problem is happening.

interest = totalSaved - interest;

Now since interestEarned has never been assigned a value how can we assign to the interestEarned variable totalSaved - interest;?

This is undefined behavior and again your professor really should have caught this and I would really suggest trying to find a new class or even school (Joking ;p though it does concern me) if he couldn't find the mistake and still gave you credit even though the output was wrong.

Anyways hope that helps a bit if you have any more questions please feel free to ask. I would be glad to help.
Last edited on
CodeGazer,
That does make it more clear. Its the first day of this stuff and am still trying to get a handle on it. My next question would be where should I initialize it and what should I initialize it to?
where should I initialize it and what should I initialize it to?


I would initialize it right where you define it. So on or around line 24. As to what to initialize it to I would say 0 because you start out with 0 interestEarned.


Though you will still have a problem with your program even after you do that. The reason why is because you have a minor problem in the way your findInterest function works. The problem deals with the equation to find how much interest would be gain.

Lets walk through the loop and one line after it.

1
2
3
4
5
6
double totalSaved = invested;

for(int i=1; i <= years; i++)
{
totalSaved += totalSaved * rate;
}


So here we initialize the local totalSaved variable to what was passed in the invested parameter. In your example this value = 100.

Next we go through a loop the amount of years worth of interest we want to add. In your example this is 2 years and the rate is 0.1 . On each time through the loop we add the interest to our totalSaved variable. So

Before loop: totalSaved = 100;
Loop 1: totalSaved = 110;
Loop 2: totalSaved = 121;

Now we want to find out how much interest we have earned (I believe this is what you want correct?). What you are doing now is doing this.

interest = totalSaved - interest;

So if we initialized the interestEarned variable to 0 like suggested the equation would be this.

interest = 121 - 0;

As you can see that doesn't seem right. What you want to do instead is compare what the starting investment was (100) to what we have stored in the totalSaved variable (121) and that should give you the total interest that was earned (21).
Ah, Finally!

When you break it down like you did it's much easier to see where the errors were.

Thanks Again - Now that I have this corrected it will give me something to reference as I continue on with some other examples.
Topic archived. No new replies allowed.