首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么它是平衡的0我不明白

为什么它是平衡的0我不明白
EN

Stack Overflow用户
提问于 2022-10-26 07:46:42
回答 2查看 57关注 0票数 -5
代码语言:javascript
复制
#include <iostream>
static float balance = 30000.0;

void deposit(float amount) {
    if (amount < 0.0 && amount > 100000.0 ) {
        printf("Error invalid amount entered ");
    }
    else  {

        balance += amount;
    }

}

float withdrawal(float withdrawal) {

    if (balance == 0.0)
        printf("NO MONEY BROKIE");
    if (withdrawal < 17000.0) {
        printf("Error invalid amount entered ");
    }
    if (balance <= withdrawal) {

        withdrawal = balance;
        balance = 0;
    }

    balance -= withdrawal;
    return withdrawal;

}

void Check() {

    printf("Your Account Balance is : %f ", &balance);
}

int main()
{

    float amount;
    int task;
    printf(" 1. Check Balance \n 2. Deposit \n 3. Withdrawal");
    printf(" \n\nPlease note :\nThe deposit amount must not be greater than $100,000 ");
    printf("\nThe withdrawal amount must not be greater than $17,000  ");
    printf("\n\nWhat service would you like to perform : ");
    scanf_s("%d", &task);

    if (task == 1) {

        Check();
    }
    else if (task == 2) {

        printf("Enter the amount you would like to deposit : ");
        scanf_s("%f", &amount);
            deposit(amount);
        printf("Thanks for doing business you have deposited %f ", &amount );
    }
    else if (task == 3) {

        printf("Enter the amount you would like to withdraw : ");
        scanf_s("%f", &amount);
        float Withdrawal = withdrawal(amount);
        printf("Thanks for doing business you have withdrawn : %f ", &Withdrawal);
    }
    else {
        printf("Invalid task number !");
    }

}

我还以为它能起作用呢。为什么我运行的时候平衡0.0000和任何其他错误,我可能会错过我是新的,所以更多的细节,为什么它要求更详细的.....................................................................................

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-10-26 07:57:48

您犯了一些语法错误,下面是完整的工作代码:

代码语言:javascript
复制
#include <iostream>
static float balance = 30000.0;

void deposit(float amount) {
    if (amount < 0.0 && amount > 100000.0 ) {
        printf("Error invalid amount entered ");
    }
    else  {

        balance += amount;
    }

}

float withdrawal(float withdrawal) {

    if (balance == 0.0)
        printf("NO MONEY BROKIE");
    if (withdrawal < 17000.0) {
        printf("Error invalid amount entered ");
    }
    if (balance <= withdrawal) {

        withdrawal = balance;
        balance = 0;
    }

    balance -= withdrawal;
    return withdrawal;

}

void Check() {

    printf("Your Account Balance is : %f ", balance);
}

int main()
{

    float amount;
    int task;
    printf(" 1. Check Balance \n 2. Deposit \n 3. Withdrawal");
    printf(" \n\nPlease note :\nThe deposit amount must not be greater than $100,000 ");
    printf("\nThe withdrawal amount must not be less than $17,000  ");
    printf("\n\nWhat service would you like to perform : ");
    scanf("%d", &task);

    if (task == 1) {

        Check();
    }
    else if (task == 2) {

        printf("Enter the amount you would like to deposit : ");
        scanf("%f", &amount);
            deposit(amount);
        printf("Thanks for doing business you have deposited %f ", amount );
    }
    else if (task == 3) {

        printf("Enter the amount you would like to withdraw : ");
        scanf("%f", &amount);
        float Withdrawal = withdrawal(amount);
        printf("Thanks for doing business you have withdrawn : %f ", Withdrawal);
    }
    else {
        printf("Invalid task number !");
    }

}
票数 0
EN

Stack Overflow用户

发布于 2022-10-26 07:50:31

除了字符串外,scanf期望通过指针传递填充的参数。printf期望参数按值传递。

这一行:

代码语言:javascript
复制
printf("Your Account Balance is : %f ", &balance);

应:

代码语言:javascript
复制
printf("Your Account Balance is : %f ", balance);

类似地,还有其他print语句需要通过值而不是指针传递主参数。如这一行:

代码语言:javascript
复制
printf("Thanks for doing business you have withdrawn : %f ", &Withdrawal);

应:

代码语言:javascript
复制
printf("Thanks for doing business you have withdrawn : %f ", Withdrawal);

另一个可能的窃听器。在withdrawl函数中,如果数量小于17000,则代码会打印错误,但不会立即返回。相反,它将继续处理事务。不确定这是否重要。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74204311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档