Welcome

Hi! This blog is for beginners of c++ and java .You can check and pick codes for your programming assignments. This will also help you to understand the concepts of these programming languages. Its all about programming.

Simple code for ATM - C++

Tuesday, July 26, 2011


#include<iostream>
#include<conio.h>
#include<string>

using namespace std;
class atm
{
   private:
      float balance;
   public:
      atm()
      {
         balance=0;
      }
      void deposit(float a)
      {
         cout<<"Enter amount you want to deposit"<<endl;
         cin>>a;
         balance=balance+a;
      }
      void withdraw(float b)
      {
         cout<<"Enter money you want to withdraw"<<endl;
         cin>>b;
         if(b>balance)
            cout<<"Your balance is not enough to continue transaction"<<endl;
         else
            balance=balance-b;
      }
      float showBalance()
      {
         return balance;
      }
};

void main()
{
   atm ob;
   float y=0,z=0;
   char choice;
   string password="";
   char a;
   cout<<"Enter your password:"<<endl;
   do
   {
      a=_getch();
      switch(a)
      {
         case '\b':     // "\b" is used for BACKSPACE
            if(password.length()>0)
               {
                  password.erase(password.length()-1);
                  cout<<'\b'<<" "<<'\b';
                  break;
               }
         default:
            if(a>31&&a<127)        // 31 to 127 are ASCII code for keyboard keys    
               {
                  password.push_back(a);
                  cout<<"*";
               }
     }
   }
   while(a!='\r');           // "\r" is used for ENTER
   if(password=="tzt")
      {
         cout << "\n^^^^Access granted^^^^ \n";
         cout<<"Your current account balance is 0.\n"<<endl;
         do
         {
            int opt;
            cout<<"Enter 1 if you want to deposit money."<<endl;
            cout<<"Enter 2 if you want to withdraw money."<<endl;
            cin>>opt;
            switch(opt)
               {
                  case 1:
                        ob.deposit(y);
                        break;
                  case 2:
                        ob.withdraw(z);
                        break;
                  default:
                        cout<<"Wrong entry!"<<endl;
                        break;
               }
           cout<<"Your account balance:"<<ob.showBalance()<<endl;
           cout<<"******PRESS******\ny to continue\nn to terminate\n";
           cin>>choice;
        }
       while(choice!='n');
     }
   else
   {
      cout << "\nAccess denied...\n";
   }
   cout<<"*********GOOD BYE*********"<<endl;
   _getch();  
}