I want to store all of these "customer" objects in an array (an array of pointers to the objects seems the wise choice to me). Then, once all are stored and the user selects exit, my console program writes them all out at once. Idea is that the ofstream should only be initialized once so that it does not open a close a file each time it writes an object, but instead writes every object that is a member of the class out at once. I've looked at this for 10+ hours, tried it with static/non static, tried it with and without pointers, tried stripping away details, tried using this pointer, tried calling customerChoices with this->customerChoices, tried lots of variations on the theme, but...obviously, didn't work right. If I called writeout via this->writeout, or perhaps even without this, and then did ofile << *this->firstname, I could get it to write out that customer's name. In an effort to create all customers in the same manner, I made createnew() static so I could call it from main and create the first customer with it. Then I loop and can create more customers. Doing this, I can still write out the most recent customer inputted with createnew, but I cannot loop through the array to access each customer's info separately, as I get "the instruction at "xxxxxxx" referenced memory at "yyyyy" - the memory could not be 'read'. Any lifesavers out there feel like helping somebody learn? Thanks much, email me at markdani3@hotmail.com if you want to see the other files that go with this header...
using namespace std;
class customer{
private:
string* firstname, *lastname, *pin;
account* myAccount;
static customer* cust_array[];
static int num_customers;
int custid;
public:
class writeout_ofileopen //two exception classes
{};
class writeout_ofilewrite
{};
//////////////////////////
//////////
//////////
//////////
//////////
/////////c
onstructor
s
customer(string c_fnameIn, string c_lnameIn, string c_pinIn, double c_balanceIn) : firstname(c_fnameIn), lastname(c_lnameIn), pin(c_pinIn)
{
cout << "\n\ncreated customer with first name: " << firstname << "\nlast name: " << lastname << "\npin: " << pin;
cout << "\nnum_customers: " << num_customers; num_customers++; cout << "\nnum_customers after inc: " << num_customers;
myAccount = new account(c_balanceIn);
custid = num_customers; cout << "\ncustomer id: " << custid;
customerChoices();
}
/*customer(customer& c) : firstname(c.firstname), lastname(c.lastname), pin(c.pin), myAccount(c.myAccount)
{
cout << "\n\ncreation by copy constructor, unexpected?...\n";
}
customer()
{cout << "\n\n\ndefault constructor...is this wrong?";} */
//////////////////////////
//////////
//////////
//////////
//////////
//////////
//////////
//////end constructors
//choose to w/draw, deposit, checkbalance, or exit..exiting writes out to customer_account.txt
double getdouble(); //verifies that input is a double
void customerChoices() {
while(true){
cout << "\nwhatcha gonna do?"
<< "\n'a' to add a customer"
<< "\n'd' to deposit"
<< "\n'w' to withdraw"
<< "\n'c' to check balance"
<< "\n'x' to exit"
<< "\ntype your choice and press enter:";
char choice;
cin >> choice;
switch(choice){
case 'a' :
{
createnew(); break;
}
case 'd' :
{
int idnum; cout << "enter the id num to depo for:";
cin >> idnum;
cout << "\nhow much you want to depo?";
double amount = getdouble();
for(int j = 0; j<num_customers; j++)
{
if(cust_array[j]->custid == idnum)
{ cust_array[j]->myAccount->
deposit(am
ount); break; }
}
break;
}
case 'w' :
{
cout << "\nhow much you want to withdraw?";
double subtract = getdouble();
myAccount->withdraw(subtra
ct); break;
}
case 'c' : { //brackets mandatory for initialization of tempbal...
double tempbal = myAccount->getbalance();
cout << "\nbalance is: " << tempbal; break;
}
case 'x' : {
writeout();
cout << "\n\n\nfind a file called customer_account.txt to view your information.\n";
exit(1);
}
default : cout << "bad selection, try again.\n"; break;
} //end switch
}
} //end while
//end customerChoices()
//////////////////////////
//////////
//////////
//////////
//////////
//////////
//////////
//
static void createnew()
{
string firstname1, lastname1, pin1;
cout << "enter your first name:";
getline(cin, firstname1);
cout << "enter your last name:";
getline(cin, lastname1);
cout << "enter your pin:";
getline(cin, pin1);
double getdoubleTemp();
cout << "enter your balance:";
double balance = getdoubleTemp();
customer::cust_array[num_c
ustomers] = new customer(firstname1, lastname1, pin1, balance); //supposed to insert pointer to new customer into array at current index level.
}
//////////////////////////
//////////
//////////
//////////
//////////
//////////
//////////
////
void writeout()
{
ofstream ofile("customer_account.tx
t", ios::trunc);
for(int j=0; j<customer::num_customers;
j++) //this is where bombs, trying to access memory that hasn't been allocated, i think?!
ofile << cust_array[j]->lastname << "\n";
}
};
//////////////////////////
//////////
//////////
//////////
//////////
//////////
///end class