title: How to properly read and parse a string of integers from stdin C++
body: <p>Just as the title says, I'm reading in a string of integers from stdin. The data i'm trying to read in appears in the following form in a text file:</p>
  
  <pre><code>3
  4
  7 8 3
  7 9 2
  8 9 1
  0 1 28
  etc...    
  </code></pre>
  
  <p>The first two lines are always just single numbers (no problems there!) and the following lines each have 3 numbers. This file is redirected as stdin to my program (myprogram &lt; textfile). </p>
  
  <p>I've tried so many things but have not been able to successfully to do this! It seems so simple but I keep tripped up on where (or how) i should convert to an integer. Here is my latest attempt:</p>
  
  <pre><code>int main()
  {
  
    string str, str1, str2;
    int numCities;
    int numRoads, x, y, z;
    cin &gt;&gt; numCities &gt;&gt; numRoads;
  
    cout &lt;&lt; numCities &lt;&lt; " " &lt;&lt; numRoads &lt;&lt; endl;
    //getline(cin, str1);
  
  
    while( getline(cin, str))
    {
      char *cstr;
      cstr = new char[str.size()+1];
      strcpy(cstr, str.c_str());
  
      x = atoi(strtok(cstr, " ")); //these will be stored in an array or something
      y = atoi(strtok(NULL, " ")); //but right now i just want to at least properly
      z = atoi(strtok(NULL, " ")); //store the appropriate values in these variables!
  
  
    }
  
  
   return 0;
  
  }
  </code></pre>
  
  <p>I segfault when i try to use atoi...</p>
  
  <p>Thanks in advance!</p>