2

I've been writing the code for an ATM inter face where i include a pin that must be imputed by the user to access the options. Once thats done, I get 5 options to choose from such as fast cash, withdraw, deposit and check balance. everything seems to be running good the only thing is that the account balance is not be updated to the correct amount when the user deposits, withdraws, or gets fast cash. Can someone help me fix this. I'll post my code down below

#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;
}
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
PenaA
  • 43
  • 1
  • 4

4 Answers4

3

When you pass an int (or any other non-pointer variable, for that matter) to a function, you only pass a copy of it. If the function then changes it (as deposit, e.g., does), it will only change the passed copy, and won't affect the original variable. Instead, you need to pass a pointer to the original value. E.g.:

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

And the calling function should pass the pointer to this variable instead of the variable itself:

case 3:
deposit(&balance);
/* Here-^ */
break;
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Why not use the original `deposit` as intended (it returns the updated balance)? Also, your `deposit` causes UB if `balance` is `NULL`. – simon Dec 13 '15 at 10:29
2

In your code, you seem to just pass in the deposit to the function, but you don't reference it back to the original balence variable, so the balance stays at 300.

For example, in the function:

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

You just sent in your balence in, but like how Mureinik said, you just passed in the original value without changing its value (it only changed the balence inside of deposit().

Instead, you can pass it in by reference, or you can move balence to the top of the code as a global variable so that all the functions can see it:

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

int balence = 300;
//more code here...

Also make sure to remove the balence call in the deposit() function to avoid ambiguity between the local and global variables..

int deposit()
{
   /*..original code here..*/
}

...and now, in the deposit() function, your balence variable now points to the global balence.

Here is the final, corrected code:

#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;
}

Now, it should run as expected, here producing a final balance of $176:

Enter your pin:9999
Would you like a reciept:1
Choose from the following:
1. Fast cash
2. Withdraw
3. Deposit
4. Check balence
5. Get card back2
Enter the amount you would like to withdraw: 124
Would you like anohter transaction: 1
1. Fast cash
2. Withdraw
3. Deposit
4. Check balence
5. Get card back4
Your current balence is: 176
Patrick Yu
  • 972
  • 1
  • 7
  • 19
1

Well, an easy solution to your problem is to declare your int balence as global. Instead of declaring it inside main() function, you can declare it above the main() function.

This will solve your current problem.

Anish Sharma
  • 495
  • 3
  • 15
1

Both deposit and withdraw returns the updated balance but you don't use the return value. Try changing the calls to:

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

case 3:
    balence = deposit(balence);
    break;

fastCash returns the amount of cash to withdraw, so you need to update the balance in main:

case 1:
    balence = balence - fastCash(fastCashChoice);
    break;

This avoids both pointers (for which you would want additional error handling, i.e. check for NULL) and global variables (which makes your program more complex*).

There are also a few more problems in your code. The argument sent to fastCash isn't really used at all, since you return the amount to withdraw.

* Are global variables bad?

Community
  • 1
  • 1
simon
  • 2,042
  • 2
  • 20
  • 31
  • If someone is a beginner, a simple explanation would be more helpful. Global variable will not harm this piece of code in any way. – Anish Sharma Dec 13 '15 at 15:21