Need advice with my average calculator code - c++

Good day,
I need help with my below code. I have a problem with calculating the average.
The code works as it should expect for calculating the average of the second entry.
I have highlighted the average that does not add up in the below picture. Its weird that the average is only not adding up on the second entry?
#include <iostream>
using namespace std;
float snelheid[][50] = {{}};
float lading[] = {};
float snelheidGem[] = {0};
int ladingNo = 1;
int snelheidToets = 1;
int main()
{
cout << "Hoeveel ladings word getoets?: ";
cin >> ladingNo;
for (int i = 0; i < ladingNo; i++)
{
int n = i + 1;
if (n == 1 || n == 8 || n > 19)
{
cout <<"Wat was die " << n << "st lading?: ";
cin >> lading[i];
cout <<"Hoeveel skote was geskiet vir die "<< n << "st lading?: ";
cin >> snelheidToets;
snelheidGem[i] = {0};
for (int t = 0; t < snelheidToets; t++)
{
int m = t + 1;
if (m == 1 || m == 8 || m > 19)
{
cout <<"Wat was die spoed van die " << m << "st skoot?: ";
cin >> snelheid[i][t];
}
else
{
cout <<"Wat was die spoed van die " << m << "de skoot?: ";
cin >> snelheid[i][t];
}
snelheidGem[i]=snelheidGem[i]+snelheid[i][t];
}
snelheidGem[i] = snelheidGem[i]/snelheidToets;
cout << snelheidGem [i] << endl;
cout << endl;
}
else
{
cout <<"Wat is die " << n << "de lading?: ";
cin >> lading[i];
cout <<"Hoeveel skote was geskiet vir die "<< n << "de lading?: ";
cin >> snelheidToets;
snelheidGem[i] = {0};
for (int t = 0; t < snelheidToets; t++)
{
snelheid[i][t] = {0};
int m = t + 1;
if (m == 1 || m == 8 || m > 19)
{
cout <<"Wat was die spoed van die " << m << "st skoot?: ";
cin >> snelheid[i][t];
}
else
{
cout <<"Wat was die spoed van die " << m << "de skoot?: ";
cin >> snelheid[i][t];
}
snelheidGem[i]=snelheidGem[i]+snelheid[i][t];
cout << snelheid[i][t] << " snelheid" << endl;
cout << snelheidGem[i] << " gemiddeld" << endl;
}
snelheidGem[i] = snelheidGem[i]/snelheidToets;
cout << snelheidGem [i] << endl;
cout << endl;
}
}
return 0;
}
Average calculation

On this line:
float snelheidGem[] = {0};
you are creating an array of size 1, but are then indexing elements such as snelheidGem[1], and snelheidGem[2], etc, which invokes undefined behavior (UB). UB means anything can happen, including giving the correct average sometimes, but not at other times.
You can give it a sufficiently large size:
float snelheidGem[50] = {0};
or much better, use a std::vector, to which you can add elements by doing push_back.
Note that you have the same issue with:
float lading[] = {};

Related

Exception handling in c++ how do I implement it here

I just created a program in c++ about adding matrices but I just dont know how to exception handle the part where to choose a number from 1 to 10 so that the user can only choose a number from 1 to 10 and if he puts a wrong input in it shows an error message and is asked to input a number again
#include <iostream>
using namespace std;
int r, c, a[10][10], b[10][10], sum[10][10], i, j;
void matrix(string s) {
cout << "Enter number of rows (between 1 and 10): ";
cin >> r;
cout << "Enter number of columns (between 1 and 10): ";
cin >> c;
}
void storeValues(string s) {
cout << endl << "Enter elements of " << s << " matrix:" << " " << endl;
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
cout << "Enter element " << i + 1 << j + 1 << " : ";
if (s == "1st") {
cin >> a[i][j];
}
else
cin >> b[i][j];
}
}
void addMatrices() {
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];
}
void displayResult() {
cout << endl << "Sum of two matrix is: " << endl;
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
cout << sum[i][j] << " ";
if (j == c - 1)
cout << endl;
}
}
int main()
{
string s = "1st";
matrix(s);
storeValues(s);
s = "2nd";
storeValues(s);
addMatrices();
displayResult();
return 0;
}
You could easily make a function that handles bounded input for you, something like:
int bounded_input(int lower, int upper, std::string message) {
int r;
bool valid = false;
cout << message << " (between " << lower << " and " << upper << "): ";
do {
cin >> r; // NOTE: You will want to check for invalid input like "three" ...
if (r < lower || r > upper) {
cout << "Invalid, please enter between " << lower << " and " << upper << ":";
} else {
valid = true;
}
} while (!valid);
return r;
}
Then you can simply call this for all of your bounds:
int row_bound = bounded_input(1, 10, "Enter row bounds");

