DEV Community

amberitas17
amberitas17

Posted on

How to Create an ATM Machine Using Functions in C Language

ATM Machine

We see how we cash out our ATM and how we deposit and withdraw our money in the ATM Machine. However, we didn't know how ATM programs were made. Now, in this blog, we are going to create an ATM Program using C language through Functions.

Although, C language is not widely used in the industry but this will be the reference for the future and maybe in college.

Step 1: Enter libraries needed (in this case, I used include<stdio.h> and include<stdlib.h>)



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


Enter fullscreen mode Exit fullscreen mode

Step 2: Enter the functions needed for the ATM (In this case, it needs to enter initial balance before we deposit or withdraw) - We have Menu, Initial Balance where users can input their initial balance, Deposit the Balance, Withdrawal, and Checking the Balance functions. We are going to use function pointers in which all of the variables will be stored and reflected in the functions.

I used integer type of variable for menu function, float type of variable for the initial balance, deposit, withdraw, and checking the balance



void menu(int *choice); //menu function
void initial(float *currbal); //initialize balance function
void deposit(float *currbal); //deposit function
void withdraw(float *currbal); //withdraw function
void checkbalance(float currbal); //checking the balance


Enter fullscreen mode Exit fullscreen mode

Step 3: Now we declared the functions, put the main() function to start the program.

Step 4: Inside the main() function, declare the variables needed in the main to declare the functions inside the main() function to show in the output. In this case, we use float type of variable for the balance and integer variable for the choices in the menu.



int choice;
float currbal;


Enter fullscreen mode Exit fullscreen mode

Now, we could declare first the menu function inside the main function. In this case, the int choice variable will be declared with an ampersand to store the variable whenever the user will enter their choice.



menu(&choice);


Enter fullscreen mode Exit fullscreen mode

Step 5: Now, I want it to be looped because ATM Machine is looped until the user will exit the program. So, we will use while loop with switch statement so that the program will identify what type of choices does the user input.



while(choice <=5){
        switch (choice) {
            case 1:
                initial(&currbal);
                menu(&choice);
                break;
            case 2:
                deposit(&currbal);
                menu(&choice);
                break;
            case 3:
                withdraw(&currbal);
                menu(&choice);
                break;
            case 4:
                checkbalance(currbal);
                menu(&choice);
                break;
            case 5:
                printf("Thank you for using our program");
                exit(0);
                break;
            default:
                printf("invalid Choice");
                menu(&choice);
                break;
       }
    }


Enter fullscreen mode Exit fullscreen mode

I used integer type of choices because in ATM, numbers are the only shown in the keypad. So, I used 1-5 choices, then in the exit choice, I directed it to exit function to end the program

Step 6: Now we have declared the functions in the main() function, let's create the menu function. Declare the menu function which is void menu (int *choice). Then input the details in the menu function. Don't forget to input the return parameter to return the value.



void menu(int *choice){
    printf("\n[1] Initialize \n");
    printf("[2] Deposit\n");
    printf("[3] Withdraw\n");
    printf("[4] Display\n");
    printf("[5] Exit\n");
    printf("\nSelect your transaction: \n\n");
    scanf("%d", &*choice);
    return;
}


Enter fullscreen mode Exit fullscreen mode

I put the &*choice in declaring the scanf function to store the variable that the user will enter.

Step 7: Now we have created the functionalities of the menu function, let's create the initial balance function. I want the user to enter their initial balance when they choose the initialize choice in the menu. Declare the initial balance function which is void initial(float *currbal). Then, inside the initial balance function, input the details where the user will enter the initial balance. The entered amount is equals to their current balance.



void initial(float *currbal){
    printf("Input an amount: \n");
    scanf("%f", &*currbal);//currbal will store it's input
    printf("\nYour balance is Php %.2f\n", *currbal);
    return;//this will return the function call
}


Enter fullscreen mode Exit fullscreen mode

I put the &*currbal in declaring the scanf function to store the variable that the user will enter. Also, in the print function, I put asterisk in the currbal variable to store the variable and to reflect the user's input.

Step 8: Now we have created the functionalities of the initial balance function, let's create the deposit function. In the deposit function, the user will enter the amount for depositing the balance. So, the entered amount for deposit will add to the initialize balance function. So, we will also declare another variable for depositing the amount.



void deposit(float *currbal){
    float depositAmount;
    printf("Enter an amount you would like to deposit: ");
    scanf("%f", &depositAmount);
    *currbal += depositAmount;//the currbal was stored in the initialize function
    printf("\nYour balance is Php %2.f\n", *currbal);
    return;//this will return the function call
}


Enter fullscreen mode Exit fullscreen mode

Now, I put the *currbal in declaring the currbal variable in adding with the depositAmount variable to add and to store the variable that the user will enter. Also, in the print function, I put asterisk in the currbal variable to store the variable and to reflect the user's input.

Step 9: Now we have created the functionalities of the deposit function, let's create the withdraw function. In the withdraw function, the user will enter the amount for withdrawing the balance. So, the entered amount for withdraw will subtracting to the initialize balance function. So, we will also declare another variable for withdrawing the amount.



void withdraw(float *currbal){
    float withdrawAmount;
    printf("Enter the amount you would like to withdraw: ");
    scanf("%f", &withdrawAmount);
    *currbal -= withdrawAmount;//the currbal was stored in the initialize function
    printf("\nYour remaining balance is Php %.2f \n", *currbal);
    if (*currbal<withdrawAmount){
        printf("Sorry, you do not have enough balance. Please try again. . . \n");
    }
    return;
}


Enter fullscreen mode Exit fullscreen mode

I want to input if the user has a low balance and the user entered higher than the withdrawAmount variable, it will create a message for a low balance. So, I used if then statement for that error message. Now, I put the *currbal in declaring the currbal variable in subtracting with the withdrawAmount variable to subtract and to store the variable that the user will enter. Also, in the print function, I put asterisk in the currbal variable to store the variable and to reflect the user's input.

Step 10: Now we have created the functionalities of the withdraw function, let's create the checking the balance function. In the check balance function, if the user entered the menu choice for checking the balance function. This will display the current balance. In this case, we will not use the function pointers here because it reflected the variable's storage.



void checkbalance(float currbal){
    printf("Your current balence is: %.2f\n", currbal);
    return;
}


Enter fullscreen mode Exit fullscreen mode

Now, Voila!! We have created an ATM Program using C language Function pointers. Although, this will improve more because it is limited but at least we have created an ATM Program. I have finished Introduction to C language Subject in college and going to the intermediate programming. I shared to you how I created my final project in Introduction to C language.

Happy learning!

Source code: https://github.com/amberitas17/intro-to-c-language-projects/blob/main/Cubacub_final.c

Top comments (2)

Collapse
 
pauljlucas profile image
Paul J. Lucas

void menu(int *choice);

You really should just return the value:

int menu(void);

scanf("%d", &*choice);

If choice is a pointer, just write:

scanf("%d", choice);

You repeat the call to menu() in every case. You could more simply do:

while ( (choice = menu()) <= 5 ) {

Collapse
 
amberitas17 profile image
amberitas17

thank you for your advice!! as I said in my ending post, this was my final project in my introduction to c language in my college and needed to be improved. Although my professor is so strict at that because he discussed 1/4 of the Introduction to C language and it was merely a beginner's project... I appreciate the advice