Thread: ATM C++ Using functions and cases

  1. #1
    Registered User GrafixFairy's Avatar
    Join Date
    Mar 2014
    Posts
    45

    ATM C++ Using functions and cases

    This is an assignment of mine, its not perfect but im just posting it..maybe it'll help someone else out...

    Code:
    #include <iostream>
    #include <cmath>
    #include <cstdlib>
    #include <iomanip> 
    
    
    using namespace std;
    
    
    /*function prototypes*/
    int mainMenu();
    int menu();
    void displayBalance();
    void withdrawFunds();
    void depositFunds();
    
    
    /*global variables*/
      double balance= 250000.00;
    
    
    int main(){
    
     cout << fixed << showpoint << setprecision(2); //Set the numeric output formatting
         
        // Declaring variables 
    
    
        int ac_number= 258647;
        int pin= 33284;
    
    
                 //Prompts user to enter Account number and PIN
            cout <<"\t**********************************************************\n" << endl
                <<" \t\t    THE 1st CHOICE INTERNATIONAL BANK\n\n\t\t\t ------* WELCOME *------\n" << endl 
                 <<"\t**********************************************************\n\n"
                 <<"\t\t\t\tUSER LOGIN" << endl
                 << "\n\t\t\t------ Enter Account Number: ";
               cin  >> ac_number;
            cout << "\n\t\t\t------ Enter PIN: ";
               cin  >> pin;
            cout << "\n";
    
    
            while (ac_number != 258647 || pin != 33284) 
      {     
                        system ("CLS"); // Clears screen
                 
              cout <<"\t**********************************************************\n" << endl
         <<" \t\t    THE 1st CHOICE INTERNATIONAL BANK\n\n\t\t\t ------* WELCOME *------\n" << endl 
                 <<"\t**********************************************************\n\n"
                 <<"\t\t\t\tUSER LOGIN\n" << endl
                 <<"\t\tINVALID ACCOUNT NUMBER OR PIN WAS ENTERED\n" << endl
                 << "\n\t\t\t------ Enter Account Number: ";
               cin  >> ac_number;
            cout << "\n\t\t\t------ Enter PIN: ";
               cin  >> pin;
            cout << "\n";
      }//end while
    
    
                system ("CLS");
                menu(); //function call
     }//end main()
    
    
     /*------------------------------------------------------------------------------------------------*/
      int menu(){
    
    
            switch(mainMenu()){
             
      case 1:
     system("cls");
         displayBalance();
         menu(); //function call
      break;
             
      case 2:
     system("cls");
         withdrawFunds();
         menu(); //function call
      break;
    
    
      case 3:
     system("cls");
         depositFunds();
         menu(); //function call
      break;
    
    
      case 4:
         exit(0);
      break; 
    
    
       default:    
         cout <<"\nINVALID ENTRY.... Please Select numbers 1 - 4\n" <<endl <<endl;
         system("pause");
         menu(); //function call
      break;      
             
    }//end switch
    
    
    system("pause");   
    return 0;    
    
    
    }//end menu()
    
    
    /*---------------------------------*****function definition*****---------------------------------------*/
    int mainMenu(){
      
            system("cls");
    
    
        int input;
        
      cout <<"\t____________________________________________\n" << endl         /* Main menu begins here*/
                 <<"\t"<<"     *************  MENU  *************" << endl           /*prompts user to make choice*/
                 <<"\t____________________________________________\n\n" << endl;
    
    
      cout <<"\tPlease Enter the number for the Transaction\n" << endl
                 << "\t\tPress 1. ----->> Balance Inquiry\n" << endl
                 << "\t\tPress 2. ----->> Withdrawal\n" << endl
                 << "\t\tPress 3. ----->> Deposit\n" << endl
                 << "\t\tPress 4. ----->> Exit\n" << endl;
    
      cout <<"\t_________________________________________\n" << endl
                 << "\t ENTER TRANSACTION NUMBER:- ";            
      cin >> input;
    
    
      return input;
    }//end mainMenu()
    
    
    /* --------------------------------*****function definition*****--------------------------------------*/
      void displayBalance(){
                
                    cout << "\nAccount Balance is: $" << balance <<"\n" << endl;
    
    
                    system ("PAUSE");
                    system ("CLS"); 
      }//end displayBalance()
    
    
    /*------------------------------------*****function definition*****------------------------------------*/
      void withdrawFunds(){
    
    
           double withdraw = 0.0;
    
    
                    /* Withdraw Menu beings here & prompts user to make chice*/
            cout << "\tYou have opted to withdraw money\n" << endl            
                 << " Please Enter amount you with to withdraw --Type 0 to return to menu: $";
            cin  >> withdraw;
            cout <<"\n";
    
    
            system ("cls");                 
                        
                 if (withdraw == 0){
            
                        system("cls");
                     
                        menu(); //function call
                 }  //end if
    
    
                 else if (withdraw != 0){
           
                 if ( withdraw > balance){          
            
                            system ("CLS");  
    
    
            cout << "Value Exceeds Current balance Please enter smaller amount\n" << endl
                 << "Please Enter Amount you Wish to Withdraw: $";
            cin >> withdraw;
                             system ("CLS");  
    
    
                if (withdraw == 0){
            
                        system("cls");
                     
                        menu(); //function call
                 } //end if
    
    
            cout << "\nYou Have Withdrawn: $" << withdraw << " From your Accout" <<"\n"<< endl;
                  balance = balance - withdraw;
            cout << "Balance is now: $" << balance <<"\n" << endl;
            
                            system ("PAUSE");
                            system ("CLS");  
                 } //end if
    
    
                  else {
            
            cout << "\nYou Have Withdrawn: $" << withdraw << " From your Accout" << endl;
                balance = balance - withdraw;
            cout << "\nBalance is now: $" << balance <<"\n" << endl;
            
                            system ("PAUSE");
                            system ("CLS"); 
          } //end else
        }//end else if
      }//end withdrawFunds()
    
    
    /*-----------------------------------*****function definition*****--------------------------------------*/
        void depositFunds(){
    
    
            double deposit = 0.0; 
    
    
    /* Deposit Menu beings here & prompts user to make chice*/
            cout << "\tYou Have Opted to Deposit Money\n" << endl
                 << " Please Enter Amount You Wish to Deposit --Type 0 to return to Menu: $";
            cin  >> deposit;
            cout <<"\n";
    
    
                        system ("cls");            
                        
                 if (deposit == 0){
            
                        system("cls");
                     
                        menu(); //function call
                 }
            
                 else if (deposit != 0){
    
    
                        balance= balance + deposit;
                       
            cout << "You Have Successfully added $" << deposit << " to your Account\n" << endl;
            cout << "Balance is Now $" << balance << "\n" <<endl;    
    
    
                        system("pause");
                        system ("cls");
        }
      }//end depositFunds()
    /*----------------------------------------------------------------------------------------------------*/

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Properly indented:
    Code:
    #include <iostream>
    #include <cmath>
    #include <cstdlib>
    #include <iomanip>
    
    
    using namespace std;
    
    
    /*function prototypes*/
    int mainMenu();
    int menu();
    void displayBalance();
    void withdrawFunds();
    void depositFunds();
    
    
    /*global variables*/
    double balance = 250000.00;
    
    
    int main()
    {
    
    	cout << fixed << showpoint << setprecision(2); //Set the numeric output formatting
    
    	// Declaring variables
    
    
    	int ac_number = 258647;
    	int pin = 33284;
    
    
    	//Prompts user to enter Account number and PIN
    	cout << "\t**********************************************************\n" << endl
    		<< " \t\t    THE 1st CHOICE INTERNATIONAL BANK\n\n\t\t\t ------* WELCOME *------\n" << endl
    		<< "\t**********************************************************\n\n"
    		<< "\t\t\t\tUSER LOGIN" << endl
    		<< "\n\t\t\t------ Enter Account Number: ";
    	cin >> ac_number;
    	cout << "\n\t\t\t------ Enter PIN: ";
    	cin >> pin;
    	cout << "\n";
    
    
    	while (ac_number != 258647 || pin != 33284)
    	{
    		system("CLS"); // Clears screen
    
    		cout << "\t**********************************************************\n" << endl
    			<< " \t\t    THE 1st CHOICE INTERNATIONAL BANK\n\n\t\t\t ------* WELCOME *------\n" << endl
    			<< "\t**********************************************************\n\n"
    			<< "\t\t\t\tUSER LOGIN\n" << endl
    			<< "\t\tINVALID ACCOUNT NUMBER OR PIN WAS ENTERED\n" << endl
    			<< "\n\t\t\t------ Enter Account Number: ";
    		cin >> ac_number;
    		cout << "\n\t\t\t------ Enter PIN: ";
    		cin >> pin;
    		cout << "\n";
    	}//end while
    
    
    	system("CLS");
    	menu(); //function call
    }//end main()
    
    
    /*------------------------------------------------------------------------------------------------*/
    int menu()
    {
    
    
    	switch (mainMenu())
    	{
    
    		case 1:
    			system("cls");
    			displayBalance();
    			menu(); //function call
    			break;
    
    		case 2:
    			system("cls");
    			withdrawFunds();
    			menu(); //function call
    			break;
    
    
    		case 3:
    			system("cls");
    			depositFunds();
    			menu(); //function call
    			break;
    
    
    		case 4:
    			exit(0);
    			break;
    
    
    		default:
    			cout << "\nINVALID ENTRY.... Please Select numbers 1 - 4\n" << endl << endl;
    			system("pause");
    			menu(); //function call
    			break;
    
    	}//end switch
    
    
    	system("pause");
    	return 0;
    
    
    }//end menu()
    
    
    /*---------------------------------*****function definition*****---------------------------------------*/
    int mainMenu()
    {
    
    	system("cls");
    
    
    	int input;
    
    	cout << "\t____________________________________________\n" << endl         /* Main menu begins here*/
    		<< "\t" << "     *************  MENU  *************" << endl           /*prompts user to make choice*/
    		<< "\t____________________________________________\n\n" << endl;
    
    
    	cout << "\tPlease Enter the number for the Transaction\n" << endl
    		<< "\t\tPress 1. ----->> Balance Inquiry\n" << endl
    		<< "\t\tPress 2. ----->> Withdrawal\n" << endl
    		<< "\t\tPress 3. ----->> Deposit\n" << endl
    		<< "\t\tPress 4. ----->> Exit\n" << endl;
    
    	cout << "\t_________________________________________\n" << endl
    		<< "\t ENTER TRANSACTION NUMBER:- ";
    	cin >> input;
    
    
    	return input;
    }//end mainMenu()
    
    
    /* --------------------------------*****function definition*****--------------------------------------*/
    void displayBalance()
    {
    
    	cout << "\nAccount Balance is: $" << balance << "\n" << endl;
    
    
    	system("PAUSE");
    	system("CLS");
    }//end displayBalance()
    
    
    /*------------------------------------*****function definition*****------------------------------------*/
    void withdrawFunds()
    {
    
    
    	double withdraw = 0.0;
    
    
    	/* Withdraw Menu beings here & prompts user to make chice*/
    	cout << "\tYou have opted to withdraw money\n" << endl
    		<< " Please Enter amount you with to withdraw --Type 0 to return to menu: $";
    	cin >> withdraw;
    	cout << "\n";
    
    
    	system("cls");
    
    	if (withdraw == 0)
    	{
    
    		system("cls");
    
    		menu(); //function call
    	}  //end if
    
    
    	else if (withdraw != 0)
    	{
    
    		if (withdraw > balance)
    		{
    
    			system("CLS");
    
    
    			cout << "Value Exceeds Current balance Please enter smaller amount\n" << endl
    				<< "Please Enter Amount you Wish to Withdraw: $";
    			cin >> withdraw;
    			system("CLS");
    
    
    			if (withdraw == 0)
    			{
    
    				system("cls");
    
    				menu(); //function call
    			} //end if
    
    
    			cout << "\nYou Have Withdrawn: $" << withdraw << " From your Accout" << "\n" << endl;
    			balance = balance - withdraw;
    			cout << "Balance is now: $" << balance << "\n" << endl;
    
    			system("PAUSE");
    			system("CLS");
    		} //end if
    
    
    		else
    		{
    
    			cout << "\nYou Have Withdrawn: $" << withdraw << " From your Accout" << endl;
    			balance = balance - withdraw;
    			cout << "\nBalance is now: $" << balance << "\n" << endl;
    
    			system("PAUSE");
    			system("CLS");
    		} //end else
    	}//end else if
    }//end withdrawFunds()
    
    
    /*-----------------------------------*****function definition*****--------------------------------------*/
    void depositFunds()
    {
    
    
    	double deposit = 0.0;
    
    
    	/* Deposit Menu beings here & prompts user to make chice*/
    	cout << "\tYou Have Opted to Deposit Money\n" << endl
    		<< " Please Enter Amount You Wish to Deposit --Type 0 to return to Menu: $";
    	cin >> deposit;
    	cout << "\n";
    
    
    	system("cls");
    
    	if (deposit == 0)
    	{
    
    		system("cls");
    
    		menu(); //function call
    	}
    
    	else if (deposit != 0)
    	{
    
    
    		balance = balance + deposit;
    
    		cout << "You Have Successfully added $" << deposit << " to your Account\n" << endl;
    		cout << "Balance is Now $" << balance << "\n" << endl;
    
    
    		system("pause");
    		system("cls");
    	}
    }//end depositFunds()
    /*----------------------------------------------------------------------------------------------------*/
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User GrafixFairy's Avatar
    Join Date
    Mar 2014
    Posts
    45
    ERROR NOTICE : There is an error in withdrawFunds(), if you enter a value exceeding the balance more than once it will give you a negative number the second time around.
    You can fix this with a [while] for the error msg.

    I cannot locate the file with the correction.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Have you learned about C++ reference parameters? If so, it should not be difficult for you to make balance into a local variable. Generally, global variables should be avoided because they make it harder for you to reason about and maintain your code.

    I suggest eliminating the use of recursion in the menu function by replacing it with a loop. With such a change, you should be able to eliminate the recursive call to menu in the other functions too, i.e., just by returning.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User GrafixFairy's Avatar
    Join Date
    Mar 2014
    Posts
    45
    Quote Originally Posted by laserlight View Post
    Have you learned about C++ reference parameters?
    I haven't yet, im am still at the introductory stage. I guess i will when i begin programming 1 next semester.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by GrafixFairy
    I haven't yet, im am still at the introductory stage. I guess i will when i begin programming 1 next semester.
    You should learn them within the next few lessons. For now, what you can do is to change the functions that deal with balance:
    Code:
    void displayBalance();
    void withdrawFunds();
    void depositFunds();
    to:
    Code:
    void displayBalance(double balance);
    double withdrawFunds(double balance);
    double depositFunds(double balance);
    That is, displayBalance will display the balance passed to it. withdrawFunds and depositFunds will work with the balanced passed to them and return the resulting balance.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Functions/Switch Cases? (in my code)
    By smogsy in forum C Programming
    Replies: 1
    Last Post: 03-02-2011, 03:04 AM
  2. cases
    By ZakkWylde969 in forum C++ Programming
    Replies: 2
    Last Post: 07-14-2003, 08:45 PM
  3. test cases
    By krithi in forum C Programming
    Replies: 10
    Last Post: 12-12-2002, 07:13 AM
  4. cases or ifs?
    By Yoshi in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2002, 07:58 AM
  5. Please help me with cases!!!
    By halifax in forum C Programming
    Replies: 1
    Last Post: 10-14-2001, 01:29 PM

Tags for this Thread