Compound interest function

Hello guys!

I am writing a function that calculates a compound interest. It takes 3 parameters.

But I have some problem: "term does not evaluate to a function taking 1 arguments" in lines 22 and 40.

In line 59: 'initializing' : truncation from 'double' to 'float'

Note that I have two functions that do the exact same thing but one of them have the first parameter passed-by-reference and the other passed-by-value.

What is that am I doing wrong?

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

using namespace std;

/* float  calcInterest1 ( the first argument for the principal, 
the second argument for the number of years of deposit, 
the third argument for the annual interest). */

/* A = P ( 1 + (r/n) ) ^(n * t) 

P = principal amount (the initial amount you borrow or deposit)
r  = annual rate of interest (as a decimal)
t  = number of years the amount is deposited or borrowed for.
A = amount of money accumulated after n years, including interest.
n  =  number of times the interest is compounded per year */

float calcInterest1 ( double P, int N, float A )
	{

	float compoundInterest;

	compoundInterest = P ( 1 + A ) ^ (N);
	
	cout << "Compound Interest Is: " << compoundInterest;

	return 0.0;

	}

/* float  calcInterest2 ( the first argument for the principal, 
the second argument for the number of years of deposit, 
the third argument for the annual interest)
*/

float calcInterest2 ( double &P, int N, float A)
	{
	
	float compoundInterest;

	compoundInterest = P ( 1 + A ) ^ (N);
	
	cout << "Compound Interest Is: " << compoundInterest;

	return 0.0;
	
	}

/* In the main routine, you consider the following test case:
Principal = $200,000.00
Year to save = 10 years
Annual Interest = 3%
 */

int main()
	{

	double P = 200000.00;
	int N = 10;
	float A = 0.03;

	calcInterest1(P, N, A);
	calcInterest2(P, N, A);

	system("pause");
	return 0;

	}
closed account (48T7M4Gy)
Try using the pow( ... ) function instead of ^

http://www.cplusplus.com/referencjavascript:editbox1.editSend()e/cmath/pow/?kw=pow
Ok, I fixed the code but same problems are there.

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

using namespace std;

/* float  calcInterest1 ( the first argument for the principal, 
the second argument for the number of years of deposit, 
the third argument for the annual interest). */

/* A = P ( 1 + (r/n) ) ^(n * t) 

P = principal amount (the initial amount you borrow or deposit)
r  = annual rate of interest (as a decimal)
t  = number of years the amount is deposited or borrowed for.
A = amount of money accumulated after n years, including interest.
n  =  number of times the interest is compounded per year */

float calcInterest1 ( double P, int N, float A )
	{

	float compoundInterest;

	compoundInterest = (pow(P(1+A), N));
	
	cout << "Compound Interest Is: " << compoundInterest;

	return 0.0;

	}

/* float  calcInterest2 ( the first argument for the principal, 
the second argument for the number of years of deposit, 
the third argument for the annual interest)
*/

float calcInterest2 ( double &P, int N, float A)
	{
	
	float compoundInterest;

	compoundInterest = (pow(P(1+A), N));
	
	cout << "Compound Interest Is: " << compoundInterest;

	return 0.0;
	
	}

/* In the main routine, you consider the following test case:
Principal = $200,000.00
Year to save = 10 years
Annual Interest = 3%
 */

int main()
	{

	double P = 200000.00;
	int N = 10;
	float A = 0.03;

	calcInterest1(P, N, A);
	calcInterest2(P, N, A);

	system("pause");
	return 0;

	}
Last edited on
closed account (48T7M4Gy)
Check that line 22 is a valid function. Remember multiplication is by way of a '*'

You also need to #include ... to enable use of the pow function.

Check the brackets on line 22 also.
Last edited on
closed account (48T7M4Gy)
I also just notice your formula at line 9 has 4 variables and yours functions only have 3.
I fixed the pow() function and included <math.h> for it.

Anyways there are 3 errors in lines: 23, 41, and 60

(23): conversion from 'double' to 'float', possible loss of data
(41): conversion from 'double' to 'float', possible loss of data
(60): 'initializing' : truncation from 'double' to 'float'

Do I have to make the function only calculates float values or double values? I think the problem is that I am not allowed to use two different data types in one function?

Edit: My function has only 3 variables is because the compound interest that I am calculating is only once a year which is an annual compound interest so the n = 1. So I didn't bother using 4th variable since I have a one test case.

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>
#include <math.h> //pow();

using namespace std;

/* float  calcInterest1 ( the first argument for the principal, 
the second argument for the number of years of deposit, 
the third argument for the annual interest). */

/* A = P ( 1 + (r/n) ) ^(n * t) 

P = principal amount (the initial amount you borrow or deposit)
r  = annual rate of interest (as a decimal)
t  = number of years the amount is deposited or borrowed for.
A = amount of money accumulated after n years, including interest.
n  =  number of times the interest is compounded per year */

float calcInterest1 ( double P, int N, float A )
	{

	float compoundInterest;

	compoundInterest = (pow(P*(1+A), N));
	
	cout << "Compound Interest Is: " << compoundInterest << endl;

	return 0.0;

	}

/* float  calcInterest2 ( the first argument for the principal, 
the second argument for the number of years of deposit, 
the third argument for the annual interest)
*/

