[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.2

store objects of a single class in an array and then write them all out to text file in plain text (not binary)

Asked by vizionary in C++ Programming Language

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
      {};
///////////////////////////////////////////////////////////////////////////constructors

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(amount);  break;      }
                                                            }                  
                                                            break;
                                                }

                              case 'w' :      
                                                {            
                                                cout << "\nhow much you want to withdraw?";
                                                double subtract = getdouble();
                                                myAccount->withdraw(subtract);      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_customers] = 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.txt", 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
[+][-]11/18/03 02:52 AM, ID: 9770099Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: C++ Programming Language
Sign Up Now!
Solution Provided By: rstaveley
Participating Experts: 2
Solution Grade: A
 
[+][-]11/15/03 08:23 PM, ID: 9757580Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/15/03 10:33 PM, ID: 9757760Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/16/03 07:04 AM, ID: 9758623Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/16/03 12:44 PM, ID: 9759858Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/16/03 05:43 PM, ID: 9760822Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/16/03 10:37 PM, ID: 9761781Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/17/03 10:02 AM, ID: 9765510Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/17/03 01:11 PM, ID: 9766904Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/17/03 04:06 PM, ID: 9767970Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/18/03 07:55 AM, ID: 9771676Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]11/18/03 08:23 AM, ID: 9771873Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/18/03 09:43 AM, ID: 9772438Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/18/03 12:46 PM, ID: 9773839Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92