NOTE: Title of thread is Incorrect I meant Updating Elements in Arrays!!!
For some apparent reason I finished typing my code. No errors it seems like I'm missing something here. I'm trying to update my accounts but the balance keeps coming out to zero. Don't show me the answer but guide me on a path to correcting this problem.
Code:
#include <stdio.h>/* Header */
#define MAX_ACCTS 10
int accounts[MAX_ACCTS] =
{20181, 20467, 20813, 21354, 21358, 21359, 22027, 22041};
double balances[MAX_ACCTS] =
{0, 145.72, 10133.00, -17.44, 612.98, 1415.30, 313.21, 3131.32};
int findAccount(int accountID) {
int index = -1;
index = 1;
return(index);
}
void printAccountBal(int accountIndex) {
int i = 0.0;
printf("The Account Number is %d. Current Balance is %f\n\n", accounts[i], balances[i]);
}
void deposit(int accountIndex) {
float amount;
int i = 0;
printf("Enter Amount: " );
scanf("%f", &amount);
for (i=0; i<MAX_ACCTS; ++i)
do {
if (amount <= 0)
printf("Unacceptable Value. Please Insert Positive Number");
}
while ((i < accountIndex) && (++i < MAX_ACCTS));
balances[i] += amount;
}
void withdraw(int accountIndex) {
float amount;
int i = 0.0;
printf("Enter Amount: " );
scanf("%f", &amount);
for (i=0; i<MAX_ACCTS; ++i)
do {
if (amount <= 0)
printf("Unacceptable Value. Please Insert Positive Number");
}
while ((i < accounts [7]) && (++i < balances[7]));
balances[i] -= amount, ++i;
}
int main(void) {
int choice;
int accountID, accountIndex;
int accountIDgood;
printf("Bank account simulator\n");
do {
accountIDgood = 0; /* False */
do {
printf("\nEnter account ID: ");
scanf("%d", &accountID);
if (accountID < 20000 || accountID > 29999) {
printf("Account ID is not correct format.\n");
} else {
accountIndex = findAccount(accountID);
if (accountIndex == -1) {
printf("Account ID %d is not valid.\n",
accountID);
} else {
accountIDgood = 1; /* True */
}
}
} while (!accountIDgood);
printAccountBal(accountIndex);
do {
printf("Choose from the following menu:\n");
printf("\t0 - Exit the program\n");
printf("\t1 - Change accounts\n");
printf("\t2 - Make deposit\n");
printf("\t3 - Make withdrawal\n");
printf("Enter your choice: ");
scanf("%d", &choice);
printf("\n");
switch (choice) {
case 2:
deposit(accountIndex);
printAccountBal(accountIndex);
break;
case 3:
withdraw(accountIndex);
printAccountBal(accountIndex);
break;
default:
if (choice != 0 && choice != 1) {
printf("Your choice (%d) is not valid.\n",
choice);
}
break;
}
} while (choice != 0 && choice != 1);
} while (choice != 0);
return(0);
}