float calcInterest2 ( double &P, int N, float A)
	{
	
	float compoundInterest;

	compoundInterest = (pow(P*(1+A), N));
	
	cout << "Compound Interest Is: " << compoundInterest << endl;

	return 0.0;
	
	}

/* In the main routine, you consider the following test case:
Principal = $200,000.00
Year to save = 10 years
Annual Interest = 3%
 */

int main()
	{

	double P = 200000.00;
	int N = 10;
	float A = 0.03;

	calcInterest1(P, N, A);
	calcInterest2(P, N, A);

	system("pause");
	return 0;

	}


Edit: fixed the problems. Now the output gives me INF which means infinity. Where did I divide a number by 0? lol.

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
#include <iostream>
#include <math.h> //pow();

using namespace std;

/* float  calcInterest1 ( the first argument for the principal, 
the second argument for the number of years of deposit, 
the third argument for the annual interest). */

/* A = P ( 1 + (r/n) ) ^(n * t) 

P = principal amount (the initial amount you borrow or deposit)
r  = annual rate of interest (as a decimal)
t  = number of years the amount is deposited or borrowed for.
A = amount of money accumulated after n years, including interest.
n  =  number of times the interest is compounded per year */

float calcInterest1 ( double P, double N, double A )
	{

	float compoundInterest;

	compoundInterest = (pow(P*(1+A), N));

	cout << "--------" << "Compound Interest for the Function <calcInterest1>" << "--------" << endl
		<< endl;
	
	cout << "Compound Interest Is: " << compoundInterest << endl
		<< endl;

	return 0.0;

	}

/* float  calcInterest2 ( the first argument for the principal, 
the second argument for the number of years of deposit, 
the third argument for the annual interest)
*/

float calcInterest2 ( double &P, double N, double A)
	{
	
	float compoundInterest;

	compoundInterest = (pow(P*(1+A), N));

	cout << "--------" << "Compound Interest for the Function <calcInterest2>" << "--------" << endl
		<< endl;
	
	cout << "Compound Interest Is: " << compoundInterest << endl
		<< endl;

	return 0.0;
	
	}

/* In the main routine, you consider the following test case:
Principal = $200,000.00
Year to save = 10 years
Annual Interest = 3%
 */

int main()
	{

	double P = 200000.00;
	int N = 10;
	double A = 0.03;

	calcInterest1(P, N, A);
	calcInterest2(P, N, A);

	system("pause");
	return 0;

	}
Last edited on
closed account (48T7M4Gy)
Yeah you get the INF error, so did I until I realised you are trying to raise P (the principal) to the same power which is wrong. You need to look nat the supplied formula carefully and modify your code on that line. You'll get it, you got this far which is quite a long way :-)
I looked at the formula and it looked just fine to me. I don't know where the flow is.
closed account (48T7M4Gy)
You have to look much harder :)

compoundInterest = (pow(P*(1+A), N)) is what you have. But that isn't the same functionally as what you were given, which is A = P ( 1 + (r/n) ) ^(n * t)

Your function means A = (P ( 1 + (r/n) )) ^(n * t) which is dramatically different and hence your INF.

Check your brackets!
Ok now it worked!

I found out that I am missing a ")" bracket. For some reason, when I added it to my formula it gave me some error earlier so I thought it's an extra one.

Anyways, thank you so much.

Edit: Just for the sake of knowledge, why do you think our professor gave us two functions to write, and one of them has its first parameter passed-by-reference?
Last edited on
closed account (48T7M4Gy)
I would say it is to demonstrate how passing by reference allows you to 'send' the actual variable to the function to enable the function to modify it (them) rather than working on a new copy which gets destroyed when the function returns.

With a bit of experience you'll see that they are both appropriate depending on the circumstances. Also by reference allows more than one variable to be modified if that's what is required.

In this case you get the expected same answer both ways, which you would be dramatically (again) surprised if it didn't.
Last edited on
I got this number as a result: 206000 on both functions.

The problem is, this is not the right answer.

I think there maybe some lost data because I got warnings:

source.cpp(24): warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
source.cpp(47): warning C4244: '=' : conversion from 'double' to 'float', possible loss of data

I know I did something silly in the data types.
closed account (48T7M4Gy)
Make all the variable double (or float) and see what happens. The warning is alerting you to possible conversion errors. float doesn't have the same numerical range as double.

Depending how big the numbers get then there's no problem but you can't say you weren't warned :-)

I changed all of them to double, it gave me the same number. And the same result when I changed everything to float.

<.<
closed account (48T7M4Gy)
Did you get any warnings?
When I have all float, these warnings that I get:

source.cpp(72): warning C4305: 'initializing' : truncation from 'double' to 'float'
source.cpp(74): warning C4305: 'argument' : truncation from 'double' to 'float'
source.cpp(75): warning C4305: 'argument' : truncation from 'double' to 'float'

When I change them all to doubles. Same warnings.
closed account (48T7M4Gy)
Ah well, as long as it works. I suspect you need to make sure all data types are compatible across the whole program including functions and prototypes. ie change all floats to double, leave ints as is.

But I'll leave it there.

Cya
All the best
Topic archived. No new replies allowed.