How to Create an ATM Machine Using Functions in C Language
Tags: c, functions, programming, beginners
Published: 2022-01-07 03:42:53


![ATM Machine](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cmhui7eby0pquxwwjrso.png)

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>`)

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


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

```c
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
```


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.

```c
int choice;
float currbal;
```
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.

```c
menu(&choice);
```


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. 

```c
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;
       }
    }
```
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.

```c
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;
}
```
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.

```c
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
}
```
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.

```c
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
}
```
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.

```c
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;
}
```
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. 

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

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