C加入两个函数

时间:2017-09-28 00:09:22

标签: c stdout

这是一个标准的银行帐户计划。允许存款,取款和查看资金。我无法让程序进入我的switch语句中的函数,这是基于他们的选择。这是此代码的输出。 CodeOutput。我不是要求任何人为我编码,如果你可以指出我出错的地方。

#include <stdio.h>

float getDeposit(float currentBalance);
float getWithdrawal(float currentBalance);
float displayBalance(float currentBalance);
char displayMenu();

int main()
{


    float currentBalance=200,newBalanceDep,newBalanceWith;
    char choice;
    choice = displayMenu();


        switch (choice)
    {

        case 'D': case 'd':
            newBalanceDep=getDeposit(currentBalance);
            break;
        case 'W': case 'w':
            newBalanceWith=getWithdrawal(currentBalance);
            break;
        case 'B': case 'b':
            displayBalance(currentBalance);
            break;
        case 'Q': case 'q':

            printf("Thank you!");
            break;
        default:
            printf("Invalid choice.");
            break;

    }

    return 0;
}

char displayMenu()

{
    char choice;

    printf("Welcome to HFC Credit Union! \n"); 
    printf("Please select from the following menu: \n");
    printf("D: Make a deposit \n");
    printf("W: Make a withdrawal \n");
    printf("B: Check your account balance \n");
    printf("Q: To quit \n");

    scanf("\n%c",choice);

    return choice;

}

float getDeposit(float currentBalance)
{
    float depositAmount;
    float newBalanceDep;



    printf("Enter amount you would like to deposit: /n");
    scanf("%f",&depositAmount);

    if(depositAmount>0)
    {
        newBalanceDep=depositAmount+currentBalance;
    }

    return newBalanceDep;   
}

float getWithdrawal(float currentBalance)
{
    float withdrawalAmount;
    float newBalanceWith;

    printf("Enter amount you would like to withdrawal: /n");
    scanf("%f",&withdrawalAmount);

    if(withdrawalAmount>currentBalance)
    {
        printf("Insufficient Funds. Try again.");
        printf("Enter amount you would like to withdrawal: /n");
        scanf("%f",&withdrawalAmount);

    }
    else if(withdrawalAmount<=currentBalance)
    {
        newBalanceWith=withdrawalAmount+currentBalance;
    }

    return newBalanceWith;  
}

float displayBalance(float currentBalance)

{
    printf("Your current balance is %.2f",currentBalance);

}

1 个答案:

答案 0 :(得分:2)

您需要将&choice传递给scanf,而不是choice

char choice; /*...*/; scanf("\n%c",choice); // --->    
//                                 v---- add  
char choice; /*...*/; scanf("\n%c",&choice);`

传递choice是未定义的行为。

一个好的编译器应该给你一个警告。