Банковская программа, функция вывода средств не работает? (С)
По какой-то причине я не могу заставить функцию снятия средств работать в моей банковской программе... Я думал, что все делал правильно, поскольку функции могут быть очень похожи на функцию пополнения счета.
Проблема в том, что я не могу получить сумму вывода без программы, даже если она правильно построена. Функция вывода должна быть очень похожа на функцию депозита правильно? Потому что вместо того, чтобы внести значение на счет, вы просто снимаете сумму с данного счета. Если на счете недостаточно денег, он возвращается как ошибка. Вот мой код:
#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;
}
1 ответ
Решение
Попробуй это. (проверка того, достаточно ли на счете денег, здесь не реализована)
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] = money[custIndex] - withdraw ; // correct the formula
balance = money[custIndex];
}
return balance;
}