Hello -

I love this site and often visit to search for answers to problems I have with homework. Well I finally ran into a brick wall with this one. Hopefully I can get some help, or at least pointed in the right direction. I usually can figure things out on my own, but am now late in turning this assignment in as I can't get it to work properly. I was hoping somebody could tell me what I am doing wrong.

Here are my instructions:

Build an application in C++ using templates that serves as a virtual ATM.

Make the templated class take different data types as the balance (float, int, etc.).

Be able to take a deposit and a withdrawal that is type non-specific (templated generic type).

Maintain a data file containing customer account numbers, balances, and PINs

When the program begins, give the user 2 options:
Open a new account
Access existing account

When the user chooses to open a new account, ask the user for Name (first, last), PIN, and initial deposit, then assign a unique account number. Append the account number, balance, and PIN out to the account data file.

When the user chooses to access an existing account, have the user enter their account number. Read their PIN and initial balance from the data file. Request the PIN from the user and verify it.

Then present a menu and implement the following options:
Make a Withdrawal
Make a Deposit
Check Balance
Exit

When the user exits, update the balance information in the data file


And my code: (It is rough and has A lot that I have added just to see what is happening, please forgive me)

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

using namespace std;
const int ARRAY_MAX = 40;
ifstream infile;
ofstream outfile;
    
template <class Type>
class customer
{
    private:
        Type balance;
        int acct, pin;
        char firstName[ARRAY_MAX], lastName[ARRAY_MAX];

    public:
        void display();
        void setData();   
        void getData();
        void writeData();
        Type transaction(Type amt)
        {
           balance += amt;
           return balance;
        }
          
        void setAcct(int acctNum)
        {
            acct = acctNum;
            return;
        }
        
        int getAcct()
        {
            return acct;
        }
        
        int getPIN()
        {
            return pin;
        }
        
        Type getBalance()
        {
            return balance;
        }
        
};

template <class Type>
void customer<Type>::setData()
{
    
    cout << "Please enter information for this account.\n" 
   	     << "First Name : ";
	cin  >> firstName;
	cout << "\nLast Name: ";
	cin  >> lastName;
	cout << "\nPIN: ";
	cin  >> pin;
	cout << "\nInitial balance: ";
	cin  >> balance;
    cout << endl;
    return;
}


template <class Type>
void customer<Type>::writeData()
{
    outfile << showpoint << fixed << setprecision(2); 
	outfile << firstName << endl << lastName << endl << acct << endl 
            << pin << endl << balance << endl;
	return;      
}

template <class Type>
void customer<Type>::getData()
{
	infile >> firstName >> lastName >> acct >> pin >> balance;
//	cout << "ACCT#: " << acct << endl;
	return;      
}

template <class Type>
void customer<Type>::display()                                                  //displays Employee information
{
    double dblBal = static_cast<double>(balance);
    cout << firstName << " " << lastName << " -" << endl;                               //to display balance as a double
	cout << setw(15) << left <<"    Account #:" << setw(10) 
         << right << acct << endl			 
         << setw(15) << left <<"    PIN:" << setw(10) << right 
         << pin << endl		
         << setw(15) << showpoint << fixed << setprecision(2) << left 
         <<"    Balance:" << setw(10) << right << dblBal << endl;		 
    return;
}



