title: c++ get array string from text file
body: <p>my text file was like</p>
  
  <pre><code>Jason Derulo
  91 Western Road,xxxx,xxxx
  1000
  david beckham
  91 Western Road,xxxx,xxxx
  1000
  </code></pre>
  
  <p>i'm trying to get the data from a text file and save it into arrays however when i want to store the data from the text file into array it loop non-stop. what should i do ? the problem exiting in looping or the method i get the data from text file ?</p>
  
  <p>code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  typedef struct {
  
      char name[30];
      char address[50];
      double balance;
  
  } ACCOUNT;
  
  //function prototype
  void menu();
  void read_data(ACCOUNT record[]);
  
  int main() {
      ACCOUNT record[31]; //Define array 'record'  which have maximum size of 30
      read_data(record);  
  }
  //--------------------------------------------------------------------
  
  void read_data(ACCOUNT record[]) {
      ifstream openfile("list.txt");              //open text file 
  
      if (!openfile) {
          cout &lt;&lt; "Error opening input file\n";
          return 0;
      } else {
          int loop = -1;                  //size of array 
          cout &lt;&lt; "--------------Data From File--------------"&lt;&lt;endl;
          while (!openfile.eof())  {
          if (openfile.peek() == '\n') 
              openfile.ignore(256, '\n');
          openfile.getline(record[loop].name, 30);
          openfile.getline(record[loop].address, 50);
          openfile &gt;&gt; record[loop].balance;
          }
          openfile.close();               //close text file
  
          for (int i = 0; i &lt;= loop + 1; i++) {
              cout &lt;&lt; "Account "  &lt;&lt; endl;
              cout &lt;&lt; "Name         : " &lt;&lt; record[i].name &lt;&lt; endl;
              cout &lt;&lt; "Address      : " &lt;&lt; record[i].address &lt;&lt; endl;
              cout &lt;&lt; "Balance      : " &lt;&lt; record[i].balance &lt;&lt; endl;
          }
      }
  }
  </code></pre>