title: Getting weird output with substring C++
body: <p>so I am having some odd problems with what seems like a very simple problem.</p>
  
  <p>I have a vector: </p>
  
  <p>vector(string) collectionOfLines;</p>
  
  <p>This holds lines of text that I got from a .txt file.  The contents of the text file are:</p>
  
  <p>"4</p>
  
  <p>Disk 0.2 0.00005</p>
  
  <p>Mouse 0.4 0.00002</p>
  
  <p>Keyboard 0.3 0.00004</p>
  
  <p>Network 0.5 0.0001"</p>
  
  <p>collectionOfLines[0] = "Disk 0.2 0.00005"</p>
  
  <p>I am trying to separate this string into three different strings that are: "Disk", "0.2", and "0.00005" and then put these strings into a different vector:</p>
  
  <p>vector(string) collectionOfCommands;</p>
  
  <p>This is my loop for getting the substrings from the line string and putting them into the new vector.</p>
  
  <pre><code>string deviceName;
  string interruptProbability;
  string interruptTime;
  
  for(int i = 1; i &lt; collectionOfLines.size(); i++) { // i = 1 because I am ignoring the "4" in the txt file
      string currentLine = collectionOfLines[i];
      int index = 0;
      for(int j = 0; j &lt; currentLine.length(); j++) {
          if(j == 0) {
              continue;
          } else if(deviceName.empty() &amp;&amp; currentLine[j-1] == ' ') {
              deviceName = currentLine.substr(index, j-1);
              index = j;
          } else if (interruptProbability.empty() &amp;&amp; currentLine[j-1] == ' ') {
              interruptProbability = currentLine.substr(index, j-1);
              index = j;
          } else if (!deviceName.empty() &amp;&amp; !interruptProbability.empty()) {
              interruptTime = currentLine.substr(index, currentLine.length());
              break;
          } else {
              continue;
          }
      }
      collectionOfCommands.push_back(deviceName);
      collectionOfCommands.push_back(interruptProbability);
      collectionOfCommands.push_back(interruptTime);
  }
  </code></pre>
  
  <p>When I run this I get no errors, but when I print the output of collectionOfCommands I get:</p>
  
  <p>"Disk</p>
  
  <p>0.2 0.00</p>
  
  <p>0.00005</p>
  
  <p>Disk </p>
  
  <p>0.2 0.00</p>
  
  <p>Mouse 0.4 0.00002</p>
  
  <p>Disk 0.2 0.00</p>
  
  <p>Keyboard 0.3 0.00004</p>
  
  <p>Disk 0.2 0.00</p>
  
  <p>Network 0.5 0.0001"</p>
  
  <p>Obviously this output is completely wrong except for the very first output, "Disk."</p>
  
  <p>Help would be much appreciated, thank you!!!!</p>