Thread: Add 4 accounts to this program

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    10

    Add 4 accounts to this program

    Here is the first account. I want to add 4 more accounts.

    Code:
    // preprocessor directives
    #include <cstddef>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include<stdlib.h>
    #include<stdio.h>
    //using namespace standard
    using namespace std;
    //void functions
    void Begin1(); // function that begins account 1
    void depositc(); // function that begins account 1's deposit to checking section
    void withdrawc(); // function that begins account 1's withdraw from checking section
    void balancec();  // function that begins account 1's balance of checking account section
    void deposits();  // function that begins account 1's deposits to savings section
    void withdraws();  // function that begins account 1's withdraw from savings section
    void balances();   // function that begins account 1's balance of savings account section
    void transferc(); // function that begins account 1's transfer to checking section
    void transfers(); // function that begins account 1's transfer to savings section
    
    
        int selection; // variable to store user's response to what transaction they want to complete.
        int select; // variable to store user's response to which section they want to withdraw from (checking or savings)
        int select2; // variable to store user's response to which section they want to deposit to (checking or savings)
        int select3; // variable to store user's response to which section they want to view their balance of (checking or savings)
        int select4; // variable to store user's response to which section they want to transfer to (checking or savings)
    
    
    int main()  // main function
    {
        // declare variable constants, strings, and arrays to store the user's account number and pin number
        const size_t size = 5;
        string Number[ size ] = { "1234" }; //stores user's account number
        string PinNumber[ size ] = { "2000" }; // stores user's pin number
        bool access = false;    
        string Num; //stores user's input for account number
        string Pin; // stores user's input for pin number
                     
    do
    {   
    	
    	
    	system("cls"); // clears the screen
        cout<<endl; // carriage return
        cout<<endl;
        cout<<"\\\\\\\\\\\\Welcome to TWC ATM /////////////"<<endl; // outputs "Welcome to TWC ATM" to the screen
        cout<<endl;
        cout<<"Enter your Account Number: "; // outputs a statement asking the user to enter their account number
        cin>>Num; // stores user's input for account number
    	cout<<"\n"<<endl;
        cout<<"\n"<<endl;
        cout<<"Enter your PIN Number: "; // outputs a statement asking the user to enter their pin number
        cin>>Pin; // stores users input for pin number
    	system ("cls"); // clears the screen
        cout<<"\n"<<endl;
        
        for ( size_t i = 0; i < size; i++ )
          {
             if ( ( Num == Number[ i ] ) && ( Pin == PinNumber[ i ] ) )
             {            
                access = true;               
             }
             if (Pin == "2000" && Num == "1234")     // if the account number and the pin number match in the previously declared arrays then access is granted.
                {Begin1(); access = false; break;}
          }
          
          }
        while ( access == false );   
    
    }
    //////////////////////////////////////////First Account//////////////////////////////////////      
    void Begin1(){ // begins account 1
    
    cout<<"Welcome Kelly Martin."<<endl; // outputs a message welcoming the first user.
    cout<<endl;
    cout<<"What would you like to do today?"<<endl; // outputs a message asking the user what transaction they want to do.
    cout<<"(1)Withdraw"<<endl;
    cout<<"(2)Deposit"<<endl;
    cout<<"(3)View Balance"<<endl;
    cout<<"(4)Transfer funds"<<endl;
    cout<<"(5)Exit this Account"<<endl<<endl;
    cout<<"Please choose an option by pressing (1), (2),(3),(4), or (5): "; // asks the user to choose an option 1-5.
    cin>>selection; // stores the users input to which option they chose.
    
    system ("cls"); // clears the screen
    cout<<"\n"<<endl;
    
    switch (selection)// switches the user's response with the corresponding case.
    {
          
    case 1:  // begins case 1
         
    
    cout<<"(3)Withdraw Checking"<<endl; // asks the user which account they want to withdraw from (checking or savings)
    cout<<"(4)Withdraw Savings"<<endl;
    cout<<endl;
    cout<<"Enter your selection: ";  // asks the user to enter their selection
    cin>>select; // stores user's input
    
    system ("cls");  // clears the screen
          switch (select)  // switches the user's input with the corresponding case.
         {
              case 3:
                withdrawc(); // if the user chooses case 3; the program calls the withdraw from checking function.
              break;
              case 4:
                withdraws(); // if the user chooses case 4; the program calls the withdraw from savings function.
              break;
              default:
               cin.clear(); // restores input stream;
               cout<<"You entered invalid number, Please Try again"<<endl;  // Tells the user they entered an invalid number.
               system("pause"); // pauses the screen
               cin.ignore();  // clears the buffer;
               system("cls"); // clears the screen;
               Begin1();
          }        
          
    break;
            
    case 2: // begins case 2
    cout<<"(3)Deposit Checking"<<endl; // outputs a statement asking the user which account they want to deposit to (checking or savings)
    cout<<"(4)Deposit Savings"<<endl;
    cout<<endl;
    cout<<"Enter your selection: ";  // asks the user to enter their selection
    cin>>select2; // stores the user's input
    
    system ("cls"); // clears the screen
          switch (select2) // switches the user's in put with the corresponding case.
         {
              case 3:
              depositc(); // if the user chooses case 3; the program calls the deposit to checking function
              break;
              case 4:
              deposits(); // if the user chooses case 4; the program calls the deposit to savings function
              break;
               default:
               cin.clear(); // restores input stream;
               cout<<"You entered invalid number, Please Try again"<<endl;  // tells the user they entered an invalid number
               system("pause"); // pause the screen
               cin.ignore();  // clears the buffer;
               system("cls");  // clears the screen
               Begin1();
          }    
    break;
     
    case 3:
    cout<<"(1)Balance Checking"<<endl;  // asks the user which account they want to see the balance of (checking or savings)
    cout<<"(2)Balance Savings"<<endl;
    cout<<endl;
    cout<<"Enter your selection: ";   // asks the user to enter their selection
    cin>>select3; // stores the users input
    
    system ("cls"); // clears the screen
          switch (select3) // switches the users input with the corresponding case.
         {
              case 1:
                balancec(); // if the user chooses case 1; the program calls the balance of checking function.
              break;
              case 2:
                balances(); // if the user chooses case 2; the program calls the balance of savings function.
              break;
               default:
               cin.clear(); // restores the input stream
               cout<<"You entered invalid number, Please Try again"<<endl; // tells the users they entered an invalid number
               system("pause");  // pauses the screen
               cin.ignore();   // clears the buffer
               system("cls");  // clears the screen
               Begin1();
          }
    break; 
    
    case 4:
    cout<<"(1)Transfer from Checking to Savings"<<endl; // asks the user which account they want to transfer to or from (checking to savings or savings to checking)
    cout<<"(2)Transfer from Savings to Checking"<<endl;
    cout<<endl;
    cout<<"Enter your selection: "; // asks the user to enter their selection
    cin>>select4; // stores the user's input
    
    system ("cls"); // clears the screen
          switch (select4)   // switches the users input with the corresponding case.
         {
              case 1:
                transferc(); // if the user chooses case 1; the program calls the transfer to checking function
              break;
              case 2:
                transfers(); // if the user chooses case 2; the program calls the transfer to savings function
              break;
              default:
               cin.clear(); // restores the input stream
               cout<<"You entered invalid number, Please Try again"<<endl; // tells the user they entered an invalid number
               system("pause"); // pauses the screen
               cin.ignore(); // clears the buffer
               system("cls");  // clears the screen
               Begin1();
          }
    break; 
    
    case 5:
    cout<<endl;
    cout<<endl;
    cin.clear(); // restores the input stream
    cout<<"\t\tThank you Kelly for using TWC ATM. Have a good day!"<<endl; // tells the user thank you and have a good day
    cout<<endl;
    cout<<endl;
    cout<<endl;
    cout<<endl;
    system ("pause");  // pauses the screen
    system ("cls");  // clears the screen
    cin.clear(); // restores the input stream
    cin.fail();  // stops the program
    
    break;
    
         default:
               cin.clear();  // restores the input stream
               cout<<"You entered invalid number, Please Try again"<<endl; // tells the user they entered an invalid number
               system("pause"); // pauses the screen
               cin.ignore(); // clears the buffer
               system("cls"); // clears the screen
               Begin1();
        
    }
    }
     
    ////////////////////////////////////////////////Checking///////////////////////////////////////        
    void withdrawc(){ // begins the withdraw from checking function
      
      
      float amountw;   // variable to store amount user withdraws from checking
      float bal;     // variable to update the checking account balance
         
    {
            
    cout<<"\n"<<endl;
    cout<<"How much would you like to withdraw? $";  // asks the user the amount they want to withdraw
    cin>>amountw;
    while(cin.fail()) // if the amount they enter is more than the balance of their checking account the program will not continue.
    
       {
         std::cout <<"Please enter a valid amount to withdraw: $"; // tells the user to enter a valid amount to withdraw
         std::cin.clear(); // restores the input stream
         std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // clears the buffer
         std::cin>>amountw; // stores the users input to the amount they want to withdraw
       }
    
    if(amountw < 0) // if the amount they input to withdraw is a negative number the program multiplies the number by -1 to correct their mistake.
    amountw = amountw * -1; 
    }
    {
    ifstream getinfo("a:\\check.txt");  // opens the checking account text file
    getinfo>>bal; // gets the balance of the checking account
    getinfo.close(); // closes the checking account text file
    
    bal = bal - amountw;  // updates the balance of the checking account file
    
    do
    {
        
    	if(bal < 0) // if the balance of their checking account is negative the program will not continue
    	{
    		cout<<"You can not withdraw more than your Checking balance of: $"<<(bal + amountw) <<endl; // tells the user they cannot withdraw more than the balance of their checking account
    		cout<<"\n"<<endl;
            cout<<"How much would you like to withdraw from Checking? $";
            cin>>amountw;  
            if(amountw < 0)
            amountw = amountw * -1;      
            ifstream getinfo("a:\\check.txt");
            getinfo>>bal;
            getinfo.close();
            bal = bal - amountw;
    	}
    }while(bal < 0);
    
    cout <<"\n"<<endl;
    cout<<"*********************************************************************"<<endl;
    cout<<"\n"<<endl;
    cout<<"\n"<<endl;
    
    ofstream putinfo("a:\\check.txt");
    putinfo<<bal << endl;
    putinfo.close();
    }
    cout<<"\n"<<endl;
    cout <<"Kelly" <<","<<" you withdrew $"<< amountw <<endl;
    cout<<"\n"<<endl;
    cout<<"Your new checking balance is: $"<<bal<<endl;
    cout<<"\n"<<endl;
    system ("pause");
    system ("cls");
    Begin1();
    }
    
    void depositc(){
      
      
      float amountd;
      float bal;   
         
    { 
     
    cout<<"\n"<<endl;
    cout<<"How much would you like to deposit? $";
    cin>>amountd;
          
    while(cin.fail())
    
       {
         std::cout <<"Please enter a valid amount to deposit: $";         
         std::cin.clear();
         std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
         std::cin>>amountd; 
       }
      
    
    if(amountd < 0)
    amountd = amountd * -1; 
    }
    {
    ifstream getinfo("a:\\check.txt");
    getinfo>>bal;
    getinfo.close();
    
    bal = bal + amountd;
    
    cout <<"\n"<<endl;
    cout<<"*********************************************************************"<<endl;
    cout<<"\n"<<endl;
    cout<<"\n"<<endl;
    
    ofstream putinfo("a:\\check.txt");
    putinfo<<bal << endl;
    putinfo.close();
    }
    cout<<"\n"<<endl;
    cout << "Kelly"<<","<<" you deposited $"<< amountd <<endl;
    cout<<"\n"<<endl;
    cout<<"Your new checking balance is: $"<<bal<<endl;
    cout<<"\n"<<endl;
    system ("pause");
    system ("cls");
    Begin1();
    }
    
    void balancec(){
      
      
      
      float bal;   
    
    
    {
    ifstream getinfo("a:\\check.txt");
    getinfo>>bal;
    getinfo.close();
    
    if (bal < 0)
    bal = 0;
    
    bal = bal;
    
    cout <<"\n"<<endl;
    cout<<"*********************************************************************"<<endl;
    cout<<"\n"<<endl;
    cout<<"\n"<<endl;
    }
    cout<<"\n"<<endl;
    cout << "Kelly"<<","<<" your checking balance is $"<< bal <<endl;
    cout<<"\n"<<endl;
    cout<<"\n"<<endl;
    system ("pause");
    system ("cls");
    Begin1();
    }
    
    
    void transferc(){//opens with check.txt file and subtracts from it
    
     
      
      float amountt;
      float bal;   
         
      
    {        
    cout<<"\n"<<endl;
    cout<<"How much would you like to transfer from Checking to Savings? $";
    cin>>amountt;
    while(cin.fail())
    
       {
         std::cout <<"Please enter a valid amount to transfer: $";         
         std::cin.clear();
         std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
         std::cin>>amountt; 
       }
    
    if(amountt < 0)
    amountt = amountt * -1;
    }  
    	
    
    {
    ifstream getinfo("a:\\check.txt");
    getinfo>>bal;
    getinfo.close();
    
    bal = bal - amountt;
    
    do
    {
        
    	if(bal < 0)
    	{
    		cout<<"You can not transfer more than your Checking balance of: $"<<(bal + amountt) <<endl;
    		cout<<"\n"<<endl;
            cout<<"How much would you like to transfer from Checking to Savings? $";
            cin>>amountt;  
            if(amountt < 0)
            amountt = amountt * -1;      
            ifstream getinfo("a:\\check.txt");
            getinfo>>bal;
            getinfo.close();
            bal = bal - amountt;
    	}
    }while(bal < 0);
           
    cout <<"\n"<<endl;
    cout<<"*********************************************************************"<<endl;
    
    ofstream putinfo("a:\\check.txt");
    putinfo<<bal << endl;
    putinfo.close();
    
    cout<<"\n"<<endl;
    cout <<"Kelly" <<","<<" you withdrew from Checking $"<< amountt <<endl;
    cout<<"\n"<<endl;
    cout<<"Your new checking balance is: $"<<bal<<endl;
    }
    {
    
    ifstream getinfo("a:\\sav.txt");
    getinfo>>bal;
    getinfo.close();
    
    bal = bal + amountt;
    
    ofstream putinfo("a:\\sav.txt");
    putinfo<<bal << endl;
    putinfo.close();
    
    cout<<"\n"<<endl;
    cout << "Kelly"<<","<<" you deposited to Savings $"<< amountt <<endl;
    cout<<"\n"<<endl;
    cout<<"Your new Savings balance is: $"<<bal<<endl;
    cout<<"\n"<<endl;
    cout<< "\n\tPress ENTER key to continue:"<<endl;
    system ("pause");
    system ("cls");
    Begin1();
    }
    }
    
    
    
    
    ///////////////////////////////Savings/////////////////////////////////////////////
    
    void withdraws(){
      
      
      float amountw;
      float bal;   
         
    {          
    cout<<"\n"<<endl;
    cout<<"How much would you like to withdraw? $";
    cin>>amountw;
    while(cin.fail())
    
       {
         std::cout <<"Please enter a valid amount to withdraw: $";         
         std::cin.clear();
         std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
         std::cin>>amountw; 
       }
    if(amountw < 0)
    amountw = amountw * -1; 
    }
    {
    ifstream getinfo("a:\\sav.txt");
    getinfo>>bal;
    getinfo.close();
    
    bal = bal - amountw;
    
    do
    {
        
    	if(bal < 0)
    	{
    		cout<<"You can not withdraw more than your Savings balance of: $"<<(bal + amountw) <<endl;
    		cout<<"\n"<<endl;
            cout<<"How much would you like to withdraw from Savings? $";
            cin>>amountw;  
            if(amountw < 0)
            amountw = amountw * -1;      
            ifstream getinfo("a:\\check.txt");
            getinfo>>bal;
            getinfo.close();
            bal = bal - amountw;
    	}
    }while(bal < 0);
    
    cout <<"\n"<<endl;
    cout<<"*********************************************************************"<<endl;
    cout<<"\n"<<endl;
    cout<<"\n"<<endl;
    
    ofstream putinfo("a:\\sav.txt");
    putinfo<<bal << endl;
    putinfo.close();
    }
    cout<<"\n"<<endl;
    cout << "Kelly"<<","<<" you withdrew $"<< amountw <<endl;
    cout<<"\n"<<endl;
    cout<<"Your new savings balance is: $"<<bal<<endl;
    cout<<"\n"<<endl;
    system ("pause");
    system ("cls");
    Begin1();
    }
    
    void deposits(){
      
      
      float amountd;
      float bal;   
         
    {          
    cout<<"\n"<<endl;
    cout<<"How much would you like to deposit? $";
    cin>>amountd;
    while(cin.fail())
    
       {
         std::cout <<"Please enter a valid amount to deposit: $";         
         std::cin.clear();
         std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
         std::cin>>amountd; 
       }
    if(amountd < 0)
    amountd = amountd * -1; 
    }
    {
    ifstream getinfo("a:\\sav.txt");
    getinfo>>bal;
    getinfo.close();
    
    bal = bal + amountd;
    
    cout <<"\n"<<endl;
    cout<<"*********************************************************************"<<endl;
    cout<<"\n"<<endl;
    cout<<"\n"<<endl;
    
    ofstream putinfo("a:\\sav.txt");
    putinfo<<bal << endl;
    putinfo.close();
    }
    cout<<"\n"<<endl;
    cout << "Kelly"<<","<<" you deposited $"<< amountd <<endl;
    cout<<"\n"<<endl;
    cout<<"Your new savings balance is: $"<<bal<<endl;
    cout<<"\n"<<endl;
    system ("pause");
    system ("cls");
    Begin1();
    }
    
    void balances(){
      
      
      
      float bal;   
    
    
    {
    ifstream getinfo("a:\\sav.txt");
    getinfo>>bal;
    getinfo.close();
    
    if (bal < 0)
    bal = 0;
    
    bal = bal;
    
    cout <<"\n"<<endl;
    cout<<"*********************************************************************"<<endl;
    cout<<"\n"<<endl;
    cout<<"\n"<<endl;
    }
    cout<<"\n"<<endl;
    cout << "Kelly"<<","<<" your savings balance is $"<< bal <<endl;
    cout<<"\n"<<endl;
    cout<<"\n"<<endl;
    system ("pause");
    system ("cls");
    Begin1();
    }
    
    void transfers(){//opens with check.txt file and subtracts from it
    
     
      
      float amountt;
      float bal;   
         
    
    {          
    cout<<"\n"<<endl;
    cout<<"How much would you like to transfer from Savings to Checking? $";
    cin>>amountt;
    while(cin.fail())
    
       {
         std::cout <<"Please enter a valid amount to transfer from Savings: $";         
         std::cin.clear();
         std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
         std::cin>>amountt; 
       }
    if(amountt < 0)
    amountt = amountt * -1; 
    }
    {
    ifstream getinfo("a:\\sav.txt");
    getinfo>>bal;
    getinfo.close();
    
    bal = bal - amountt;
    
    do
    {
        
    	if(bal < 0)
    	{
    		cout<<"You can not transfer more than your Savings balance of: $"<<(bal + amountt) <<endl;
    		cout<<"\n"<<endl;
            cout<<"How much would you like to transfer from Savings to Checking? $";
            cin>>amountt;        
            ifstream getinfo("a:\\sav.txt");
            getinfo>>bal;
            getinfo.close();
            bal = bal - amountt;
    	}
    }while(bal < 0);
    
    cout <<"\n"<<endl;
    cout<<"*********************************************************************"<<endl;
    
    ofstream putinfo("a:\\sav.txt");
    putinfo<<bal << endl;
    putinfo.close();
    }
    cout<<"\n"<<endl;
    cout <<"Kelly" <<","<<" you withdrew from Savings $"<< amountt <<endl;
    cout<<"\n"<<endl;
    cout<<"Your new Savings balance is: $"<<bal<<endl;
    
    
    ifstream getinfo("a:\\check.txt");
    getinfo>>bal;
    getinfo.close();
    
    bal = bal + amountt;
    
    ofstream putinfo("a:\\check.txt");
    putinfo<<bal << endl;
    putinfo.close();
    
    cout<<"\n"<<endl;
    cout << "Kelly"<<","<<" you deposited to Checking $"<< amountt <<endl;
    cout<<"\n"<<endl;
    cout<<"Your new Checking balance is: $"<<bal<<endl;
    cout<<"\n"<<endl;
    cout<< "\n\tPress ENTER key to continue:"<<endl;
    system ("pause");
    system ("cls");
    Begin1();
    }

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Just do it. (Nike)

    Edit: Just in case reading between the lines isn't your strength: what do you expect us to do ?

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Desolation View Post
    Edit: Just in case reading between the lines isn't your strength: what do you expect us to do ?
    Apparently, he wants functions like... DepositIntoAccount1(), DepositIntoAccount2(), DepositIntoAccount3(), etc. Absurd.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Step 1 - learn how to format code. Nobody is going to wade through that mess.

    Step 2 - learn about parameters.
    So you can replace
    void Begin1(); // function that begins account 1
    With
    void Begin( account_t &account ); // function that begins an account

    Then main simply has
    account_t accounts[4]; // yay!, that was easy

    Followed by things like
    Begin( accounts[0] ); // do something
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    All of the code is redundant and not really well-thought. From what I think you want to do (oops, you never even told us), you have at the very least twice the number of lines you'd need to accomplish this task. I don't think I even have to comment on the indentation, the variable names aren't meaningful and you are using system() which is evil, no matter what people say; if someone replaced cmd.exe with a malicious application (i.e. virus, trojan..) it would launch it thanks to your system() call. I could go on and on but please, ask us a question. Do not ask us to code for you. If there is a topic you don't understand we will be happy to help you understand it or if you have tried fixing a problem and haven't found a solution, we can help you as well but do not ask us to code for you.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Hmm. Perhaps creating a class called CAccount that encapsulates account data and functions would be best eh? So if you want 4 accounts you would create 4 instances of the CAccount class. But then you would probably want an account manager to keep a list of accounts. All of that is impossible to explain in one post.

    And I'm not going to wade through that mess of code either.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. add your program to start menu ?
    By Anddos in forum Windows Programming
    Replies: 6
    Last Post: 04-17-2008, 04:15 AM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. Replies: 3
    Last Post: 01-14-2003, 10:34 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM