c - 银行程序提款功能不起作用? (C)

标签 c banking

由于某种原因,我无法在我的银行程序中使用提款功能...我认为我做得正确,因为这些功能与存款功能非常相似。

问题是,即使程序没有正确构建,我似乎也无法获得提款金额。提款功能应该与存款功能非常相似,对吗?因为您不是将金额存入帐户,而是只是从给定帐户中提取金额。如果帐户没有足够的钱,则会返回错误。这是我的代码:

#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;
}

最佳答案

试试这个。 (这里没有实现检查账户是否有足够的资金)

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;
}

关于c - 银行程序提款功能不起作用? (C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35604136/

相关文章:

python - 将 C 实例传递给 Python

c - Httperf 文件描述符限制

ruby-on-rails - 如何下拉银行交易记录?

banking - 如何从网站上提取银行对账单?

我们可以像在 Windows 上一样直接用 C 在 OS X 和 Linux 上制作图形吗?

c - 从整数创建指针,无需在 C 函数调用中进行强制转换

c - 抑制 "Bad system call"消息

database-design - Cassandra 适合银行应用吗?

xsd - XML Schema 格式文件 : pain. 008.002.02

java - 为什么我的 public void Constructor {} 不能编译?