So, this is the little compound interest program i wrote that is due tommorow.
As far as I can tell it should be working, but it says something about an undefined reference? All help appreciated.

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main(int argc, char *argv[])

{
double computeInterest(double princ, double interest, double A, int time);
void printEarning(double A, double princ);
          char go='y';
double princ, interest, A, interestEarned;
bool repeat(char go);
int time;
int n=0, m=1;


    do{
             cout<<"Please enter the Principle Value:"<<endl;
cin>>princ;
cout<<"Please enter the interest rate in %:"<<endl;
cin>>interest;
cout<<"Please enter the amount of time in years:"<<endl;
cin>>time;
cout<<setw(5);
cout<< "Year\tBegin Balance\tInterest Earned\tEnding Balance"<<endl;
cout<< "----------------------------------------------------"<<endl;
A=princ;
             for (n=time;m<=time;++m)
             {
                 princ=A;
                 cout<<m<<" "<<princ<<"\t"<<endl;
                 computeInterest(princ, interest, A, time);
                 printEarning(A, princ);
                 
              
                 
             }
    cout<<"Repeat program? (Y)es/(N)o"<<endl;
    cin>>go;
             
             }while(go=='y'||go=='Y')

;    system("PAUSE");
    return EXIT_SUCCESS;
}

double computeInterest(int princ, double interest, double A, int time)
{
                    
A=princ*(1+interest);
A=pow(A, time);
A=A-princ;
return A;
}
void printEarning(double A, double princ)
{
              cout<<A<<"\t"<<A+princ<<endl;
              }

Could actually post that specific error message please. I presume it says 'undefined reference to *something*'

it says [Linker error] undefined reference to `computeInterest(double, double, double, int)'

Yes your function definition is inside main.

instead of:

int main(int argc, char *argv[])
{
    double computeInterest(double princ, double interest, double A, int time);
.
.
.
}

do

double computeInterest(double princ, double interest, double A, int time );

int main( int argc, char *argv[])
{
.
.
.
}

Edited 6 Years Ago by Valaraukar: n/a

This is because your forward declaration doesn't match the implementation.
Look here:

double computeInterest(double princ, double interest, double A, int time);
double computeInterest(int princ, double interest, double A, int time)
{
                    
A=princ*(1+interest);
A=pow(A, time);
A=A-princ;
return A;
}

In the implementation you are attempting to take an int as the first parameter and not a double as you declared.
Also, it is good programming practice to implement main last. This way you won't need to forward declare all of your functions.

This article has been dead for over six months. Start a new discussion instead.