Home > Back-end >  How to return value to one function from another
How to return value to one function from another

Time:10-26

I am trying to create an ATM banking system which has options like Deposit, Withdraw, Check balance etc. I kept a fixed balance value at first. Then make that balance go through withdraw, deposit function. But when I checked my balance, it's showing that same fixed value at first, it's not updating. When I try to get out of the function, I use mainmenu() at first, but if I replace it with return balance. My code doesn't work properly. Where is my mistake and what can I do to fix it. Here's my code:

void mainmenu()

{

    int option, choose, balance = 10000;

    printf("PRESS < 1 >  CHECK BALANCE\n");
    printf("PRESS < 2 >  DEPOSIT\n");
    printf("PRESS < 3 >  WITHDRAW\n");
    printf("PRESS < 4 >  SEMESTER FEE\n");
    printf("CHOOSE AN OPTION: ");
    scanf("%d", &option);


    switch (option)
    {
        case 1:
            checkBalance(balance);
            break;

        case 2:
            moneyDeposit(balance);
            break;

        case 3:
            moneyWithdraw(balance);
            break;

        default:
        printf("WRONG INPUT. PLEASE PRESS ANY KEY AND ENTER TO TRY AGAIN....");
        char option[10];
        scanf("%s", &option);
        mainmenu();
    }

 
    void checkBalance(float balance)
    {
        printf("YOU CHOOSE TO SEE YOUR BALANCE\n");
        printf("****YOUR AVAILABLE BALANCE IS:   $%.2f\n", balance);
    }

    float moneyDeposit(float balance)
    {
        system("cls");

        float deposit;
        gotoxy(28, 2);
        printf("YOU CHOOSE TO DEPOSIT MONEY\n");
        gotoxy(22, 4);
        printf("$$$$YOUR BALANCE IS: $%.2f\n\n", balance); 
        gotoxy(22, 7);
        printf("****ENTER YOUR AMOUNT TO DEPOSIT: ");
        scanf("%f", &deposit);

    balance  = deposit;
    printf("****YOUR NEW BALANCE IS:   $%.2f\n\n", balance);
    printf("WOULD YOU LIKE TO DO ANOTHER TRANSACTION:\n");
    gotoxy(17, 14);
    printf("< 1 > YES\n");
    gotoxy(17, 16);
    printf("< 2 > NO\n");
    int option;

    do
    {
        gotoxy(10, 18);
        printf("PLEASE CHOOSE YOUR OPTIONS...");
        scanf("%d", &option);
        if (option == 1)
            {
                mainmenu;
            }
        else if(option == 2)
            {
                menuExit();
            }
        else
            {
                printf("INVALID INPUT. PLEASE PRESS ANY KEY AND ENTER TO TRY AGAIN");
                char choose[10];
                scanf("%s", &choose);
            }
    } while (option > 2);
    }
    loat moneyWithdraw(float balance)
    {

    float withdraw;

gotoxy(28, 2);
printf("YOU CHOOSE TO WITHDRAW MONEY\n");
gotoxy(22, 4);
printf("$$$$YOUR BALANCE IS: $%.2f\n\n", balance);
gotoxy(22, 6);
printf("ENTER YOUR AMOUNT TO WITHDRAW:");
scanf("%f", &withdraw);


if (withdraw <= balance)
    {
        balance -= withdraw;
        gotoxy(28, 9);
        printf("$$$$YOUR WITHDRAWING MONEY IS:  $%.2f\n", withdraw);
        gotoxy(28, 11);
        printf("****YOUR NEW BALANCE IS:   $%.2f\n\n", balance);


    }

else
    {
        gotoxy(28, 9);
        printf("   YOU DON'T HAVE ENOUGH MONEY   \n");
        gotoxy(28, 11);
        printf("****YOUR CURRENT BALANCE IS:   $%.2f\n\n", balance);
        gotoxy(28, 13);
        printf("PLEASE DEPOSIT MONEY\n");

    }

gotoxy(17, 16);
printf("WOULD YOU LIKE TO DO ANOTHER TRANSACTION:\n");
gotoxy(17, 18);
printf("< 1 > YES\n");
gotoxy(17, 20);
printf("< 2 > NO\n");
int option;

    do
        {
            gotoxy(10, 22);
            printf("PLEASE CHOOSE YOUR OPTIONS...");
            scanf("%d", &option);
            if (option == 1)
                {
                    mainmenu();
                }
            else if(option == 2)
                {
                    menuExit();
                }
            else
                {
                    printf("INVALID INPUT. PLEASE PRESS ANY KEY AND ENTER TO TRY AGAIN");
                    char choose[10];
                    scanf("%s", &choose);
                }
        } while (option > 2);


}

CodePudding user response:

When you pass a variable as an argument to a function, that variable is copied and therefore you operate on the copy of balance variable and when you return from the function that copy is released.

To work around that you need to either pass a pointer to the variable:

void moneyDeposit(float *balance){ // * indicates it is a pointer to float
    ...
    *balance  = deposit; // with * you refer to the value under pointer rather than to the pointer itself 
}
moneyDeposit(&balance); //& gets an address of the variable

or return the changed value and assign it back to the variable:

float moneyDeposit(float balance){
    return balance deposit;
}
balance=moneyDeposit(balance);
  • Related