Mimicking the working of an ATM machine in C

Posted in: C by musiitwa joseph 2015-11-12 at 20:39:45

image for Mimicking  the working of an ATM machine in C The program allows the user to enter his/her account number and pin number, if they are correct it displays the menu below else it informs the user that he/she has entered wrong account number or pin number. This menu allows the user to select the amount he/she wants to withdraw. The person enter 1 to withdraw 10,000/= or 2 to withdraw 20,000/= and so on. For a person to withdraw money, the program must first check if there is enough money on the account. If true the transaction proceeds causing a decrease of the money withdrew and a bank charge of 300/= on the account. A message informing the user his/her balance is displayed
#include <stdio.h>
int main(){
	
	/*A program that mimics the working an ATM machine*/

	//declaring and initializing the account number and pin number
	int pin_number=0, machine_pin, amount_number;
	long int account_number, machine_number, balance;
	
	//displaying a welcome message to the user	
	printf("\nYOU ARE WELCOME TO CODEEAGLES BANK\n");

	start:

	//getting the account number from the user
	printf("Please enter your account number: ");
	scanf("%ld", &account_number);

	//getting the pin number from the user
	printf("Please enter your pin number: ");
	scanf("%d", &pin_number);


	//creating a file handle
	FILE *fh;
	
	//opening the file for reading
	if((fh = fopen("ATM/account number.txt", "r")) == NULL ){
		printf("Error in opening pin number file");
	}else{
		fscanf(fh, "%ld", &machine_number);
		fclose(fh);
	}

	if((fh = fopen("ATM/pin number.txt", "r")) == NULL ){
		printf("Error in opening account number file");
	}else{
		fscanf(fh, "%d", &machine_pin);
		fclose(fh);
	}


	if(!(account_number == machine_number && pin_number == machine_pin )){
		printf("\nWRONG PASSWORD OR USERNAME\n\n");
		printf("a");
		goto start;
	}else{
		do{
			printf("\nEnter:\n");
			printf("\t1. 10,000\n");
			printf("\t2. 20,000\n");
			printf("\t3. 30,000\n");
			printf("\t4. 40,000\n");
			printf("\t5. 50,000\n\t");
		
			scanf("%d", &amount_number);
		
			if(amount_number > 5 || amount_number < 1){
				printf("\nPlease enter a number between 1 and 5\n");
			}
		}while(amount_number > 5 || amount_number < 1);
	}
	
	if((fh = fopen("ATM/balance.txt", "r")) == NULL ){
		printf("Error in opening balance file");
	}else{
		fscanf(fh, "%ld", &balance);
		fclose(fh);
	}
	
	if((amount_number*10000+300) > balance){
		printf("You donot have enough money on your account");
	}else{
		if((fh = fopen("ATM/balance.txt", "w")) == NULL ){
			printf("Error in opening balance file");
		}else{
			balance = balance - amount_number*10000 - 300;
			fprintf(fh, "%ld", balance);
			fclose(fh);
		}

		printf("\nYour balance is: %ld\nThanx for banking with Us.\n", balance);	
	}	
	
	
	
	printf("\n");
	return 0;
}

Tags:
c

Search

Comments(2)


The program works fine but I do not have the pin n...Read moreThe program works fine but I do not have the pin number
less

from: Charles at: 2016-04-24 , 05:23:38 AM

use the number found in text file called pin numbe...Read moreuse the number found in text file called pin number as the pin number of the account
less

from: joseph at: 2016-08-24 , 01:01:28 PM


Leave a comment

Fields with * are required
Your email address will not be displayed
5 + 1 =