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.