Function 在函数中传递值

Function 在函数中传递值,function,turbo-c,Function,Turbo C,我有这项工作,似乎是一个自动取款机,但我的问题是我想更新余额,但我不能。例如,当我第一次检查余额时,它给出“0”,然后如果我再次检查余额时存款“200”,它现在给我“200”,然后如果我想提取“100”,那么当我检查余额时,它应该给我“100”。但是我在传递函数中的值时遇到了一个问题。这是我的作品。请帮帮我。哦,顺便说一下,我正在使用devc+ #include <stdio.h> #include <conio.h> #include <stdlib.h&

我有这项工作,似乎是一个自动取款机,但我的问题是我想更新余额,但我不能。例如,当我第一次检查余额时,它给出“0”,然后如果我再次检查余额时存款“200”,它现在给我“200”,然后如果我想提取“100”,那么当我检查余额时,它应该给我“100”。但是我在传递函数中的值时遇到了一个问题。这是我的作品。请帮帮我。哦,顺便说一下,我正在使用devc+

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

// Declaring Functions that exist in the program.  

    int menu();  

// End  

// Main method at top, just to help with readability. And it can use the functions since we have already told main they exist.  
    int main()  
    {  
    int a = 0;  
    int option;  
    int atmDeposit();  
    int atmWithdrawal(int balance);  
    int atmCheck(int z); 

    system("cls");  
    do  
    {  
        option = menu();  
        switch(option)  
        {  
            case 1:  
                atmDeposit();  
                break;  
            case 2:  
                atmWithdraw(a);  
                break;  
            case 3: atmCheck(a);  
                break; 
            case 4:  
                printf("\nGoodbye!");  
                system("pause");  
                exit(0);  
            default:  
                printf("\nInvalid!\n");  
                break;  
        }  
    }  
    while (option != 4);  

    getch();  
    return 0;  
}  

int menu()  
{  
    int op;  
    system("cls");  

    printf("What do you want to do?: \n");  
        printf("1 - Deposit\n");  
        printf("2 - Withdraw\n");  
        printf("3 - Check Balance\n");  
        printf("4 - Exit\n\n");  

        printf("Enter Choice: ");  

        scanf("%d",&op);  
    return op;  

}  
// End  

// to check balance  
int atmCheck(int z)  
{  

    printf("\nYour Balance is P%d\n",z);  
    system("pause");  
    return z;  
}  
// End check balance  

// to Deposit  
int atmDeposit()  
{  
    int deposit, a=0;  

    printf("\nHow much money do you want to deposit?: P");  
    scanf("%d", &deposit);  

    a += deposit;  

    printf("%d",a);  
    system("pause");  


    return a;  
}  
// end deposit  

// to withdraw  
int atmWithdraw(int balance)  
{  
    int withdraw;  

    printf("\nHow much money do you want to withdraw?: P");  
    scanf("%d", &withdraw);  

    balance -= withdraw;  
    printf("%d",balance);  
    system("pause");   

    return balance;  
}  
// end withdraw  
#包括
#包括
#包括
//声明程序中存在的函数。
int菜单();
//结束
//主要方法在顶部,只是为了帮助可读性。它可以使用这些函数,因为我们已经告诉main它们存在。
int main()
{  
int a=0;
int选项;
int-atmDeposit();
int atmWithdrawal(int余额);
intatmcheck(intz);
系统(“cls”);
做
{  
选项=菜单();
开关(选件)
{  
案例1:
atmDeposit();
打破
案例2:
atmWithdraw(a);
打破
案例3:atmCheck(a);
打破
案例4:
printf(“\nGoodbye!”);
系统(“暂停”);
出口(0);
违约:
printf(“\n无效!\n”);
打破
}  
}  
while(选项!=4);
getch();
返回0;
}  
int菜单()
{  
int op;
系统(“cls”);
printf(“您想做什么?:\n”);
printf(“1-存款\n”);
printf(“2-撤回”\n);
printf(“3-支票余额”);
printf(“4-退出\n\n”);
printf(“输入选项:”);
scanf(“%d”和&op);
返回op;
}  
//结束
//平衡
int atmCheck(int z)
{  
printf(“\n您的余额为P%d\n”,z);
系统(“暂停”);
返回z;
}  
//期末支票余额
//存放
int-atmDeposit()
{  
整数存款,a=0;
printf(“\n您想存多少钱?:P”);
scanf(“%d”和存款);
a+=存款;
printf(“%d”,a);
系统(“暂停”);
返回a;
}  
//期末存款
//撤回
int atmWithdraw(int余额)
{  
int退出;
printf(“\n您想取多少钱?:P”);
scanf(“%d”,撤消(&d));
结余-=提取;
printf(“%d”,余额);
系统(“暂停”);
收益余额;
}  
//收尾

这里有几个地方出了问题。首先,取款和存款功能中使用的变量仅限于这些功能。因此,下次您要求用户提交选择并执行操作时,上次分配的值将丢失

您可以使用
main
中声明的变量
a
来存储余额,然后在其他函数中对其执行操作,从而纠正此问题。但是,如果变量是在
main
中声明的,这也是一个函数,则在其他函数中不会识别该变量。因此,您需要在
main
之外声明它,如下所示:

int menu();
int a = 0;

int main()
{
...
}
接下来,在
atmDeposit
函数中,声明一个局部变量
a
。将存款金额添加到该迭代中时,该值仅保留在该迭代中。下次您要求用户输入选项时,变量将重置为0,并且您以前的更改将丢失。所以,去掉局部变量,使用上面声明的全局变量。
atmWithdraw
atmCheck
函数需要相同的修复-从余额金额的全局变量中减去存款并返回

最后,我不确定您为什么需要这些函数中的任何一个的输入参数,因为您从用户那里获取存款和取款的输入。因此,删除函数的输入参数。此外,我不知道为什么要返回值,如果您只在屏幕上显示每种情况下的新余额。因此,您的函数签名应该是:

void atmDeposit();
void atmWithdraw();
void atmCheck();
1)声明变量
inta=0全局(即:主功能之外-如下所示)。这允许所有函数访问公共变量

int a=0;

int main()
{
  ...
2)在函数
int-atmDeposit()
中,删除声明
a=0

3)在函数
int-atmWithdraw(int-balance)
中,在系统之前添加下一行(“暂停”)


通过这些更改,该程序可以正常工作。

您确定存款后检查余额时会显示200吗?它给了我0。
a = balance;