title: Reading integers and chars from a file in C++
body: <p>So, i'm trying to write a program that can read data from a file. 
  The file consists of integers, chars and doubles. And i have to be able to assign them to different variables for further calculation (this is actually just a part of a bigger program). I've searched around the web (here also), and i've seen different solutions. stringstream, vectors and whatnot. The one i got furthest with, was using "skipws" when reading. But i can't seem to read the integers properly.  </p>
  
  <p>I would prefer a solution that requires the use of as few methods (streams, gets etc) as possible. But if i need more, i can live with it. </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;cstring&gt;
  
  using namespace::std;
  
  int main()
  {
      int j = 0, kill = 0, i;
      char initials[10];
      int results[10];
      double avg;
  
      ifstream read("P1.txt");
  
      if (!read) //test to see if file can be opened.
      {
          cerr &lt;&lt; "Error" &lt;&lt; endl;
          exit(1);
      }
  
      while(kill != 1) //kill = 1 same as eof. Just my weird way of coding. 
      {
          switch (j)
          {
              case 0:     //read the first line of data, which is the chars.
                  read &gt;&gt; skipws &gt;&gt; initials;
                  j++;
                  break;
  
              case 1: //read the second line of data, which is 10 integers and whitespace
                  for (i=0;i&lt;10;i++)
                  read &gt;&gt; skipws &gt;&gt; results[i];
                  j++;
                  break;
  
              case 2:     //read the last line which is a single double.
                  read &gt;&gt; skipws &gt;&gt; avg;
                  j++;
                  break;
  
              case 3: //check for end of file. 
                  if(read.eof() == 0)
                      kill = 1;
                  break;
          }
      }
      //the 3 lines below are just writing the contents of the file to the prompt. 
      //It's to check that values are stored correctly for further use. 
      cout &lt;&lt; initials &lt;&lt; endl; 
      cout &lt;&lt; results &lt;&lt; endl;
      cout &lt;&lt; avg &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>
  
  <p>I know exactly what y input file, "P1.txt", will look like, because i create it myself in another part of the program. It looks like this: </p>
  
  <pre><code>ccs
  2 4 5 3 1 4 6 1 1 1
  2.8       
  </code></pre>
  
  <p>Those 3 lines gives me the initials of a player, his results of 10 games and his average score. In short, the data of one player. In the final program, i need to be able to read values for maybe 10 players. </p>
  
  <p>I'm expecting the 3 cout lines in the end to show this: </p>
  
  <pre><code>ccs
  2453146111
  2.8
  </code></pre>
  
  <p>Now, this is as far as i have gotten. 
  If i declare results[10] as char i can get this in my console: </p>
  
  <pre><code>ccs
  2453146111ccs
  2.8
  </code></pre>
  
  <p>If i declare it as int i get this</p>
  
  <pre><code>ccs
  0x7fff5fbff6c0
  2.8
  </code></pre>
  
  <p>Obviously, i can see that values aren't stored properly and something is out of hand, but i need another persons eyes on this. I'm out of clues. </p>
  
  <p>I know this is a messy way to do it and that it can be done in less space, but i'm new to C++, so this made sense to me, and made it fairly easy for me to locate my mistakes, until now.</p>
  
  <p>I tried to explain it as well as i could. This is my first post here, so please tell me if you need more info.</p>
  
  <p>Thanks in advance!!
  Chris.   </p>