title: Is there a way to combine two variables?
body: <p>I have been trying to get my code to dynamically allocate class objects to file to later read but having trouble with getting user input to save into each different object.</p>
  
  <p>I'm trying to have the user input their names, ages and phone numbers and have it save to file where it can be read later hopefully using the same method to run through the file.</p>
  
  <p>I tried using arrays but that can't save all three fields of the object. Is there a dynamic variable that can be used?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;cassert&gt;
  using namespace std;
  
  string mName, mID, mPhoneNumber;
  int id = 0;
  class Student
  {
  public:
     string mName;
     string mId;
     string mPhoneNumber;
  
     Student(string id = "", string name = "", string phone = "") : mId(id), mName(name), mPhoneNumber(phone)
     {}
  
     bool operator==(const Student&amp; obj)
     {
        return (mId == obj.mId) &amp;&amp; (mName == obj.mName) &amp;&amp; (mPhoneNumber == obj.mPhoneNumber);
     }
  
     /*
      * Write the member variables to stream objects
      */
     friend ostream&amp; operator &lt;&lt; (ostream&amp; out, const Student&amp; obj)
     {
        out &lt;&lt; obj.mId &lt;&lt; "\n" &lt;&lt; obj.mName &lt;&lt; "\n" &lt;&lt; obj.mPhoneNumber &lt;&lt; endl;
        return out;
     }
     /*
      * Read data from stream object and fill it in member variables
      */
     friend istream&amp; operator &gt;&gt; (istream&amp; in, Student&amp; obj)
     {
        in &gt;&gt; obj.mId;
        in &gt;&gt; obj.mName;
        in &gt;&gt; obj.mPhoneNumber;
        return in;
     }
  };
  
  int main()
  {
     cin &gt;&gt; id;
     Student stud1("1", "Jack", "4445554455");
     Student stud2("4", "Riti", "4445511111");
     Student stud3("6", "Aadi", "4040404011");
  
     // open the File
     ofstream out("students.txt");
     // Write objects to file (targets to cout)
     out &lt;&lt; stud1;
     out &lt;&lt; stud2;
     out &lt;&lt; stud3;
  
     out.close();
     // Open the File
     ifstream in("students.txt");
     Student student1;
     Student student2;
     Student student3;
     // Read objects from file and fill in data
     in &gt;&gt; student1;
     in &gt;&gt; student2;
     in &gt;&gt; student3;
     in.close();
     // Compare the Objects
     assert(stud1 == student1);
     assert(stud2 == student2);
     assert(stud3 == student3);
  
     cout &lt;&lt; stud1 &lt;&lt; endl;
     cout &lt;&lt; stud2 &lt;&lt; endl;
     cout &lt;&lt; stud3 &lt;&lt; endl;
     return 0;
  }
  </code></pre>