C语言中的ATM机

C语言中的ATM机,c,C,我一直在为ATM接口编写代码,其中包括一个pin,必须由用户输入才能访问选项。完成后,我有5个选项可供选择,如快速现金、取款、存款和支票余额。一切似乎都运行良好唯一的问题是,当用户存款、取款或获得快速现金时,帐户余额没有更新到正确的金额。有人能帮我修一下吗。我会把我的代码贴在下面 #include <stdio.h> int fastCash(int amount); int deposit(int deposit); int withdraw(int balence); void

我一直在为ATM接口编写代码,其中包括一个pin,必须由用户输入才能访问选项。完成后,我有5个选项可供选择,如快速现金、取款、存款和支票余额。一切似乎都运行良好唯一的问题是,当用户存款、取款或获得快速现金时,帐户余额没有更新到正确的金额。有人能帮我修一下吗。我会把我的代码贴在下面

#include <stdio.h>

int fastCash(int amount);
int deposit(int deposit);
int withdraw(int balence);
void checkBalence(int balence);

int main()
{
    int pin;
    int pins = 9999;
    int pinTries = 1;
    int reciept;
    int options;
    int options2;
    int balence = 300;
    int fastCashChoice;
    printf("Enter your pin:");// the pin is 9999
    scanf("%d", &pin);
    while (pinTries <= 3)
    {
        if (pin == pins)
        {
            printf("Would you like a reciept:");
            //1 is equal to yes and 2 is equal to no
            scanf("%d", &reciept);
            printf("Choose from the following:\n");
            printf("1. Fast cash\n2. Withdraw\n3. Deposit\n4. Check balence\n5. Get card back");
            scanf("%d", &options);
            while (options <= 5)
            {
                switch (options)
                {
                case 1:
                    fastCash(fastCashChoice);
                    balence = balence - fastCashChoice;
                    break;

                case 2:
                    withdraw(balence);
                    break;

                case 3:
                    deposit(balence);
                    break;

                case 4:
                    checkBalence(balence);
                    break;

                case 5:
                    options2 == 2;
                    break;

                }
                printf("Would you like anohter transaction: ");// 1 is equal to yes and 2 is equal to no
                scanf("%d", &options2);
                if (options2 == 1)
                {
                    printf("1. Fast cash\n2. Withdraw\n3. Deposit\n4. Check balence\n5. Get card back");
                    scanf("%d", &options);
                }

                else
                {
                    options = 5;
                    pinTries = 4;
                    printf("Thank you for useing this ATM, GoodBye\n");
                }
            }
        }

        else if (pin != pins)
        {
            printf("Invalid pin, try again:");
            scanf("%d", &pin);
            pinTries++;
        }

        if (pinTries == 3)
        {
            printf("Sorry, you cant continue, please contact your bank");
        }
    }
    return 0;
}


int fastCash(int amount)
{
    int choice;
    printf("1. $20.00\n2. 40.00\n3. 80.00\n4. 100.00\n5. Exit");
    scanf("%d", &choice);

    switch (choice)
    {
    case 1:
        amount = 20;

    case 2:
        amount = 40;

    case 3:
        amount = 80;

    case 4:
        amount = 100;

    case 5:
        break;
    }

    return amount;
}

int withdraw(int balence)
{
    int withdrawAmount;
    printf("Enter the amount you would like to withdraw: ");
    scanf("%d", &withdrawAmount);
    balence = -withdrawAmount;
    return balence;
}

int deposit(int balence)
{
    int depositAmount;
    printf("Enter an amount you would like to deposit: ");
    scanf("%d", &depositAmount);
    balence += depositAmount;
    return balence;
}

