title: Parse int and string
body: <p>Hi I'm not sure if this is the right place to ask this question.
  Anyway I have written this code to parse a molecule formula and split it into atoms and amount of each atoms.</p>
  
  <p>For instance if I input <code>"H2O"</code> I will for the atom array get <code>{"H", "O"}</code> and in the amount array I will get <code>{2, 1}</code>. I haven't taken account for amount that is larger than 9, since I don't think there are molecule which can bind to something that is larger than 8.</p>
  
  <p>Anyway I'm quite newbie, so I wonder if this piece of code can be made better?</p>
  
  <pre><code>   string formula = "H2O";
     int no, k = 0, a = 0;
     string atom[10];
     int amount[10];
     bool flag = true;
     stringstream ss(formula);
  
     for(int i = 0; i &lt; formula.size(); ++i)
     {
  
        no = atoi(&amp;formula[i]);
        if(no == 0 &amp;&amp; (flag || islower(formula[i]) )  )
        {
           cout &lt;&lt; "k = " &lt;&lt; k &lt;&lt; endl;
           atom[k] += formula[i];
           flag = false;
           cout &lt;&lt; "FOO1 " &lt;&lt; atom[k] &lt;&lt; endl;
           amount[a] = 1;
        }
        else if(no != 0)
        {
           amount[a] = no;
           cout &lt;&lt; "FOO2 " &lt;&lt; amount[a] &lt;&lt; endl;
           a++;
           flag = true;
           k++;
        }
        else
        {
           k++;
           a++;
           atom[k] = formula[i];
           cout &lt;&lt; "FOO3 " &lt;&lt; atom[k] &lt;&lt; endl;
           amount[a] = 1;
  
           flag = false;
        }
  
        cout &lt;&lt; no &lt;&lt; endl;
     }
  </code></pre>