title: Reading information into array of structs c++
body: <p>I'm making a program for my c++ class. Ultimately I want my program to perform a quicksort on a text file of contacts in the following format:</p>
  
  <p>Firstname Secondname Number</p>
  
  <p>Each contact is separated by a new line.  I've started by counting the number of lines and using dynamic memory allocation to create an array of structs which has the same size as the number of lines.  </p>
  
  <p>However, when I tried to read in the information from the text file and output it to the screen, all I get is gibberish.  I've had a look around on the internet to try and find a solution but everything I've found seems to use a different syntax to me.</p>
  
  <p>Here's my code so far:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;istream&gt;
  
  char in[20];
  char out[20];
  
  using namespace std;
  
  struct contact
  {
      char firstName[14];
      char surName[14];
      char number[9];
  };
  //structure definition
  
  int main(void){
  
      cout &lt;&lt; "Please enter the input filename: " &lt;&lt; endl;
      cin &gt;&gt; in;
      ifstream input(in);
  
      if(!input){
          cerr &lt;&lt; "failed to open input file " &lt;&lt; in &lt;&lt; endl;
          exit(1);
      }
  
      cout &lt;&lt; "Please enter tne output filename: " &lt;&lt; endl;
      cin &gt;&gt; out;
  
      // read in the input and output filenames
  
      char a;
      int b=0;
      while (input.good ())
      {
          a=input.get ();
          if (a=='\n')
          {
              b++;
          }
      }
      // count the number of lines in the input file
  
      input.seekg (0, ios::beg);
  
      //rewind to beginning of file
  
      contact* list = new contact[b];
  
      //dynamically create memory space for array of contacts
  
      int i = 0.;
        while(input){
              if(i &gt;= b) break;
              if(input &gt;&gt; *list[i].firstName &gt;&gt; *list[i].surName &gt;&gt; *list[i].number) i++;  
              else break;
        }
      input.close();
  
      //read information from input file into array of contacts
  
      for(int N = 0; N &lt; b; N++){
          cout &lt;&lt; list[N].firstName &lt;&lt; list[N].surName &lt;&lt; list[N].number &lt;&lt; endl;
      }
  
      ofstream output(out);
      int k = 0;
      for(int k = 0; k&lt;b; k++){
          output &lt;&lt; list[k].firstName &lt;&lt; "        " &lt;&lt; list[k].surName &lt;&lt; "       " &lt;&lt; list[k].number &lt;&lt; endl;
      }
  
      //print out the unsorted list to screen and write to output file
      //i've done both here just to check, won't print to screen in final version
  
      output.close();
      delete []list;
  
  }       // end of main()
  </code></pre>