Reputation: 113
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;
}
Upvotes: 1
Views: 1917
Reputation: 34615
Some problems I noticed :
accounts[x].term
is also 1 ? Ans: You would never get in to that loop.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.
Upvotes: 1
Reputation: 4408
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.
Upvotes: 3