1

I am having trouble with my code. I cannot seem to understand how to implement my balances into the arrays while using structures. Is there anyone that could help me?

#include <iostream>
#include <iomanip>

using namespace std;    

struct BankAccount
{
    int accountNum;
    double accountBal;
    double annualIntrest;
    int term;
};
int main()
{
    const int BANKACC = 5;
    const int QUIT = 1;
    const int MONTHS_IN_YEAR = 12;
    int x,found,input,month;
    double total = 0;
    double average = 0;
    BankAccount accounts[BANKACC];

    for(x = 0; x < BANKACC; x++)
    {
        do
        {
            found = 0;
            cout << "Enter in account # " << (x + 1) << endl;
            cin >> accounts[x].accountNum;
            while(accounts[x].accountNum < 1000 || accounts[x].accountNum > 9999)
            {
                cout << "Account number must be four didgets:" << endl;
                cin >> accounts[x].accountNum;
            }
            for(int check = 0; check < x; check++)
            {
                while(accounts[x].accountNum == accounts[check].accountNum)
                {
                    cout << endl << "Account Numbers cannot be the same, enter in a new account number." << endl;
                    found = 1;
                    break;
                }
            }
        } while(found); 

        cout << "Enter the accounts balance."  << endl;
        cin >> accounts[x].accountBal;
        while(accounts[x].accountBal < 0)
        {
            cout << "Account cannot have a negitive balance." << endl;
            cin >> accounts[x].accountBal;
        }
        cout << "Enter the interest rate." << endl;
        cin >> accounts[x].annualIntrest;
        while(accounts[x].annualIntrest > 0 && accounts[x].annualIntrest > 0.15)
        {
            cout << "Annual interest must be from 0 to 0.15." << endl;
            cin >> accounts[x].annualIntrest;
        }
        cout << "How many years will the term be held for? " << endl;
        cin >> accounts[x].term;

            while(accounts[x].term < 1 || accounts[x].term > 10)
            {
                cout << "The Term must be greater than 1 and should not exceed 10" << endl;
                cin >> accounts[x].term;
            }
    }
    for(int year = 1; year < accounts[x].term; year++)
    {
        for( month = 1; month < MONTHS_IN_YEAR; month++)
        { 
            accounts[x].accountBal = accounts[x].accountBal * accounts[x].annualIntrest + accounts[x].accountBal;
            total += accounts[x].accountBal;
            x++;

        }
        month = 1;
        average = total  / BANKACC;
    }
    for(x = 0; x < BANKACC; x++)
    {
        cout << "Account # " << (x + 1) << "'s number is: " << accounts[x].accountNum;
        cout << " The accounts balance is: " << accounts[x].accountBal;
        cout << " The interest on the account is: " << accounts[x].annualIntrest << endl;
    }

    cout << "Average of all the bank accounts is: " << average << endl;

    cout << "Which account do you want to access?" << endl <<
        " To stop or look at none of the account numbers type " << QUIT << endl;    
        for(x = 0; x < BANKACC; x++)
        {
            cout << accounts[x].accountNum << "    ";
        }
        cin >> input;

        while(input != QUIT)
        {
            found = 0;
            x = 0;
            while(x < BANKACC && input != accounts[x].accountNum)
            {
                x++;            
            }
                if(input == accounts[x].accountNum)
                {
                    cout << "Account:" << accounts[x].accountNum << " balance is: " <<
                    accounts[x].accountBal << " Interest rate is: " << accounts[x].annualIntrest;
                    cout << endl << "Enter the another account number or type 1 to quit.";
                    found = 1;
                    cin >> input;
                }
            if(found == 0)
            {
            cout << "Sorry that account doesn't exist. Enter another account number.";
            cin >> input;
            }
        }
    system("pause");
    return 0;
}
Alex
  • 113
  • 1
  • 7
  • 3
    Where in your code are you getting stuck? – Marlon Feb 22 '11 at 22:49
  • 1
    That's a lot of code to sift through. Can you be more specific about what you're having trouble with? And also trim down your program to the relevant section(s), if possible? – John Kugelman Feb 22 '11 at 22:52
  • 2
    I realize this code looks like you're trying to learn C++, and you've got plenty on your hands already, but **[never use floating point for currency](http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency)**. Floating point is great for some kinds of physical simulations but is inappropriate for representing currency. – sarnold Feb 22 '11 at 23:05
  • Yeah it's homework and I am needing help with it. – Alex Mar 03 '11 at 18:03
  • @sarnold- I didn't use a float data type did I? O.o? – Alex Mar 03 '11 at 18:04

2 Answers2

3

Everything looks ok, until this line:

for(int year = 1; year < accounts[x].term; year++) 

This code is outside the scope of the for loop:

for(x = 0; x < BANKACC; x++) { ... }

I believe that the loop over the years should be inside the BANKACC loop. For one, by the time the code gets to this, x is out of bounds of the array it looks like.

But that is not the only problem. The loops that iterate over the years and months will always go 1 less than they should since they are starting from 1, and going until (< term) or (< months_in_year)

Also, the way the average is being computed also seems to be wrong.

Nick Banks
  • 4,298
  • 5
  • 39
  • 65
  • I ended up implementing both of the above suggestions and it works. I just need to use set precision and everything should work. Thanks a lot I have been working on this for about a week and just couldnt figure it out : / – Alex Feb 23 '11 at 00:08
1

Some problems I noticed :

  1. What if in accounts[x].term is also 1 ? Ans: You would never get in to that loop.
  2. Are you actually computing the average of all bank accounts? Ans: No
  3. You said account must be of 4 digits. So, 1000 & 9999 are valid account numbers. So, your condition should have been - while( accounts[x].accountNum < 999 || accounts[x].accountNum > 10000) ) { /.... }

Assuming that, user will always enter intergers only between 1 to 10 for term, try this -

for( int x=0; x < BANKACC; ++x )
{
    for(int year = 1; (year < accounts[x].term) || (year==accounts[x].term); ++year)
    {
        for( month = 0; month < MONTHS_IN_YEAR; ++month)
        { 
            accounts[x].accountBal += accounts[x].accountBal * accounts[x].annualIntrest;
            total += accounts[x].accountBal;
        }
    }
}
average =(double) (total  / BANKACC);

Also, if had set the warnings on while compilation, you should have got useful messages regarding array out of bounds.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • I ended up implementing both of the above suggestions and it works. I just need to use set precision and everything should work. Thanks a lot I have been working on this for about a week and just couldn't figure it out : / – Alex Feb 23 '11 at 00:09
  • @Alex - Also, begin practicing to seperate the logic using functions. Here for example, you can write a seperate function to calculate the `average`, to which however you need to pass the specific parameters required. Don't write everything in `main()`. – Mahesh Feb 23 '11 at 00:26
  • @Mahesh- Yeah, this chapter within my school book wasn't wanting functions and so I am trying to currently make this program into functions since I am on the functions chapter. – Alex Mar 03 '11 at 14:43