This program is very tricky...

First off it's an ATM I coded and every time you as for money back or put money in or ANYTHING it the name and account number gets re-entered.

Here's the code:

#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;
float opening_bal = 2000.99;   //For points practice (.99)
struct bank
{      
     public:
            void opening();
            void menu();
            char first_name[201];
            char last_name[21];
            int accnt_num;
};
int main(int argc, char *argv[])
{
    bank Money;
    cout<<"Welcome to First National Bank ATM!"<<endl;
    cout<<"ATM software developed by: Shaun Dougherty"<<endl;
    Money.opening();
    Money.menu();
    system("PAUSE");
    return 0;
}
void bank::opening()
{
     
     char answer[8];
     cout<<"Please Enter your first name\n"; cin>>first_name;
     cout<<"Enter your last name\n"; 
     cin>>last_name;
     
     cout<<"Hello " << strcat(first_name, last_name) <<"!"<<endl;
     cout<<"Please enter your account number\n";
     cin>>accnt_num;
}

void bank::menu()
{
     float deposit_amnt, withdrawal_amnt;
     int choice;
     for(;;)
     {
        cout<<"Welcome, "<<strcat(last_name, first_name)<<endl;
        cout<<"Account #:" << accnt_num<<endl;
        cout<<"Current Balance: $"<<fixed<<setprecision(2)<<opening_bal<<endl;;
        cout<<"-----------------------------------------"<<endl;
        cout<<" **********************************"<<endl;
        cout<<" *               MENU             * "<<endl;
        cout<<" *  1. Deposit     2. Withdrawal  * "<<endl;
        cout<<" *  3. Fast Cash ($60) 4. Check Balance *"<<endl;
        cout<<" * 0 = exit                             *"<<endl;
        cout<<" ****************************************"<<endl;
     
         cin>>choice;
         if ( choice == 0 )
         {
              break;
         }
         else
         {
              
              {
                     switch (choice)
                     {
             case 1:
                     cout<<"Deposit amount?\n";
                     cin>>deposit_amnt;
                     opening_bal += deposit_amnt;
                     cout<<"Your balance is now $" <<setprecision(2)<<fixed<<opening_bal<<endl;
                 break;
            case 2:
                     cout<<"Withdrawal amount?\n";
                     cin>> withdrawal_amnt;
                     if (withdrawal_amnt > opening_bal )
                     {
                        
                         cout<<"There is not enough to make the transaction!!!"<< '\a'<<endl;
                                       break;
                     }
                     else
                     {
                       opening_bal -= withdrawal_amnt;
                     }
                     cout<<"Your balance is now $" << setprecision(2)<<fixed<<opening_bal<<endl;
                 break;
            case 3:
                 opening_bal -= (float) 60.00;
                 cout<<"$60 cash taken out... Balance is now $"<<setprecision(2)<<fixed<<opening_bal<<endl;
                 break;
            case 4:
                 cout<<"Your current balance is $"<<setprecision(2)<<fixed<<opening_bal<<endl;
                 break;
                 case 0:
                      break;
            default:
                    cout<<'\a'<< "Invalid Number\n";
                    break;
                    }
              }   
         }
      system("cls");
     }
}

I'm thinking hopefully somebody can run this program see its errors and be able to help me. thanks :-)

Recommended Answers

All 3 Replies

I have run your program several times and don't see a problem other than you need to put a space between the first and last names when you print them on the screen.

If you don't want to have to type your name every time you run the program then you need to save that info to disk. Then the first thing the program should do is prompt for the account number. Check the data file for the account number and, if found, read the first name, last name, and current balance. Otherwise if the account number is not on file then prompt for the first and last number. Before exiting the program it needs to write that information back to the file.

Also the name is getting printed wierdly

Welcome to First National Bank ATM!
ATM software developed by: Shaun Dougherty
Please Enter your first name
mel
Enter your last name
sto
Hello melsto!
Please enter your account number
100
Welcome, doejohndoe<<<< HERE 
Account #:100
Current Balance: $2000.99
-----------------------------------------
 **********************************
 *               MENU             *
 *  1. Deposit     2. Withdrawal  *
 *  3. Fast Cash ($60) 4. Check Balance *
 * 0 = exit                             *
 ****************************************

>> cout<<"Hello " << strcat(first_name, last_name) <<"!"<<endl;

Don't do that! every time that line is executed strcat() will append last_name to the end of first_name, eventually causing the program to scribble all over memory.

A solution is to declare another variable char name[255]; and after entering the last ane first names, copy them both to that variable -- only ONCE. sprintf(name, "%s %s", first_name, last_name); Do you know about std::string c++ class yet? Are you allowed to use it in your program? If yes, then replace all those character arrays with std::strings

std::string last_name;
std::string first_name;
std::string name;
...
...
name = first_name + " " + last_name;

Ok thanks!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.