How to properly read and parse a string of integers from stdin C++
===

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:
    
    3
    4
    7 8 3
    7 9 2
    8 9 1
    0 1 28
    etc...    

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 < textfile). 

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:
    
    int main()
    {

      string str, str1, str2;
      int numCities;
      int numRoads, x, y, z;
      cin >> numCities >> numRoads;

      cout << numCities << " " << numRoads << 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;

    }

I segfault when i try to use atoi...

Thanks in advance!