void checkBalence(int balence)
{
    printf("Your current balence is: %d\n", balence);
    return;
}
#包括
int快速现金(int金额);
国际存款(国际存款);
int取款(int打包);
无效支票余额(整数余额);
int main()
{
int引脚;
内部引脚=9999;
int pintrys=1;
int-receipt;
int选项;
int选项2;
int balence=300;
int快速现金选择;
printf(“输入pin:”;//pin是9999
scanf(“%d”和pin);

而(pinTries当您将
int
(或任何其他非指针变量)传递给函数时,您只传递它的一个副本。如果函数随后更改它(例如,
存款
),它只会更改传递的副本,不会影响原始变量。相反,您需要传递指向原始值的指针。例如:

int deposit(int* balance)
{
    int depositAmount;
    printf("Enter an amount you would like to deposit: ");
    scanf("%d", &depositAmount);
    *balance += depositAmount;
}
调用函数应该将指针传递到此变量,而不是变量本身:

case 3:
deposit(&balance);
/* Here-^ */
break;

在代码中,您似乎只是将押金传递给函数,但没有将其引用回原始
balence
变量,因此余额保持在300

例如,在函数中:

int deposit(int balence)
{
   int depositAmount;
   printf("Enter an amount you would like to deposit: ");
   scanf("%d", &depositAmount);
   balence += depositAmount;
   return balence;
}
您刚刚在中发送了
balence
,但正如Mureinik所说,您只是传入了原始值,而没有更改其值(它只更改了
存款()中的
balence

相反,您可以通过引用将其传入,也可以将
balence
作为全局变量移动到代码顶部,以便所有函数都可以看到它:

//code here.....
void checkBalence();

int balence = 300;
//more code here...
还要确保删除
deposit()
函数中的
balence
调用,以避免局部变量和全局变量之间的歧义

int deposit()
{
   /*..original code here..*/
}
…现在,在
deposit()
函数中,您的
balence
变量现在指向全局
balence

以下是最终的更正代码:

#include <stdio.h>

int fastCash(int amount);
int deposit();
int withdraw();
void checkBalence();

int balence = 300;

int main()
{
int pin;
int pins = 9999;
int pinTries = 1;
int reciept;
int options;
int options2;

int fastCashChoice;
printf("Enter your pin:");// the pin is 9999
scanf("%d", &pin);
while(pinTries <= 3)
{
   if(pin == pins)
   {
       printf("Would you like a reciept:");
       //1 is equal to yes and 2 is equal to no
       scanf("%d", &reciept);
       printf("Choose from the following:\n");
       printf("1. Fast cash\n2. Withdraw\n3. Deposit\n4. Check balence\n5. Get card back");
       scanf("%d", &options);
       while(options <= 5)
       {
           switch(options)
           {
               case 1:
               fastCash(fastCashChoice);
               balence = balence - fastCashChoice;
               break;

               case 2:
               withdraw(balence);
               break;

               case 3:
               deposit(balence);
               break;

               case 4:
               checkBalence(balence);
               break;

               case 5:
               options2 = 2;
               break;

           }
           printf("Would you like anohter transaction: ");// 1 is equal to yes and 2 is equal to no
           scanf("%d", &options2);
           if(options2 == 1)
           {
               printf("1. Fast cash\n2. Withdraw\n3. Deposit\n4. Check balence\n5. Get card back");
               scanf("%d", &options);
           }

           else
           {
               options = 5;
               pinTries = 4;
               printf("Thank you for useing this ATM, GoodBye\n");
               break;
           }
       }
   }

   else if(pin != pins)
   {
       printf("Invalid pin, try again:");
       scanf("%d", &pin);
       pinTries++;
   }

   if(pinTries == 3)
   {
       printf("Sorry, you cant continue, please contact your bank");
   }
}
return 0;
}


int fastCash(int amount)
{
int choice;
printf("1. $20.00\n2. 40.00\n3. 80.00\n4. 100.00\n5. Exit");
scanf("%d", &choice);

   switch(choice)
   {
       case 1:
       amount = 20;


       case 2:
       amount = 40;


       case 3:
       amount = 80;


       case 4:
       amount = 100;


       case 5:
       break;
}


return amount;
}

int withdraw()
{
int withdrawAmount;
printf("Enter the amount you would like to withdraw: ");
scanf("%d", &withdrawAmount);
balence -= withdrawAmount;
return balence;
}

int deposit()
{
  int depositAmount;
  printf("Enter an amount you would like to deposit: ");
  scanf("%d", &depositAmount);
  balence += depositAmount;
  return balence;
}

void checkBalence(int balence)
{
printf("Your current balence is: %d\n", balence);
return;
}

嗯,解决问题的一个简单方法是将
int-balence
声明为全局。您可以在
main()
函数上方声明它,而不是在
main()
函数中声明它


这将解决您当前的问题。

存款和取款都将返回更新后的
余额,但您不使用返回值。请尝试将调用更改为:

case 2:
    balence = withdraw(balence);
    break;

case 3:
    balence = deposit(balence);
    break;
fastCash
返回要提取的现金金额,因此您需要更新
main
中的余额:

case 1:
    balence = balence - fastCash(fastCashChoice);
    break;
这避免了指针(您需要额外的错误处理,即检查
NULL
)和全局变量(这会使您的程序更复杂*)

代码中还有一些问题。发送到
fastCash
的参数实际上根本没有使用,因为您返回了要提取的金额


*

为什么不按预期使用原始的
存款
(它返回更新的余额)?此外,如果
余额
,您的
存款
会导致UB。如果某人是初学者,简单的解释会更有帮助。全局变量不会以任何方式损害这段代码。
case 1:
    balence = balence - fastCash(fastCashChoice);
    break;