Compound Interest Program in C++


/*
   An Interesting Compound Interest Problem
*/

#include < iostream.h >
#include < conio.h >

const double rate = 0.08;

void firstten(double &, double &, double &, double &);
void nextyears(double, double, double, double);
void print (int, double, double, double, double);
void heading();

int main()
{


  double depben,        // benjamin's deposits
         balben,        // benjamin's balance
         depalbert,     // albert's deposits
         balalbert;     // albert's balance

  firstten(depben, balben, depalbert,balalbert);

  nextyears(depben, balben, depalbert,balalbert);
  getch();
  return(0);
}




// figures the amounts for the first ten years
void firstten (double &depben, double & balben, double & depalbert,
               double & balalbert)
{
  balben=0;
  balalbert=0;

  heading();
  for(int x = 1;x <= 10; x++)
  {
     depben=2000.00;
     balben= (depben + balben)+((balben+depben)* rate);
     depalbert=0;
     balalbert = (depalbert +balalbert) + ((depalbert +balalbert)* rate);
     print (x, depben, balben, depalbert,balalbert );
  }
  getch();
}


// figures the amounts for the last years
void nextyears (double depben, double balben, double depalbert, 
                double balalbert)
{
 heading();
 for(int x = 11; x<=55; x++)
  {
  depben=0;
  balben= (depben + balben)+((balben+depben)* rate);
  depalbert=2000.00;
  balalbert = (depalbert +balalbert) + ((depalbert +balalbert)* rate);
  print (x, depben, balben, depalbert,balalbert );
          if (x%10==0)
          	{
            getch();
            heading();

            }
  }

}

// prints each year's balances
void print (int x, double depben, double balben, double depalbert,
            double balalbert)
{

     cout.width(2);
     cout<< x;
     cout.setf(ios::fixed);
     cout.precision(2);
     cout.width(12);
     cout << depben;
     cout.setf(ios::fixed);
     cout.precision(2);
     cout.width(20);
     cout << balben;
     cout.setf(ios::fixed);
     cout.precision(2);
     cout.width(15);
     cout << depalbert;
     cout.setf(ios::fixed);
     cout.precision(2);
     cout.width(15);
     cout << balalbert;
     cout<< endl;
}


// prints the heading for the columns
void heading()
{
  clrscr();
  cout << "Year   Deposits- Ben   Balance Ben";
  cout << "   Deposits Albert     Balance Albert\n";
}