1

Hey, My code after I execute it says that "accounts" has become corrupted...

What does this mean and how do I fix it?

#include <iostream>
#include <iomanip>

using namespace std;

class BankAccount
{
private:
    int accountNum;
    double accountBal;
    const static double annualIntRate;
public:
    void enterAccountData(int, double);
    //void computeInterest();
    //void displayAccount();
};

// implementation section:
//const double BankAccount::annualIntRate = 0.03;
void BankAccount::enterAccountData(int number, double balance)
{
    cout << setprecision(2) << fixed;
    accountNum = number;
    accountBal = balance;

    cout << "Enter the account number " << endl;
    cin >> number;

    while(number < 0 && number <= 999)
    {
        cout << "Account numbers cannot be negative or less than 1000 " <<
                "Enter a new account number: " << endl;
        cin >> number;
    }

    cout << "Enter the account balance " << endl;
    cin >> balance;

    while(balance < 0)
    {
        cout << "Account balances cannot be negative. " <<
                "Enter a new account balance: " << endl;
        cin >> balance;
    }
    return;
}
/*void BankAccount::computeInterest()
{

}*/

int main()
{
    const int QUIT = 0;
    const int MAX_ACCOUNTS = 10;
    int counter;
    int input;
    int number = 0;
    double balance = 0;

    BankAccount accounts[MAX_ACCOUNTS];
    //BankAccount display;
    do
    {
        counter = 0;
        accounts[MAX_ACCOUNTS].enterAccountData(number, balance);
        cout << " Enter " << QUIT << " to stop, or press 1 to proceed.";
        cin >> input;
        counter++;
    } while(input != QUIT && counter != 10);

    system("pause");
    return 0;
}
6502
  • 112,025
  • 15
  • 165
  • 265
Alex
  • 113
  • 1
  • 7

2 Answers2

4
accounts[MAX_ACCOUNTS].enterAccountData(number, balance);

Presumably you mean:

accounts[counter].enterAccountData(number, balance);

accounts[MAX_ACCOUNTS] doesn't exist: the accounts array has MAX_ACCOUNTS elements in it, and they are indexed starting at zero. You also need to move the initialization of counter outside of the loop, otherwise you aren't really counting anything at all!

Note also that your input handling is incorrect. If you were to type in hello when prompted for a new account number, your program would stop functioning because you don't check the state of the stream to ensure that the extraction succeeded. Idiomatic input operations in C++ look something like this:

int number;
if (!(std::cin >> number)) {
    // o noez! The user didn't type in a number at all! Handle the error here...
}

When handling the error, if you want to continue reading from the stream, you'll need to use its clear() member function to reset the state flags and its ignore() member function to skip over the invalid input.

When handing console input, it is often easier to always read a string into a std::string and then parse the string using a std::stringstream; this allows for simpler, more straightforward error handling, since you don't have to deal with clearing invalid characters out of std::cin, you can just throw away the std::stringstream and start over easily.

Community
  • 1
  • 1
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • I think so because my compiler only tells me "accounts" – Alex Mar 12 '11 at 18:08
  • @Alex: Actually your debugger tells you that. Precise output from your debugger would have been better. You're overflowing the array `accounts` by writing one-past-the-end. – Lightness Races in Orbit Mar 12 '11 at 18:08
  • your "Check error state on stream" code kinda just blew my mind....im just learning C++ and I haven't seen anything like that code before in any of my books. I will have to keep it in mind. – Alex Mar 12 '11 at 18:16
  • @Alex: That's certainly understandable. It's easy even for the best programmers to get I/O code wrong (in most languages, not just C++). Just make sure you are using [a good introductory book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn the language! :-) – James McNellis Mar 12 '11 at 18:25
2

In enterAccountData(), you're using accounts[MAX_ACCOUNTS] when you should be using accounts[counter].

The MAX_ACCOUNTS index is outside the bounds of the array (by one), causing unpredictable behaviour when you retrieve it (it will read whatever is in memory immediately after the array and try to shoehorn it into a BankAccount object).

Also, since you're initializing counter to 0 inside the loop, it will get re-initilized each time through; you want to move that initialization to before the loop starts.

Cameron
  • 96,106
  • 25
  • 196
  • 225
  • Thanks that fixed it, I just thought that I should use the MAX_ACCOUNTS for it : /. – Alex Mar 12 '11 at 18:09
  • @Alex: When you say `array[index]`, the `index`th element will get retrieved. This is different from declaring an array, where the size of the array is put in the `[ ]` brackets. – Cameron Mar 12 '11 at 18:13