title: reading from .txt file and storing them to vector with class objects as values in c++
body: <pre><code>    vector&lt;Person*&gt; people;
  ifstream in_stream;
  string line;
  string name;
  int age;
  
  in_stream.open("people.txt");
  getline(in_stream, line);
  stringstream sline(line);
  sline &gt;&gt; name &gt;&gt; age;
  Person a(name, age);
  people.push_back(&amp;a);
  
  
  for (auto x : people)
      cout &lt;&lt; *x &lt;&lt; endl;
  </code></pre>
  
  <p>So i have this piece of code right here and im trying to read from a .txt file which is formed like that</p>
  
  <pre><code>Nick 23
  Peter 27
  Tom 42
  Sofia 28
  Daniel 39
  Jonas 28
  </code></pre>
  
  <p>And save these people(name and age) in a vector which contains pointers to a class called Person.
  The code above works fine but when i put the getline in a while loop so i can get all lines of the txt is bugging out and the vector will be like that</p>
  
  <pre><code>28
  28
  28
  28
  28
  28
  </code></pre>
  
  <p>No names just the age of the last person.
  Any ideas what im doing wrong here?
  Person.h</p>
  
  <pre><code>#pragma once
  #include &lt;ostream&gt;
  #include &lt;istream&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  class Person {
  protected:
      string name;
      size_t age;
      virtual void print(ostream&amp; out)  const;
  public:
      Person(const Person&amp; p);
      Person(string n, size_t a);
      Person();
      friend bool operator==(const Person&amp; lhs, const Person&amp; rhs);
      friend ostream&amp; operator&lt;&lt;(ostream&amp; out, const Person&amp; rhs);
      friend istream&amp; operator&gt;&gt;(istream&amp; in, Person&amp; rhs);
  
  };
  </code></pre>
  
  <p>Person.cpp</p>
  
  <pre><code>#include "Person.h"
  
  Person::Person(const Person&amp; p) : name{p.name}, age {p.age}
  {
  }
  
  Person::Person(string n, size_t a) : name{ n }, age{ a }
  {
  }
  
  Person::Person() : name{"default"}, age {0}
  {
  }
  
  bool operator==(const Person&amp; lhs, const Person&amp; rhs)
  {
      if (lhs.name == rhs.name &amp;&amp; lhs.age == rhs.age) {
          return true;
      }
      else {
          return false;
      }
  
  }
  
  void Person::print(ostream&amp; out) const
  {
      out &lt;&lt; name &lt;&lt; " " &lt;&lt; age;
  }
  
  ostream&amp; operator&lt;&lt;(ostream&amp; out, const Person&amp; rhs)
  {
      rhs.print(out);
      return out;
  }
  
  istream&amp; operator&gt;&gt;(istream&amp; in, Person&amp; rhs)
  {
      string x;
      int y;
      in &gt;&gt; x &gt;&gt; y;
      rhs.name = x;
      rhs.age = y;
      return in;
  }
  </code></pre>
  
  <p>Any help is highly appreciated.</p>