int main()
{
    customer<double> custAcct;                                                               //counts customer accounts
    char check, selection = '0';
    int acctNum, tempPIN, pos;
    double amount;
    bool found;

    do
    {
        cout << "What would you like to do.\n"
             << "    1 - Open a new account.\n"
             << "    2 - Access an existing account.\n"
             << "Please enter the number of your selection: ";
        cin  >> selection;
        cout << endl;
        switch (selection)
        {
            case '1':
                infile.open("CUSTACCTS.txt");
                do
                {
                    found = false;
                    srand((unsigned)time(0));
                    acctNum = (rand() % 8999)+ 1000;
                    cout << acctNum << endl;
                    while(!found && infile)
                    {
                        custAcct.getData();
                        if (acctNum == custAcct.getAcct())
                            found = true;
                    }
                } while (found);
                infile.close();
                custAcct.setAcct(acctNum);
                custAcct.setData();
                custAcct.writeData();
                cout << "Account number is " << custAcct.getAcct() << ".\n"
                     << "Please save this for your records." << endl;
                selection = '0';
                break;
            case '2':
                found = false;
                infile.open("CUSTACCTS.txt");
             //   infile.seekg(0);
                cout << infile.tellg();
                cout << "Please enter the account number: ";
                cin  >> acctNum;
                cout << endl;
                while(!found && infile)
                {
                    custAcct.getData();
                    if (acctNum == custAcct.getAcct())
                        found = true;
                }
                if (!found)
                {
                    cout << "That is not a valid account number" << endl << endl;
                    selection = '0';
                }
                else
                {
                    pos = infile.tellg();
                    infile.close();
                    cout << "Please enter PIN for that account: ";
                    cin  >> tempPIN;
                    cout << endl;
                    if (tempPIN == custAcct.getPIN())
                    {
                        cout << "Please make selection.\n"
                             << "    1 - Make a withdrawal\n"
                             << "    2 - Make a deposit\n"
                             << "    3 - Check balance\n"
                             << "    4 - Exit\n"
                             << "Enter selection: ";
                        cin  >> selection;
                        cout << endl;
                        switch (selection)
                        {
                            case '1':
                                outfile.open("CUSTACCTS.txt", ios::app);
                                cout << "How much money would you"
                                     << " like to withdrawal: ";       
                                cin  >> amount;
                                amount = 0 - amount;
                              //   outfile.seekp(pos - sizeof(custAcct));
                                custAcct.writeData();
                                selection = '0';
                                outfile.close();
                                break;
                            case '2':
                                outfile.open("CUSTACCTS.txt", ios::app);
                                
                                cout << "How much money would you"
                                     << " like to deposit: ";       
                                cin  >> amount; 
                               // outfile.seekp(pos - sizeof(double));
                                custAcct.writeData();
                                selection = '0';
                                outfile.close();
                                break;
                            case '3':
                                 cout << showpoint << fixed << setprecision(2);
                                 cout << "The balance is " << custAcct.getBalance()
                                      << endl << endl;
                                 selection = '0';
                                 break;
                            case '4':
                                cout << "You will now exit" << endl << endl;
                                break;
                            default:
                                cout << "That was incorrect input" << endl;
                                selection = '0';        // reset to zero to restart loop upon bad user input
                                break;
                        }    //end switch                 
                    } //end if
                    else 
                        cout << "That was an incorrect PIN" << endl;
                } // end else
                    break;
            default:
                selection = '0';
                cout << "That is invalid input." << endl << endl;
                break;
        }//end switch
    } while (selection == '0');   //end do
    
    
    cout << endl << endl;
    cout << "Press [enter] to exit" << endl;
    cin.ignore();
    cin.get(); 
    return 0;   
}

Any advice is appreciated.

Recommended Answers

All 3 Replies

1. That program is not using a "random access file". Those kinds of files are binary files with fixed-length records. What you are doing is sequential access file -- reading from beginning to end in an attempt to find a given record, and the records may or may not be fixed-length.

2. Your program would be easier to read and comprehend if you used functions to replace all that code in the switch statement.

3. What is the question? You posted a lot of code without telling us what your problem is.

Sorry, when I read and find the correct record and attempt to update the balance, I overwrite the entire file with just the one entry I want, not replace the one entry. Also if I add a record, I can not access an existing one afterward. I can only access an existing record if I do that first. The random access I figured would be that when I find the desired record I want to update one part of it. Sorry.

To my knowledge:

If you want to add an entirely new set of customer data to end of current data stored in file without overwriting current data, then you can open the file in app mode to append the data to the end of the current file.

If you want to insert data for a new customer someplace other than the end of the current file then you will need to rewrite the entire file, or at least overwrite all the data after the insertion point.

If you want to overwrite specific information currently in file and the information to be overwritten has exactly the same memory footprint that the memory to replace it with has, then you (probably) can use the tell/seek methods of streams to do that. If the new information doesn't have the exact same memory footprint as the old information, then you have probably corrupted the information in the file.

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.