title: C++ Address Book Array and Textfile
body: <p>Sorry for the lack of previous explanation to my school's assignment. Here's what I'm working with and what I have / think I have to do.</p>
  
  <p>I have the basic structure for populating the address book inside an array, however, the logic behind populating a text file is a bit beyond my knowledge. I've researched a few examples, however, the implementation is a bit tricky due to my novice programming ability. </p>
  
  <p>I've gone through some code that looks relevant in regard to my requirements:</p>
  
  <pre><code>ifstream input("addressbook.txt"); 
  ofstream out("addressbook.txt");
  </code></pre>
  
  <p>For ifstream, I believe implementing this into the voidAddBook::AddEntry() would work, though I've tried it and the code failed to compile, for multiple reasons.</p>
  
  <p>For ostream, I'm lost and unsure as to how I can implement this correctly. I understand basic file input and output into a text file, however, this method is a bit more advanced and hence why I'm resorting to stackoverflow's guidance. </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string.h&gt; //Required to use string compare                         
  
  using namespace std;
  
  class AddBook{
  
  public:
      AddBook()
        {
           count=0;
        }
  
     void AddEntry();
     void DispAll();
     void DispEntry(int i); // Displays one entry                              
     void SearchLast();
  
     int Menu();
  
     struct EntryStruct
     {
        char FirstName[15];
        char LastName[15];
        char Birthday[15];
        char PhoneNum[15];
        char Email[15];
     };
  
     EntryStruct entries[100];
     int count;
  };
  
  
  void AddBook::AddEntry()
  {
  
     cout &lt;&lt; "Enter First Name: ";
     cin &gt;&gt; entries[count].FirstName;
  
     cout &lt;&lt; "Enter Last Name: ";
     cin &gt;&gt; entries[count].LastName;
  
     cout &lt;&lt; "Enter Date of Birth: ";
     cin &gt;&gt; entries[count].Birthday;
  
      cout &lt;&lt; "Enter Phone Number: ";
     cin &gt;&gt; entries[count].PhoneNum;
  
     cout &lt;&lt; "Enter Email: ";
     cin &gt;&gt; entries[count].Email;
  
     ++count;                                                
  }
  
  
  void AddBook::DispEntry(int i)
  {                
     cout &lt;&lt; "First name : " &lt;&lt; entries[i].FirstName &lt;&lt; endl;
     cout &lt;&lt; "Last name : " &lt;&lt; entries[i].LastName &lt;&lt; endl;
     cout &lt;&lt; "Date of birth : " &lt;&lt; entries[i].Birthday &lt;&lt; endl;
     cout &lt;&lt; "Phone number : " &lt;&lt; entries[i].PhoneNum &lt;&lt; endl;
     cout &lt;&lt; "Email: " &lt;&lt; entries[i].Email &lt;&lt; endl;
  }
  
  void AddBook::DispAll()
  {
     cout &lt;&lt; "Number of entries : " &lt;&lt; count &lt;&lt; endl;
  
     for(int i = 0;i &lt; count;++i)
        DispEntry(i);
  }
  
  void AddBook::SearchLast()
  {
     char lastname[32];
     cout &lt;&lt; "Enter last name : ";
     cin &gt;&gt; lastname;
  
     for(int i = 0;i &lt; count;++i)
     {
        if(strcmp(lastname, entries[i].LastName) == 0)
        {
           cout &lt;&lt; "Found ";
           DispEntry(i);
           cout &lt;&lt; endl;
        }
     }
  }
  
  AddBook AddressBook;
  
  int Menu()
  {
     int num;
     bool BoolQuit = false;
  
     while(BoolQuit == false)
     {                     
  
        cout &lt;&lt; "Address Book Menu" &lt;&lt; endl;
        cout &lt;&lt; "(1) Add A New Contact" &lt;&lt; endl;
        cout &lt;&lt; "(2) Search By Last Name" &lt;&lt; endl;
        cout &lt;&lt; "(3) Show Complete List" &lt;&lt; endl;
        cout &lt;&lt; "(4) Exit And Save" &lt;&lt; endl;
        cout &lt;&lt; endl;
  
          cout &lt;&lt; "Please enter your selection (1-4) and press enter: ";      
        cin &gt;&gt; num;
        cout &lt;&lt; endl;
  
        if (num == 1)
           AddressBook.AddEntry();
        else if (num == 2)
           AddressBook.SearchLast();
        else if (num == 3)
           AddressBook.DispAll();
        else if (num == 4)
           BoolQuit = true;
        else
           cout &lt;&lt; "Please enter a number (1-4) and press enter: " &lt;&lt; endl;
  
        cout &lt;&lt; endl;                                                            
     }
  
     return 0;
  }
  
  int main (){
     Menu();
  
     return 0;
  }
  </code></pre>
  
  <p>As it currently stands, I'm still stuck. Here's where I believe I should start:</p>
  
  <pre><code>cout &lt;&lt; "Please enter your selection (1-4) and press enter: ";      
        cin &gt;&gt; num;
        cout &lt;&lt; endl;
  
        if (num == 1)
           AddressBook.AddEntry();
        else if (num == 2)
           AddressBook.SearchLast();
        else if (num == 3)
           AddressBook.DispAll();
        else if (num == 4)
           BoolQuit = true;
        //save first name
        //save last name
        //save dob
        //save phone number
        //save email
        //exit
        else
           cout &lt;&lt; "Please enter a number (1-4) and press enter: " &lt;&lt; endl;
  
        cout &lt;&lt; endl;                                                            
     }
  </code></pre>
  
  <p>Somehow, during menu option 4 the array should dump the data into a .txt file and arrange it in a way that it can be easily imported upon reloading the program. I'm a little confused as to how I can store the array data from each character array into a .txt file. </p>