title: strtok read length
body: <p>Here's the contents of a text file:</p>
  
  <pre><code>SQUARE 2
  SQUARE
  RECTANGLE 4 5
  </code></pre>
  
  <p>I'm trying to figure out why my strtok() loop won't take the end of the 2ND "SQUARE" and just make the length = 0. Don't fully understand the concept behind strtok either, I wouldn't mind a lecture about strtok. Here's the code: </p>
  
  <pre><code>#include &lt;cstring&gt;
  #include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  using std::cout;
  using std::endl;
  using std::cin;
  using std::ios;
  
  #include&lt;iomanip&gt;
  using std::setprecision;
  
  #include &lt;fstream&gt;
  using std::ifstream;
  
  const int MAX_CHARS_PER_LINE = 512;
  const int MAX_TOKENS_PER_LINE = 20;
  const char* const DELIMITER = " ";
  
  int main()
  {
  // create a file-reading object
  ifstream fin;
  fin.open("geo.txt"); // open a file
  if (!fin.good()) 
      return 1; // exit if file not found
  
  //PI
  float pi = 3.14159265359; 
  
  //DIMENSIONS
  float length, width, height, radius;
  
  //AREAS, PERIMETERS, VOLUMES
  float areaSquare, periSquare;
  float areaRectangle, periRectangle;
  float areaCube, volCube;
  float areaPrism, volPrism;
  float areaCircle, periCircle;
  float areaCylinder, volCylinder;
  
  // read each line of the file
  while (!fin.eof())
  {
      // read an entire line into memory
      char buf[MAX_CHARS_PER_LINE];
      fin.getline(buf, MAX_CHARS_PER_LINE);
      // parse the line into blank-delimited tokens
      int n = 0; // a for-loop index
  
      // array to store memory addresses of the tokens in buf
      const char* token[MAX_TOKENS_PER_LINE] = {0}; // initialize to 0
  
      // parse the line
      token[0] = strtok(buf, DELIMITER); // first token
      if (token[0]) // zero if line is blank
      {
          for (n = 1; n &lt; MAX_TOKENS_PER_LINE; n++)
          {
              token[n] = strtok(0, DELIMITER); // subsequent tokens
              if (!token[n] || token[n]==0) break;
          }
      }
  
      if(strcmp("SQUARE", token[0]) == 0) //1
      {
          length = atof(token[1])?atof(token[1]):0;
          areaSquare = length * length;
          periSquare = 4 * length;
  
          cout.setf(ios::fixed|ios::showpoint);
          cout &lt;&lt; setprecision(2);
          cout &lt;&lt; token[0] &lt;&lt; ' ' &lt;&lt; "length="&lt;&lt; token[1] &lt;&lt; ' ';
          cout &lt;&lt; "Area=" &lt;&lt; areaSquare &lt;&lt; ' ';
          cout &lt;&lt; "Perimeter=" &lt;&lt; periSquare &lt;&lt; '\n';
          cout.unsetf(ios::fixed|ios::showpoint);
          cout &lt;&lt; setprecision(6);
      }
  
      else if(strcmp("RECTANGLE", token[0]) == 0) //2
      {
          length = atof(token[1])?atof(token[1]):0;
          width = atof(token[2])?atof(token[2]):0;
  
          areaRectangle = length * width;
          periRectangle = 2 * length + 2 * width;
  
          cout.setf(ios::fixed|ios::showpoint);
          cout &lt;&lt; setprecision(2);
          cout &lt;&lt; token[0] &lt;&lt; ' ' &lt;&lt; "length="&lt;&lt; token[1] &lt;&lt; ' ';
          cout &lt;&lt; "width=" &lt;&lt; token[2] &lt;&lt; ' ' ;
          cout &lt;&lt; "Area=" &lt;&lt; areaRectangle &lt;&lt; ' ';
          cout &lt;&lt; "Perimeter=" &lt;&lt; periRectangle &lt;&lt; '\n';
          cout.unsetf(ios::fixed|ios::showpoint);
          cout &lt;&lt; setprecision(6);
          }
      else
      {
          cout &lt;&lt; "End of program. Press ENTER to exit.";
          cin.ignore(1000,10);
          break;
  }
      }
  }
  </code></pre>