I'm working on a problem to balance a checkbook. I've been struggling with it so I split it up into its components. I first made an array to store withdrawals. When I ran this program, everything ran fine. Now I'm trying to create a second array to store deposits. I can't get it to display properly.

//checkbookagain.cpp

#include <iostream>
using namespace std;

int main ()
{
    double with = 0;
    double *wAmt = new double[with];
    double dep = 0;
    double *dAmt = new double[dep];

    cout << "How many withdrawals were made? ";
    cin >> with;
    cout << "How many deposits were made? ";
    cin >> dep;

    for (int i=0; i<with; i++)
    {
        cout << "Enter in the amount of withdrawal " << i + 1 << ": $";
        cin >> wAmt[i];
    }// end for

    for (int j=0; i<dep; j++)
    {
        cout << "Enter in the amount of deposit " << j + 1 << ": $";
        cin >> dAmt[j];
    }//end for

    for (i=0; i<with; i++)
    {
        cout << "Withdrawal " << i + 1 << ": $";
        cout << wAmt[i] << endl;
    }//end for
    for (j=0; j<dep; j++)
    {
        cout << "Deposit " << j+1 << ": $";
        cout << dAmt[j] << endl;
    }//end for

    return 0;
}//end of main function

When its this way, it lets me input everything up to the deposits. When it gets to the deposit input, it just displays some numbers in scientific notation. I tried moving

cout << "How many deposits were made? ";
cin >> dep;

farther down after the loop for withdrawals, but that just made the program ask for more deposits than were inputted.

How to I organize these two arrays to get them to properly display? Thanks

Recommended Answers

All 6 Replies

Lines 8 to 11, 21, and 27.

You are creating arrays of size 0, then attempting to store information in those arrays of size 0, hence overflowing the array. If you don't get an actual seg fault, you can expect other problems. Seems like lines 9 and 11 shouldbe moved to below line 16. You don't know the size of the array needed until AFTER line 16, yet you reserver the memory BEFORE you know how much memory you need. Try moving lines 9 and 11 after line 16 and see if that solves anything.

Oh I see what you're saying. That makes sense. I moved 9 and 11 to after 16, but it still does the same thing where I can't input the deposit amounts.

Just noticed something. Your array size variables (with and dep) are doubles. They need to be positive integers. Array sizes always are. You can't have an array size of, say, 1.5, so the variables should be integers. Plus, i and j are integers and you want to go through the loop an integral number of times (i.e. it makes no sense to go through a look three and a half times. Make with and dep integers to reflect that.

Look at lines 18, 24, 30, and 35, your for-loop declarations. On two of them, you declare "int i" and "int j" inside the for-loop declaration. On two of them, you do not. Do it the same way for all of them. Either declare variables ABOVE the first for loop called i and j or declare "int i" and "int j" in all four. If you have the warnings turned on, the way you have it, you'll get some "obsolete binding" and "name lookup" / "scope" errors.

Good thing too because it calls your attention to an easy to miss error. Look closely at line 24. Really closely! See if you see the other error!

Line 24.. I use j to initialize but then do i<dep. Fixed that and the other stuff and it runs great now! Thanks so much.

One last thing.. I need to have the program show the balance after each transaction.. I got it to do the beginning, total deposits, total withdrawals and ending balance, but I'm having trouble getting it to add/subtract the transaction after each deposit/withdrawal.

What I tried to do was this:

    double *newBala = new double[with];
    ...
    for (i=0; i<with; i++)
    {
        newBala[i] = begBal - wAmt[i];
    }//end for  

All this is doing is adding the beginning balance to the most recent transaction but not making the transactions cumulative.

newBala[i] = begBal - wAmt[i];

Think about what you do when you make a deposit or withdrawal in real life.

Deposit -- Take the current balance, add the deposit, and you get the new current balance.
Withdrawal -- Take the current balance, subtract the withdrawal, and you get the new current balance.

Compare these operations with yours.

newBala[i] = begBal - wAmt[i];

If I am the teller and you've deposited 20 times and withdrawn 20 times and I need to figure out what your current balance is, do I care about what you started out with in the very beginning? Nope. I care about what you have right now. Hence begBal? Does that play anywhere in the description above? No. Get rid of it or rename it. curentBalance? That might be more like it. You could have a current balance variable and use that. That's be fine, but you are apparently going the array route. Fine. Just go with one or the other. So if I don't have a currentBalance variable, what's the current balance before the transaction. How about the last balance?

newBala[i] = newBala[i-1] - wAmt[i];

That might work. newBala[i-1] would be the LAST calulated balance. However, things get more complicated. You have new balances both before and after each transaction. That means both deposits and withdrawals. Hence your balance[] (perhaps a better name than newBala[]? Are all the balances really "new"?) array needs to be adjusted for each deposit OR withdrawal, which means it needs to be at least the size as the maximum number of deposits plus the maximum number of withdrawals.

Ok. I see what you're saying.

Trying to use the currentBal option, I coded this

    for (i=0; i<with; i++)
    {
        cout << "Enter in the amount of withdrawal " << i + 1 << ": $";
        cin >> wAmt[i];
        currentBal = currentBal - wAmt[i];
    }// end for


    for (j=0; j<dep; j++)
    {
        cout << "Enter in the amount of deposit " << j + 1 << ": $";
        cin >> dAmt[j];
        currentBal = currentBal + dAmt[j];
    }//end for
    ...
    cout << "***************************************************************"<< endl;
cout << setw(60) << currentBal<< endl;
for (i=0; i<with; i++)
    {
        cout << setw(20) << "$" << wAmt[i] << setw(30) << currentBal << endl;
    }//end for
for (j=0; j<dep; j++)
    {
        cout << "  $"<<  dAmt[j] << setw(30) << currentBal << endl;
    }//end for
cout << setw(60) << currentBal<< endl;
cout << "***************************************************************"<< endl;

The problem I'm having is it's not showing the current balance after each transaction, just the ending current balance. I went with the array because I thought that would show the current balance after each transaction, but the currentBal variable seems more logical if I could figure out how to get it to display each individual currentBal

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.