C 语言的简单银行系统程序中的逻辑错误

问题描述 投票:0回答:1

因此,当用户在帐户(结构)上存入金额时,余额不会更新,因此我无法继续进行其他操作,例如提款等。

这是代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct bank_account
{
    int account_number;
    char name[15];
    char surname[15];
    float balance;
};

void deposit(struct bank_account, float);
void withdraw(struct bank_account, float);
void check_balance(struct bank_account);


int main(int argc, char **argv[]){
    
// initialization of bank account with default values

    struct bank_account account; 
    account.account_number = 1234567;
    strcpy(account.name, "Randy");
    strcpy(account.surname, "Orton");
    account.balance = 0;

    int choice;
    float amount;

    printf("Welcome to our Bank's environment!\n");
    printf("Account number: %d\n", account.account_number);

    // Menu for banking operations
    do
    {
        printf("\nMenu:\n");
        printf("1. Deposit\n");
        printf("2. Withdraw\n");
        printf("3. Check balance\n");
        printf("4. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice)
        {
        case 1:
            printf("Enter the amount to deposit: ");
            scanf("%f", &amount);
            deposit(account, amount);
            break;
        case 2:
            printf("Enter the amount to withdraw: ");
            scanf("%f", &amount);
            withdraw(account, amount);
            break;
        case 3:
            check_balance(account);
            break;
        case 4:
            printf("Thank you for your preference.\n");
            break;
        default:
            printf("Invalid choise! Please try again.\n");
            break;
        }
    }
    while(choice != 4);

    return 0;
}

// Function to deposit money into the account
void deposit(struct bank_account account, float amount)
{
   account.balance += amount;
   printf("Deposit succesfull! Current balance: %.2f\n", account.balance);
}

// Function to withdraw money from the account
void withdraw(struct bank_account account, float amount)
{
    if(amount > account.balance) 
        printf("Insufficient balance. Cannot procceed with withdraw.\n");
    else 
    {
        account.balance -= amount;
        printf("Withdraw succesfull! Current balance: %.2f\n", account.balance);
    }
}

// Function to check account balance
void check_balance(struct bank_account account)
{
    printf("Current balance: %.2f\n", account.balance);
} 

我把它给了聊天 gpt 并说在函数括号内我需要包含一个指向结构的指针而不是结构本身以及要访问的函数中的这个运算符“->”(我不知道)结构成员,而不是我使用的点。

我问它是否可以在没有 -> 运算符的情况下以某种方式实现它,但它不能给我一个明确的答案。

那么你觉得呢?有没有其他方法或者我必须使用指针和“->”?

c pointers struct error-handling logic
1个回答
0
投票

在您的

deposit
函数中,您按值传递结构。制作副本后,您可以修改该副本,然后不会对原始副本产生任何影响。您需要将 pointers 传递给您的结构。

例如

void deposit(struct bank_account *account, float amount)
{
   account->balance += amount;
   printf("Deposit succesfull! Current balance: %.2f\n", account->balance);
} 

您的其他功能也需要纳入这一点。

© www.soinside.com 2019 - 2024. All rights reserved.