Hi, I am in a programming class and I have an error in my code that I can't fix.

Here is the task: The future value of a bank deposit is computed using the following:


Find the future value of a bank deposit

p principal, the amount deposited
r annual rate of interest
c number of times interest is compounded per year
n number of years
i interest rate per period
m total number of times interest is compounded
i = r / c
m = c * n
balance = p * ((1 + i))



Write a computer program using functions to:

a) Get p, r, c and n from the user
b) Calculate the value of i, m and balance
c) Display the results on the screen
d) Please make sure that you utilize the creation of your own class or class template in your code

Sample Output:

This program will calculate the future value of a bank deposit given the initial deposit, the annual interest rate, the number of times per year interest is compounded, and the number of years until maturity

Enter your initial deposit
100
Enter the annual interest rate
5
Enter the number of time interest is compounded per year
4
Enter the number of years before maturity
5
for an initial deposit of $100 at 5%
In 5 years you will have $128.204

Write your codes here:

#include<iostream>
using namespace std;

float interestfunc(float r, float c, float interest);
int compoundfunc(float c, int n, int compound);
float balancefunc(float p, float interest, float balance);

int main()
{
int n, compound;
float p, interest, c, r, balance;

cout << "Enter your initial deposit: ";
cin >> p;
cout << "Enter the annual interest rate: ";
cin >> r;
cout << "Enter the number of time interest is compounded per year: ";
cin >> c;
cout << "Enter the number of years before maturity: ";
cin >> n;

interest = interestfunc(r, c, interest);
compound = compoundfunc(c, n, compound);
balance = balancefunc(p, interest, balance);

cout << "Your interest rate per period is: " << interest << endl;
cout << "The number of times interest is compounded: " << compound << endl;
cout << " Your balance is: " << balance << endl;

return 0;
}

float interestfunc()
{
float r, c, interest;
interest = r / c;

return interest;
}

int compoundfunc()
{
float c;
int n, compound;
compound = c * n;

return compound;
}

float balancefunc()
{
float p, interest, balance;
balance = p * ((1 + interest)); <- //Here is where the error is supposedly located.

return balance;
}

When I wrote the code above I intended to use a function prototype but at first failed to carry the variables in each function. Now I receive an error that says "unresolved external link" something like that. I ran into an issue like this before but I never found a solution, anyone mind giving me their opinion?
Please use code wrap ([ code][ /code]) and indent your code.
I am sorry but this is my first programming class and I have never heard and learned what you said to use. What do you mean?
press the edit button and put the code in code tags.

EX):

float c;
blah;

->

[.code]
float c;
blah;
[./code]

of course without the dots in the brackets. Makes reading code a lot easier.
I don't know what kind of error is that so try with another compiler. Btw what compiler are you using?
Your function definitions don't match the declarations in the prototypes.

That is to say that your prototypes have parameters and your definitions don't.

And, to reiterate previous posts, please use code tags. It makes it so much easier to spot errors.
Okay here now I see the problem:
#include<iostream>
using namespace std;

float interestfunc(float r, float c, float interest);
int compoundfunc(float c, int n, int compound);
float balancefunc(float p, float interest, float balance);

int main()
{
int n, compound;
float p, interest, c, r, balance;

cout << "Enter your initial deposit: ";
cin >> p;
cout << "Enter the annual interest rate: ";
cin >> r;
cout << "Enter the number of time interest is compounded per year: ";
cin >> c;
cout << "Enter the number of years before maturity: ";
cin >> n;

interest = interestfunc(r, c, interest);
compound = compoundfunc(c, n, compound);
balancefunc(p, interest, balance);

cout << "Your interest rate per period is: " << interestfunc(r, c, interest) << endl;
cout << "The number of times interest is compounded: " << compoundfunc(c, n, compound) << endl;
cout << " Your balance is: " << balancefunc(p, interest, balance) << endl;

return 0;
}

float interestfunc(float r, float c, float interest)
{
interest = r / c;

return interest;
}

int compoundfunc(float c, int n, int compound)
{
compound = c * n;

return compound;
}

float balancefunc(float p, float interest, float balance)
{
balance = p * ((1 + interest));

return balance;
}

But there seems to be another issue. When I debug the code an more than one debug error pops up saying that I am using the interest, compound, and balance without initializing them. When I click ignore on all of them the output works fine.
I just ran your program through my own IDE and it ran perfectly, What type of IDE or compiler are you using?

I'm using CODEBLOCKS and it likes your program just fine.
That is because you haven't initialized them and you are passing them as parameters to a function. You are passing them by value, which basically tells the compiler this is a read operation, you haven't initialized them so it is warning you of that. Either initialize them or change your functions such that you don't pass them in (because really it doesn't make sense to pass a parameter you are not going to use).

e.g.
1
2
3
4
5
float interestfunc(float r, float c)
{
   float interest = r / c; //Notice:  declaring local float variable interest, exists in this function only.
   return interest;
}


Then you can change your call site to this:
float interest = interestfunc(r, c); //Moved the declaration down to this line (in effect initializing as well...

Also, the names of your variables inside your function do not have to match the name of your interest variable outside the function scope, they are two entirely different variables.
Last edited on
The unresolved external whatever is an error that occurs in Visual C++6.0 Enterprise for me all the time. Just click rebuild instead of build, and it'll go around it. I do not know why.
Unresolved External means that the linker has gone looking for something and cannot find it. Generally, it means you have not linked to the right library, or you have not compiled some of your code, or you have compiled it but not linked to it. It is not a compiler error; the code compiled, it just relies on something that the linker cannot find. In MS land, this often translates to "didn't put it in your project" or "didn't add the right library path" or "didn't add the right library".

In this case, the linker went looking for a function that didn't exist, because you got the parameters wrong.
Last edited on
Topic archived. No new replies allowed.