Erronous output from program using derived classes. Not for the light of heart
<p>Note: I apologize if this program is painful to look at. this is my first program using derived classes. I plan on submitting it to code review after I get it to do what I want so I can recieve tips on how it could have been designed better. Feel free to provide those tips here but my main intention for this post is to get it to work correctly.</p>

<p>This program processes withdrawals and deposits for bank accounts and then applies an interest rate.</p>

<p><strong>the problem:</strong> For some reason it seems like the value of checkingBalance gets stuck and does not update to a new value throughout the rest of the loop when processing the accounts of class type "checkingAccount". It is odd because the same loop is used to process the other derived class "savingsAccount" and it works fine and one of accounts of type "checkingAccount" even seems to have been processed correctly so I am completely bamboozled as to what is going on. </p>

<p>If anyone could lend a hand and help me resolve this issue it would be greatly appreciated. Please do not hesitate to ask me to specify anything, I will respond asap.</p>

<p><strong>the data being read:</strong></p>

<p><img src="http://i.stack.imgur.com/n5V1D.png" alt="enter image description here"></p>

<p><strong>The output:</strong>
<img src="http://i.stack.imgur.com/HGxe8.png" alt="enter image description here"></p>

<p><strong>The code I suspect the issue to be in:</strong></p>

<pre><code>//This loop is exited early:

 for (int j = 0; j &lt; transactionNum; j++)
            {
                inFile &gt;&gt; transactionTypeTemp &gt;&gt; amountTemp;
                inFile.get(discard);
                ba.applyTransaction(accountType, transactionTypeTemp, amountTemp, j);
            }

//when something in here occurs?
// c = j from loop

double checkingAccount:: applyTransaction(char transactionTypeTemp, int amountTemp, int c, double checkingBalance)
{
  if (transactionTypeTemp == 'D')
        {
            checkingBalance = checkingBalance + amountTemp;
        }
  else if (transactionTypeTemp == 'W')
        {
            if (checkingBalance &lt; amountTemp)
            {
            cout &lt;&lt; "error: transaction number " &lt;&lt; c + 1 &lt;&lt; " never occured due to insufficent funds." &lt;&lt; endl;
            }
            else
            {
                checkingBalance = checkingBalance - amountTemp;
                if(checkingBalance &lt; minimumBalance) //if last transaction brought the balance below minimum balance
                {
                    checkingBalance = (checkingBalance - serviceCharge); //apply service charge
                }
            }
        }

        return checkingBalance;
}
</code></pre>

<p><strong>the whole program:</strong></p>

<pre><code>//header file
#include &lt;iostream&gt;
#include &lt;fstream&gt;


using namespace std;

class bankAccount
{
    public:
    bankAccount();
    void setAccountInfo(int accountNumTemp, double balanceTemp);
    void prePrint(char accountType);
    void applyTransaction(char accountType, char transactionTypeTemp, int amountTemp, int j);
    void applyInterest(char accountType);
    void postPrint();

    private:
    int accountNumber;
    double balance;
};

class checkingAccount: public bankAccount
{
    public:
    void prePrint(int accountNumber, char accountType, double checkingBalance);
    checkingAccount();
    double  applyTransaction(char transactionTypeTemp, int amountTemp, int c, double    checkingBalance);
    double  applyInterest(double checkingBalance);

    private:
    float interestRate;
    int minimumBalance;
    float serviceCharge;

};

class savingsAccount: public bankAccount
{
    public:
    void prePrint(int savingsAccountNumber, char accountType, double savingsBalance);
    savingsAccount();
    double applyTransaction(char transactionTypeTemp, int amountTemp, int c, double savingsBalance);
    double applyInterest(double checkingBalance);

    private:
    float interestRate;
};

//class implementation .cpp file

bankAccount:: bankAccount()
{
    accountNumber = 0;
    balance = 0;
}

void bankAccount:: setAccountInfo(int accountNumTemp, double balanceTemp)
{
    accountNumber = accountNumTemp;
    balance = balanceTemp;
}

void bankAccount:: prePrint(char accountType)
{
    if(accountType == 'C')
    {
        int checkingAccountNumber = accountNumber;
        double checkingBalance = balance;
        checkingAccount ca;
        ca.prePrint(checkingAccountNumber, accountType, checkingBalance);
    }
    else if (accountType == 'S')
    {
        int savingsAccountNumber = accountNumber;
        double savingsBalance = balance;
        savingsAccount sa;
        sa.prePrint(savingsAccountNumber, accountType, savingsBalance);
    }


}

void bankAccount:: applyTransaction(char accountType, char transactionTypeTemp, int amountTemp, int j)
{
    int c;
    c = j;
    double checkingBalance, savingsBalance;
    checkingAccount ca;
    savingsAccount sa;

        if (accountType == 'C')
        {
            checkingBalance = balance;
            balance = ca.applyTransaction(transactionTypeTemp, amountTemp, c, checkingBalance);
        }
        else if (accountType == 'S')
        {
            savingsBalance = balance;
            balance = sa.applyTransaction(transactionTypeTemp, amountTemp, c, savingsBalance);
        }

}

