title: C++ Read Value from a pointer to struct including the target struct in an array
body: <p>I am new to c++ and trying to make an atm. It consits of one file reading user data from files and a main file. Everytime I try to read the output of the returned pointer to the array of structs, where the data is stored, I get strange data. I think there is a problem when assigning strings to the first struct, because when I tried with test values (third file) it worked.</p>
  <p>Edit:
  I can read out integeres from the strcut, but I have problems with strings.</p>
  <p>Here is the code</p>
  <p>users.cpp (Creates the struct, included in main.cpp)</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  
  #define STRING_ERROR 4294967295
  #define MAX_USERS 100000
  using namespace std;
  
  struct user
  {
      int id;
      string firstname;
      string name;
      int age;
      int pin;
  };
  struct user_container{
      int size;
      user users [MAX_USERS];
  };
  
  typedef struct user User;
  typedef struct user_container container;
  
  
  void print_user_vars(User *user){
      cout &lt;&lt; &quot;Id: &quot; &lt;&lt; user-&gt;id &lt;&lt; &quot;\nFirstname: &quot; &lt;&lt; user-&gt;firstname &lt;&lt; &quot;\nName: &quot; &lt;&lt; user-&gt;name &lt;&lt; &quot;\nAge: &quot; &lt;&lt; user-&gt;age &lt;&lt; &quot;\nPIN: &quot; &lt;&lt; user-&gt;pin &lt;&lt; endl;
  }
  
  int get_usercount(string path){
      int usercount = 0;
      ifstream file;
      string line;
      file.open(path, ios::binary | ios::in);
      while(true){
          getline(file , line);
          if(file.fail())
              break;
          if(line.find(&quot;}&quot;) != STRING_ERROR){
              usercount++;
          }
      }
      file.close();
      return usercount;
  }
  
  container * get_users(string path){
      const int usercount = get_usercount(path);
      ifstream file;
      string line;
      User users[usercount];
      const char *values[5] = {&quot;id&quot;, &quot;firstname&quot;, &quot;name&quot;, &quot;age&quot;, &quot;pin&quot;};
  
      file.open(path, ios::binary | ios::in);
      User proto_user;
      int user_num = 0;
      while(true){
          getline(file , line);
          if(file.fail())
              break;
          if(line.find(&quot;:&quot;) != STRING_ERROR){
              for(int i = 0; i &lt; 5; i++){
                  if(line.find(values[i]) != STRING_ERROR){
                      string value;
                      for(int v = 0; v &lt; line.length(); v++){
                          if(v &gt; line.find_first_of(&quot;:&quot;)){
                              value += line[v];
                          }
                      }
                      if(values[i] == &quot;id&quot;){
                          proto_user.id = stoi(value);
                      }
                      else if(values[i] == &quot;firstname&quot;){
                          proto_user.firstname = value;
                      }
                      else if(values[i] == &quot;name&quot;){
                          proto_user.name = value;
                      }
                      else if(values[i] == &quot;age&quot;){
                          proto_user.age = stoi(value);
                      }
                      else if(values[i] == &quot;pin&quot;){
                          proto_user.pin = stoi(value);
                      }
                      break;
                  }
              }
          }
          else if(line.find(&quot;}&quot;) != STRING_ERROR){
              //print_user_vars(&amp;proto_user);
              users[user_num++] = proto_user;
              //cout &lt;&lt; &quot;Added user at &quot; &lt;&lt; user_num &lt;&lt; endl;
          }
          //cout &lt;&lt; line &lt;&lt; endl;
      }
  
      for(int i = 0; i &lt; user_num; i++){
          //cout &lt;&lt; &quot;\nUSER: &quot; &lt;&lt; i + 1 &lt;&lt; endl;
          //print_user_vars(&amp;users[i]);
      }
  
      static container con;
      con.size = usercount;
      for(int i = 0; i &lt; usercount; i++){
          if(i &lt;= MAX_USERS){
              // con.users[i] = users[i] didnt work, but this does
              con.users[i].firstname = users[i].firstname;
              con.users[i].age = users[i].age;
              con.users[i].name = users[i].name;
              con.users[i].pin = users[i].pin;
              con.users[i].id = users[i].id;
          }
      }
      print_user_vars(&amp;con.users[0]);
  
      return &amp;con;
  }
  </code></pre>
  <p>main.cpp (Calls the function)</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &quot;includes/users.h&quot;
  
  using namespace std;
  
  int main(void){
      string wasd = &quot;w&quot;;
      int id;
      int pin;
      cout &lt;&lt; &quot;\n    Welcome\n\n    ID\n&gt;&gt;&gt; &quot;;
      cin &gt;&gt; id;
      cout &lt;&lt; &quot;    PIN\n&gt;&gt;&gt; &quot;;
      cin &gt;&gt; pin;
      container *con = get_users(&quot;users&quot;);
      int size = con-&gt;size;
      cout &lt;&lt; &quot;Age: &quot; &lt;&lt; con-&gt;users[0].age &lt;&lt; &quot;, PIN: &quot; &lt;&lt; con-&gt;users[0].pin &lt;&lt; &quot;, Firstname: &quot; &lt;&lt; con-&gt;users[0].firstname &lt;&lt; &quot;, Name: &quot;
      &lt;&lt; con-&gt;users[0].name &lt;&lt; &quot;, ID: &quot; &lt;&lt; con-&gt;users[0].id &lt;&lt; endl;  
  
      //Functionality
  
      return 0;
  }
  </code></pre>
  <p>Simpler.cpp (Just like users.cpp, but with test values, included in main.cpp)</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  
  #define STRING_ERROR 4294967295
  #define MAX_USERS 100000
  using namespace std;
  
  struct user
  {
      int id;
      string firstname;
      string name;
      int age;
      int pin;
  };
  struct user_container{
      int size;
      user users [MAX_USERS];
  };
  
  typedef struct user User;
  typedef struct user_container container;
  
  container * get_users(string path){
      const int usercount = 2;
      User users[usercount];
  
      users[0].age = 59;
      users[0].firstname = &quot;Peter&quot;;
      users[0].name = &quot;Bremer&quot;;
      users[0].id = 456878;
      users[0].pin = 1234;
  
      users[1].age = 8;
      users[1].firstname = &quot;a&quot;;
      users[1].name = &quot;b&quot;;
      users[1].id = 456;
      users[1].pin = 1111;
  
  
      static container con;
      con.size = usercount;
      for(int i = 0; i &lt; usercount; i++){
          if(i &lt;= MAX_USERS){
              // con.users[i] = users[i] didnt work, but this does
              con.users[i].firstname = users[i].firstname;
              con.users[i].age = users[i].age;
              con.users[i].name = users[i].name;
              con.users[i].pin = users[i].pin;
              con.users[i].id = users[i].id;
          }
      }
      cout &lt;&lt; &quot;Usercount &quot; &lt;&lt; usercount &lt;&lt; endl;
      return &amp;con;
  }
  
  int main(void){
      container *con = get_users(&quot;users&quot;);
      int size = con-&gt;size;
      cout &lt;&lt; &quot;Age: &quot; &lt;&lt; con-&gt;users[0].age &lt;&lt; &quot;, PIN: &quot; &lt;&lt; con-&gt;users[0].pin &lt;&lt; &quot;, Firstname: &quot; &lt;&lt; con-&gt;users[0].firstname &lt;&lt; &quot;, Name: &quot;
      &lt;&lt; con-&gt;users[0].name &lt;&lt; &quot;, ID: &quot; &lt;&lt; con-&gt;users[0].id &lt;&lt; endl;
      return 0;
  }
  </code></pre>