title: Recognize a piece of a string, delete it, then save the remainder of the string into a separate variable? c++
body: <p>The particular function I am writing is being passed a line (as a string) read from a file. All lines from the file start with a line number, then read a command, such as</p>
  
  <pre><code>    1 commandX hello
      2 commandY world
      ...
      12 commandZ yes
  </code></pre>
  
  <p>My function needs to first recognize the line number, save that into an integer variable, then erase the line number and the white space after the integer from the string so that only the command remains. Then, the command is stored into a global (pre-declared) array at the location of the integer variable. So far, I have the following code. However, this only works when the line number is a single digit (&lt;10).</p>
  
  <pre><code>void command(string &amp; in)
   {
     // take the string, identify the line number at the beginning,
     stringstream ss;
     string s;
     int i;
     // number identification
     char number = in.at(0);
     ss &lt;&lt; number;
     ss &gt;&gt; s;
     i = atoi(s.c_str());
  
     if (i &lt; 10)
      in.erase(0, 2);
     else if (i &lt; 100)
      in.erase(0, 3);
     else
      in.erase(0, 4);
  
     arr[i] = in;
  }
  </code></pre>
  
  <p>So this works for a line number up to 9, and sets the integer i equal to the integer line number value. I'm not sure how to fix it so that it can take a variable amount of digits (it needs to be able to read up to the line number 999, so up to three digits - hence why the erase only takes into account up to three digits plus the white space). </p>
  
  <p>Thus, in theory, if the lines at the top were read in, the array would look something like: arr[1] = "commandX hello", arr[2] = "commandY world", arr[12] = "commandZ yes", and so forth.</p>
  
  <p>Thanks!</p>