void bankAccount:: applyInterest(char accountType)
{
    double checkingBalance, savingsBalance;
    checkingAccount ca;
    savingsAccount sa;

    if (accountType == 'C')
    {
    checkingBalance = balance;
    balance = ca.applyInterest(checkingBalance);
    }
    else if (accountType == 'S')
    {
    savingsBalance = balance;
    balance = sa.applyInterest(savingsBalance);
    }


}

void bankAccount:: postPrint()
{
   cout &lt;&lt; "Balance after processing: " &lt;&lt; balance &lt;&lt; endl;
}

checkingAccount:: checkingAccount()
{
    interestRate = .02;
    minimumBalance = 500;
    serviceCharge = 20;
}

void checkingAccount:: prePrint(int checkingAccountNumber, char accountType, double checkingBalance)
{
    cout &lt;&lt; "Account Number:" &lt;&lt; checkingAccountNumber &lt;&lt; " account type:" &lt;&lt; accountType &lt;&lt; " Starting Balance:" &lt;&lt; checkingBalance &lt;&lt; endl;
}

double checkingAccount:: applyTransaction(char transactionTypeTemp, int amountTemp, int c, double checkingBalance)
{
  if (transactionTypeTemp == 'D')
        {
            checkingBalance = checkingBalance + amountTemp;
        }
  else if (transactionTypeTemp == 'W')
        {
            if (checkingBalance &lt; amountTemp)
            {
            cout &lt;&lt; "error: transaction number " &lt;&lt; c + 1 &lt;&lt; " never occured due to insufficent funds." &lt;&lt; endl;
            }
            else
            {
                checkingBalance = checkingBalance - amountTemp;
                if(checkingBalance &lt; minimumBalance) //if last transaction brought the balance below minimum balance
                {
                    checkingBalance = (checkingBalance - serviceCharge); //apply service charge
                }
            }
        }

        return checkingBalance;
}

double checkingAccount:: applyInterest(double checkingBalance)
{
    checkingBalance = (checkingBalance + (checkingBalance * interestRate));
    return checkingBalance;
}
savingsAccount:: savingsAccount()
{
    interestRate = .04;
}


void savingsAccount:: prePrint(int savingsAccountNumber, char accountType, double savingsBalance)
{
    cout &lt;&lt; "Account Number:" &lt;&lt; savingsAccountNumber &lt;&lt; " account type:" &lt;&lt; accountType &lt;&lt; " Starting Balance:" &lt;&lt; savingsBalance &lt;&lt; endl;
}

double savingsAccount:: applyTransaction(char transactionTypeTemp, int amountTemp, int c, double savingsBalance)
{
  if (transactionTypeTemp == 'D')
        {
            savingsBalance = savingsBalance + amountTemp;
        }
  else if (transactionTypeTemp == 'W')
        {
            if (savingsBalance &lt; amountTemp)
            {
            cout &lt;&lt; "error: transaction number" &lt;&lt; c + 1 &lt;&lt; " never occured due to insufficent funds." &lt;&lt; endl;
            }
            else
            {
                savingsBalance = savingsBalance - amountTemp;     //apply transaction
            }
        }

        return savingsBalance;
}

double savingsAccount:: applyInterest(double savingsBalance)
{
    savingsBalance = (savingsBalance + (savingsBalance * interestRate));
    return savingsBalance;
}

//main .cpp file
int main()
{
    ifstream inFile;
    int numberOfAccounts, accountNumTemp, transactionNum, amountTemp;
    double balanceTemp;
    char discard, accountType, transactionTypeTemp;
    bankAccount ba;

    cout &lt;&lt; "Processing account data..." &lt;&lt; endl;

    inFile.open("Bank.txt");

    if (!inFile)
    {
        for  (int a = 0; a &lt; 20; a++)
            cout  &lt;&lt; endl;
        cout &lt;&lt; "Cannot open the input file."
             &lt;&lt; endl;
            return 1;
    }

    inFile &gt;&gt; numberOfAccounts;
    inFile.get(discard);

    for (int i = 0; i &lt; numberOfAccounts; i++)
    {
            inFile &gt;&gt; accountNumTemp &gt;&gt; accountType &gt;&gt; balanceTemp &gt;&gt; transactionNum;
            inFile.get(discard);
            ba.setAccountInfo(accountNumTemp, balanceTemp);
            ba.prePrint(accountType);

            for (int j = 0; j &lt; transactionNum; j++)
            {
                inFile &gt;&gt; transactionTypeTemp &gt;&gt; amountTemp;
                inFile.get(discard);
                ba.applyTransaction(accountType, transactionTypeTemp, amountTemp, j);
            }
            ba.applyInterest(accountType);
            ba.postPrint();
    }


    inFile.close();

    return 0;
}
</code></pre>

<p>Here is the input in copy-and-pastable form so that others don't have to type it in:</p>

<pre><code>6
35231 C 500 3
W 250
W 200
W 100
46728 S 2700 2
D 250
W 100
87324 C 500 3
D 300
W 100
W 150
79873 S 800 0
89932 C 3000 2
W 1000
W 1500
98322 C 750 6
D 50
W 75
W 100
W 25
W 30
W 75
</code></pre>
