Thread: How do I get my ATM Machine to accept PIN input only a total of 3 tries?

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    1

    How do I get my ATM Machine to accept PIN input only a total of 3 tries?

    How do I get my ATM Machine to accept PIN input only a total of 3 tries?

    Here is my code

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    
    int main(){
    
    
     int balance = 15000;
     int pin = 1016;
     int pin2;
     int newtransaction = 1; //new transaction , 2 end transaction
     
     
     
     printf("***Welcome To Stehl's ATM Machine*** \n");
     printf("Enter Pin: ");
     scanf("%d", &pin2);
     
     if(pin != pin2)
     
     {
     	printf("Pin not Valid.");
     	return 0;
     }
     
     
    	 while(newtransaction == 1)
    	 {
    	 int option;
    	 
    	 printf("Please pick an option \n");
    	 printf("1 - Check your balance \n");
    	 printf("2 - Deposit\n");
    	 printf("3 - Withdraw\n");
    	 scanf("%d" , &option );
    	  
    	  if (option == 1)//balance
    	  
    	  {
    	  	printf("Your current balance is : %d \n", balance);
    	  }
    	  
    	  else if (option == 2)//deposit
    	  {
    	  	  int deposit;
    	  	  
    	  	  
    		  printf("Plese input deposit amount: ");
    		  scanf("%d", &deposit);
    		
    		  if(deposit >0 )
    		  {
    		  balance += deposit;
    		  printf("Your total balance is %d \n ", balance);
              }
    
    
              else
              {
              	printf("Invalid amount\n");
    		  }
    		   
    	  }
    	   
    	  else if (option == 3)//withdraw
    	  {
    	  	int withdraw;
    	  	
    	  	printf("Please input withdraw amount: ");
    	  	scanf("%d", &withdraw);
    	  	
    	  	if(withdraw <= balance)
    	  	{
    		balance -= withdraw;
    		printf("Your total balance is %d \n", balance);
    		}
    		else {
    		    	if (withdraw > balance)
    		    	{
    				printf("Insufficient Balance. Declined \n");
    		    	}
    		    }
    	  } 
    	  else
    	  {
    	  	printf("Invalid Transaction.\n");
    	  }
    	  
    	  newtransaction = 0;
    	  
    	  while(newtransaction != 1 && newtransaction !=2)
    	  {
    	  printf("Do you want a new transaction?\n");
    	  printf("1 - Yes\n");
    	  printf("2 - No\n");
    	  scanf("%d", &newtransaction);
          }
    	}
    
    
    
    
    
    
      getch();
      return 0;
      
    }

  2. #2

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    @nerio: ATM simulation is a common programming exercise, so it shouldn't be surprising to see many variations of this theme throughout the forum.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Quote Originally Posted by Stehl Soriano View Post
    How do I get my ATM Machine to accept PIN input only a total of 3 tries?
    You already know how to make code repeat: use a loop.

    If you only want to allow a certain number of tries, incorporate a counter into the loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array add total rows and total colms
    By gameover6005 in forum C Programming
    Replies: 1
    Last Post: 04-16-2013, 11:46 PM
  2. inline asm with machine code, non-mnemonic input?
    By wenxinleong in forum C Programming
    Replies: 11
    Last Post: 03-01-2011, 03:12 PM
  3. Can a pointer accept user input?
    By Programmer_P in forum C++ Programming
    Replies: 24
    Last Post: 07-31-2009, 10:16 AM
  4. Replies: 8
    Last Post: 02-14-2009, 11:33 PM
  5. How to accept input in graphics mode
    By nurulsiddik in forum C Programming
    Replies: 1
    Last Post: 02-13-2002, 10:40 AM