for some reason I can't get the withdraw function to work in my banking program... I thought I was doing it correct as the functions can be very similar to the deposit function.
The problem is I can't seem to get a withdraw amount without the program even building correctly. The withdraw function should be very similar to the deposit function correct? Because instead of depositing a value into the account, you're simply withdrawing an amount from the given account. If the account doesn't have enough money, then it returns as an error. Here's my code:
#include <stdio.h>
#include <stdlib.h>
#define MAXNUMCUSTOMERS 10 // Maximum number of customers
#define MAXCUSTOMERNAME 20 // Max length of customer's name including null character at the end
// Function Prototypes
int DisplayMenu();
int userLogin(int acctNumber[MAXNUMCUSTOMERS]);
float acctBalance (int custIndex, float money[MAXNUMCUSTOMERS]);
float depositMoney (int custIndex, float money[MAXNUMCUSTOMERS]);
float withdrawFunds (int custIndex, float money[MAXNUMCUSTOMERS]);
/*
*
*/
int main(int argc, char** argv) {
int acctNumber[MAXNUMCUSTOMERS] =
{1111,2222,3333,4444,5555,6666,7777,8888,9999,10000};
char customerName[MAXNUMCUSTOMERS][MAXCUSTOMERNAME] =
{"Joe","Mary","bob","tim","sid","will","ray","les","quinn","helen"};
float money[MAXNUMCUSTOMERS] =
{12.50,25.30,69.3,46.0,777.00,10000.,55.6,33.78,99.54,47895.33};
int option = 0;
int myAcct = -1;
float myBalance = 0.0;
// loop until done
while (option != 6) {
// Display menu - return option
option = DisplayMenu();
if (option == 0) { // customer login
myAcct = userLogin(acctNumber);
if (myAcct == -1) {
printf("Invalid account\n");
} else {
printf ("Welcome %s \n", customerName[myAcct]);
}
} else if (option == 1) { // deposit money
myBalance = depositMoney (myAcct, money);
if (myBalance < 0.0)
printf("Account not found\n");
else
printf ("Your new account balance is: %.2f\n", myBalance);
} else if (option == 2) { // withdraw money
if (myBalance > 0.0)
printf("Account not found\n");
else
printf ("Your new account balance is: %.2f\n", myBalance);
} else if (option == 3) { // account balance
myBalance = acctBalance (myAcct, money);
if (myBalance < 0.0)
printf("Account not found\n");
else
printf ("Your account balance is: %.2f\n", myBalance);
} else if (option == 4) { // add a customer
} else if (option == 5) { // delete a customer
} else if (option == 6) { // exit
}
// exit program
} //end loop until done
return (EXIT_SUCCESS);
}
int DisplayMenu() {
int customerChoice;
printf("Welcome to My Bank\n");
printf("Enter an option from the menu\n");
printf("\t 0 Customer Login\n");
printf("\t 1 Deposit Money Login\n");
printf("\t 2 Withdraw Money\n");
printf("\t 3 Account Balance\n");
printf("\t 4 Add a customer\n");
printf("\t 5 delete a customer\n");
printf("\t 6 Exit\n");
scanf("%d", &customerChoice);
return (customerChoice);
}
int userLogin(int acctNumber[MAXNUMCUSTOMERS])
{
int customerIndex = -1;
int accountNum;
int i;
// get the account number
printf("Please enter your account number>");
scanf("%d", &accountNum);
// loop to find which index has customer information
for (i=0; i < MAXNUMCUSTOMERS; i++)
{
if (accountNum == acctNumber[i])
customerIndex = i;
} // end loop
return customerIndex;
}
float acctBalance (int custIndex, float money[MAXNUMCUSTOMERS])
{
float balance = -1.0;
if (custIndex >= 0)
balance = money[custIndex];
return balance;
}
float depositMoney (int custIndex, float money[MAXNUMCUSTOMERS])
{
float balance = -1.0;
float deposit;
if (custIndex >= 0)
{
// get the deposit amount
printf ("Enter Deposit Amount>");
scanf ("%f", &deposit);
money[custIndex] = money[custIndex] + deposit;
balance = money[custIndex];
}
return balance;
}
//my problem is down here. This section doesn't work???
float withdrawFunds (int custIndex, float money[MAXNUMCUSTOMERS];
{
float balance = -1.0;
float withdraw;
if (custIndex >= 0)
{
//get withdraw amount
printf ("Enter Withdraw Amount>");
scanf ("%f", %withdraw);
money[custIndex] = withdraw - money[custIndex];
balance = money[custIndex];
}
return balance;
}