How to only print out highest/smallest array values in c++

Good day, I'm having difficulty on the last two parts of my program where it's supposed to only output players who got maximum/minimum scores, I need help on how to do it because I'm really confused. If it's also alright to provide some explanations I'd really appreciate it.
I tried this approach:
#include <iostream>
using namespace std;
int main() {
double lrgst, lrgst2, lrgst3;
int numbers[5];
lrgst = lrgst2 = lrgst3;
for (int i = 0; i < 5; i++) {
cin >> numbers[i];
}
for (int i = 0; i < 5; i++) {
if (numbers[i] > lrgst) {
lrgst3 = lrgst2;
lrgst2 = lrgst;
lrgst = numbers[i];
} else if (numbers[i] > lrgst2) {
lrgst3 = lrgst2;
lrgst2 = numbers[i];
} else if (numbers[i] > lrgst3) {
lrgst3 = numbers[i];
}
}
cout << "largest are: " << lrgst << " " << lrgst2 << " " << lrgst3;
}
this is my actual code:
#include <iostream>
using namespace std;
struct playerdata {
char name[50];
int age, score1, score2;
double average;
};
int main() {
int choice, i = 1, j = 1, z = 1, backtomain2;
char backtomain;
playerdata p1[10];
do {
for (int a = 0; a < 47; a++) {
cout << "=";
}
cout << "\n";
for (int b = 0; b < 22; b++) {
cout << " ";
if (b == 21) {
cout << "MENU \n";
}
}
for (int c = 0; c < 47; c++) {
ocut << "=";
}
cout << " "
"\n1. Add record\n"
"2. View players records\n"
"3. Compute for the average\n"
"4. Show the player(s) who gets the max average.\n"
"5. Show the player(s) who gets the min average.\n"
"6. Exit\n"
"Enter your choice:";
cin >> choice;
if (choice == 1) {
cout << "Add player data" << endl;
do {
cout << "Enter player " << i << " nickname:";
cin >> p1[i].name;
cout << "Enter player " << i << " age:";
cin >> p1[i].age;
cout << "Enter player " << i << " score 1:";
cin >> p1[i].score1;
cout << "Enter player " << i << " score 2:";
cin >> p1[i].score2;
cout << "Enter again? (Y/N)";
cin >> backtomain;
i++;
}
while (backtomain != 'N' && backtomain != 'n' && i < 7);
if (choice == 2) {
cout << "Player records" << endl;
cout << "Player nickname "
<< "Player age "
<< " player score 1"
<< "
player score 2\n ";
for (z = 1; z <= i - 1; z++) {
cout << p1[z].name << " " << p1[z].age << "" << p1[z].score1 << ""
<< p1[z].score2 << "\n";
}
cout << "Press 1 to go back to main menu\n";
cin >> backtomain;
}
if (choice == 3) {
cout << "Computing for average...\n";
for (int d = 1; d <= i - 1; d++) {
p1[d].average = (p1[d].score1 + p1[d].score2) / 2.0;
cout << "\n" << p1[d].average << "\n";
}
cout << "Press 1 to go back to main menu\n";
cin >> backtomain;
}
if (choice == 4) {
cout << "Player(s) who got the max average:\n";
cout << "\nPress 1 to go back to main menu";
cin >> backtomain;
}
if (choice == 5) {
cout << "player(s) who got the min average: \n";
cout << "Press 1 to go back to main menu";
cin >> backtomain;
}
}
while (choice != 6);
}
You can simply sort the array of players for that
int n = sizeof(p1)/ sizeof(p1[0]);
sort(p1, p1+n, compPlayer);
//larget at pl[0]
//smallest at pl[9]
where
bool compPlayer(playerdata p1, playerdata p2) {
return (p1.score1+p1.score2) > (p2.score1+p2.score2);
//use score incase average has not been calculated for all players yet
}

How do I properly run if statements and do while loops with functions?

I have 5 programs that run perfectly fine individually, but when I combine them I get error messages and won't build. I have a menu to pick which program to run using if statements. Also a do while loop to repeat the programs. I believe it has something to do with the functions because I haven't had this problem before with simple programs. The program should first ask which program you want to run from the menu. It will run that program, then ask if you want to repeat.
I don't know what to try other than what is now in the program. I did take the do while loop out but still had the issue with the if statements.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;
int main()
{
int choice;
char repeat;
if (repeat == 'm' || 'M'){
cout << "1. Perfect Scores\n"
"2. Larger Than n\n"
"3. Roman Numeral Converter\n"
"4. Monkey Business\n"
"5. Lottery\n"
"6. Exit\n";
cout << "Pick which program you would like to run." << endl;
cin >> choice;
}
else if (choice == 1){ // -----------Perfect Scores------------
do{
int countPerfect(int a[])
{
int i=0;
for(int l=0; l<10; l++)
if(a[1]==100)
i++;
return i;
}
{
int score[10];
for(int i=0; i<10; i++)
{
cout << "Enter score " << i+1 << endl;
cin >> score[i];
while(score[i]<0 || score[i] > 100)
{
cout << "Enter score between 1 and 100." << endl;
cin >> score[i];
}
}
int n = countPerfect(score);
cout << "No of perfect scores: " << n << endl;
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
return 0;
}
}while(repeat == 'Y' || repeat == 'y');
}
// ------------Larger Than n--------------
else if (choice == 2){
do{
void display_greator(int A[], int size, int n)
{
int i;
for(i=0; i< size; i++)
{
if(A[i]>n)
{
cout << A[i] << endl;
}
}
}
int main(void)
{
int i, size;
cout << "Enter the size of your array:"<< endl;
cin >> size;
int N[size];
cout << "Enter a list of " << size << " numbers:" << endl;
for( i=0; i<size; i++)
{
cin >> N[i];
}
int num;
cout << "Enter your number n:" << endl;
cin >> num;
display_greator(N, size, num);
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
}
}while(repeat == 'Y' || repeat == 'y');
}
// ----------------------Roman Numeral Converter------------------
else if (choice == 3){
char repeat;
do{
{
int n;
string romanNumbers[]={"I", "II", "III", "IV", "V", "VI", "VII",
"VIII", "IX", "X", "XI", "XII",
"XIII", "VIX", "XV", "XVI", "XVII", "XVIII", "XIX", "XX"};
cout << "Enter a decimal number or enter 0 to quit." << endl;
cin >> n;
if(n==0)
exit(0);
do
{
cout << "Enter number between 1 and 20" << endl;
cout << "Enter number or enter 0 to quit" << endl;
cin >> n;
} while(n < 0 || n > 20);
{
cout << "Enter decimal equivalent roman number:" << endl;
cout << "Enter a number between 1 and 20:" << endl;
cin >> n;
if(n==0)
exit(0);
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
}
while(n > 0 || n < 20);
return 0;
}
}while(repeat == 'Y' || repeat == 'y');
}
// ---------------Monkey Business--------------------
else if(choice == 4){
do{
const int DAYS = 7;
double getTotalAmountOfFood(int[][DAYS],int);
double getLeastAmountOfFood(int[][DAYS],int, double);
double getGreatestAmountOfFood(int[][DAYS],int, double);
{
const int MONKEYS = 3;
double totalFood, averageFood, leastFood, greatestFood;
int foodInfo[MONKEYS][DAYS];
for(int i= 0; i< MONKEYS; i++)
{
cout << "Enter the food information of the monkey" <<
(i + 1) << ":" << endl;
for(int j = 0; j < DAYS; j++)
{
cout << "Day" << (j + 1) << ":" << endl;
cin >> foodInfo[i][j];
while(foodInfo[i][j] < 0)
{
cout << "Day " << (j+1) << ":" << endl;
cin >> foodInfo[i][j];
}
}
cout << endl;
}
totalFood = getTotalAmountOfFood(foodInfo, MONKEYS);
leastFood = getLeastAmountOfFood(foodInfo, MONKEYS, totalFood);
greatestFood = getGreatestAmountOfFood(foodInfo, MONKEYS, 0);
averageFood = totalFood / DAYS;
cout << "The average amount of food per day for three monkeys(in pounds):"
<< averageFood << endl;
cout << "The least amount of food per week for monkeys(in pounds) is:"
<< leastFood << endl;
cout << "The greatest amount of food per wek for a monkey is(in pounds):"
<< greatestFood << endl;
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
return 0;
}
double getTotalAmountOfFood(int food[][DAYS],int mnks)
{
double total = 0;
for(int i = 0; i < mnks; i++)
{
for(int j = 0; j < DAYS; j++)
{
total += food[i][j];
}
}
return total;
}
double getLeastAmountOfFood(int food[][DAYS], int mnks, double leastAmount)
{
double least = leastAmount;
double weekTotal;
for(int i = 0; i < mnks; i++)
{
weekTotal = 0;
for(int j = 0; j < DAYS; j++)
{
weekTotal += food[i][j];
}
if(least > weekTotal)
least = weekTotal;
}
return least;
}
double getGreatestAmountOfFood(int food[][DAYS], int mnks, double greatestAmount)
{
double greatest = greatestAmount;
double weekTotal;
for(int i = 0; i < mnks; i++)
{
weekTotal = 0;
for(int j = 0; j < DAYS; j++)
{
weekTotal +=food[i][j];
}
if(greatest < weekTotal)
greatest = weekTotal;
}
return greatest;
}
}while(repeat == 'Y' || repeat == 'y');
}
// ----------------Lottery--------------------
else if (choice == 5){
char repeat;
do{
srand(time(NULL));
int winningDigits[5];
int player[5];
int num;
int matchCount = 0;
for (int i = 0; i < 5; i++)
{
winningDigits[i] = rand() % 10;
}
cout << "Enter 5 integers in the range of 0 to 9." << endl;
for(int i = 0; i < 5; i++)
{
cout << "Number #" << (i + 1) << ": " << endl;
cin >> num;
while (num < 0 || num > 9)
{
cout << "Invalid number! It should be in the range of 0 through 9." << endl;
cout << "Number #" << (i + 1) << ": " << endl;
cin >> num;
}
player[i] = num;
}
for (int i = 0; i < 5; i++)
{
if (winningDigits[i] == player[i])
{
matchCount++;
}
}
cout << "Winning digits: " << endl;
for (int i = 0; i < 5; i++)
{
cout << winningDigits[i] << " " << endl;
}
cout << "Player's digits: " << endl;
for (int i = 0; i < 5; i++)
{
cout << player[i] << " " << endl;
}
cout << endl << endl << "Number of digits matched: "
<< matchCount << endl;
cout << "To repeat, press Y. For main menu, press M" << endl;
cin >> repeat;
return 0;
}while(repeat == 'Y' || repeat == 'y');
}
else if (choice == 6)
{cout << "Bye" << endl;}
}
I'm expecting to be able to choose a program to run, repeat it, and repeat the entire program from main menu.
I am not going to point out all errors in your code, they are just too many. You went too fast too far. If you think the complexity of code and the errors it procudes are intimidating you are right. I wrote codes with more lines, but yours is too complicated for me. Go in small steps. Start with something along the line of:
int choose() { return 0; }
void func1() {}
void func2() {}
int main() {
int choice = 0;
while ( choice = choose() ) {
switch(choice) {
case 1 : func1(); break;
case 2 : func2(); break;
}
}
}
Your main function does not have to be more complex than that.
Write this, not more. Make sure it compiles, then in tiny steps fill the gaps, after each step compile, see if it does what you expect and only then continue to put more.
Some problems in your code (partly stolen from comments):
you cannot have more than one main
you cannot define functions inside functions
you cannot start an if statement with else if
int N[size]; is not standard C++ for a non-compile-time-constant size. Use std::vector instead
see here why using namespace std is considered bad practice
if (repeat == 'm' || 'M') is not doing what you expect, it should be if (repeat == 'm' || repeat == 'M'). In yours 'M' is taken as a bool which is always true (because it isnt 0).
make sure to initialize variables. Using variables that are not initialized causes undefined behaviour.
please next time reduce your code to a mcve and try to concentrate on a single problem, also include the error in the question
I cannot help myself than to point that for each single problem there are duplicate questions, which brings me back to: Don't do too many things at once. Fixing 100 errors at once is extremely difficult, fixing 1 error is doable.
last but not least, pay attention to compiler errors and warnings while you write the code (again: not after you wrote several pages, but after each single line)

Error: No operator = matches these operands

How come when I do fractionAry[i] = new Fraction(num1, denom1) it works and stores the created fraction into index of i.
But when I do fractionAry[i][j] = new Fraction(num1, denom1), the compiler gives me an error saying that no operator matches these operands?
If I would like to store the fraction into j, how would I do that? I am not entirely sure how to store a fraction into an array of array of fractions...
Here's my code, thanks for the help ahead of time.
void createArray() {
Fraction** fractionAry;
int aryCount;
int arySize;
int i, j;
int num1, denom1;
cout << "\nHow many arrays of fractions (treating these as array of arrays of fractions? ";
cin >> aryCount;
if (aryCount > 0) {
fractionAry = new Fraction*[aryCount];
for (i = 0; i < aryCount; i++) {
cout << "\nCreating array index # " << i
<< "\n How many fractions(s)? ";
cin >> arySize;
if (arySize > 0) {
fractionAry[i] = new Fraction[arySize + 1];
fractionAry[i][0] = arySize;
for (j = 1; j < arySize + 1; j++) {
cout << "\n Enter the numerator: ";
cin >> num1;
cout << " Enter the denominator: ";
cin >> denom1;
while (denom1 == 0) {
cout << "\nCan't set to 0! Enter a new denominator: ";
cin >> denom1;
}
fractionAry[i] = new Fraction(num1, denom1);
// fractionAry[i][j] = new Fraction(num1, denom1); I would like to do this instead
}
cout << "\nFor array index #" << i << endl;
for (j = 0; j < arySize + 1; j++) {
cout << " Element index #" << j << " : " << *(*(fractionAry + i) + j) << endl;
}
}
}
}
}
Instead of having fractionAry[i][j] = new Fraction(...) use fractionAry[i][j] = Fraction(...). The type at fractionAry[i][j] is Fraction not Fraction* which is what the new operator returns.
void createArray() {
Fraction** fractionAry;
int aryCount;
int arySize;
int i, j;
int num1, denom1;
cout << "\nHow many arrays of fractions (treating these as array of arrays of fractions? ";
cin >> aryCount;
if (aryCount < 1)
{
//print error message or exit function
}
fractionAry = new Fraction*[aryCount];
for (i = 0; i < aryCount; i++)
{
cout << "\nCreating array index # " << i
<< "\n How many fractions(s)? ";
cin >> arySize;
if (arySize < 1)
{
//Print error message decrement i and continue or exit function
}
fractionAry[i] = new Fraction[arySize + 1];
//I don't understand the purpose of assigning the below value or making the array one size bigger
fractionAry[i][0] = arySize;
///
for (j = 1; j < arySize + 1; j++)
{
cout << "\n Enter the numerator: ";
cin >> num1;
cout << " Enter the denominator: ";
cin >> denom1;
while (denom1 == 0)
{
cout << "\nCan't set to 0! Enter a new denominator: ";
cin >> denom1;
}
fractionAry[i][j] = Fraction(num1, denom1);
}
cout << "\nFor array index #" << i << endl;
for (j = 0; j < arySize + 1; j++)
{
cout << " Element index #" << j << " : " << fractionAry[i][j] << endl;
}
}
}

Structures with arrays

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;
}
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.
Some problems I noticed :
What if in accounts[x].term is also 1 ? Ans: You would never get in to that loop.
Are you actually computing the average of all bank accounts? Ans: No
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.