title: "How to simplify an program for calculating molar mass of chemical fusion
body: <p>The below program that calculates the molar by a given molecular formula
  mass of a chemical compound containing no other elements than carbon,
  hydrogen, nitrogen and oxygen.</p>
  
  <p>At start up, the program should display the message on the console
  <code>Please enter the formula in the form CcHhNnOo (c, h, n, o - integers)</code></p>
  
  <p>The user must then enter a formula in accordance with the above template.
  If the chemical indices of any of the atoms are 0 or 1, they must be given by a number.</p>
  
  <p>For example, for lower compounds, enter the text in blue:</p>
  
  <pre><code>C0H2N0O1 (water, H2O) MW = 18.015
  C17H4N0O0 (methane, CH4) MW = 16.043
  C7H5N3O6 (trinitrotoluene, C7H5N3O6) MW = 227.132
  C9H13N103 (adrenaline, C9H13NO3) MW = 183.207
  C12H22N0O11 (sucrose, C12H22O11) MW = 342.297
  </code></pre>
  
  <p>The calculation can be made according to the below formula:
  <code>MW = nC.AC + nH.AH + nN.AN + nO.AO</code>
  where MW is the required molar mass. <code>nC, nH, nN and nO</code> are respectively the number of carbon, hydrogen, nitrogen and oxygen atoms in the molecule
  the compound, and <code>AC, AH, AN and AO</code> are the corresponding atomic masses:</p>
  
  <pre><code>AC = 12.011
  AH = 1.008
  AN = 14.007
  AO = 15.999
  </code></pre>
  
  <p>After the formula has been entered, the program has to calculate and write to
  the console sought molar mass. If the formula does not contain atoms arranged in the order line, instead of the molar table, the program should display the following message: <code>Wrong formula! Please use the form CcHhNnOo!</code></p>
  
  <p>So far what we have done is the full program, but with a lot of checks which should be removed, as we are trying to make the program as simple as 10 lines of code. The program so far has been written in C++ within CodeBlocks and that is were we would prefer to run it.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstring&gt;
  
  using namespace std;
  
  int main()
  {
  
      char formula[512];
  
      cout &lt;&lt; "Please enter formula in the form CcHhNnOo (c, h, n, o - integers):" &lt;&lt; endl;
      cin &gt;&gt; formula;
  
      if (formula[0] != 'C')
      {
          cout &lt;&lt; "Wrong formula! Please use the form CcHhNnOo!" &lt;&lt; endl;
          return 0;
      }
  
      int len = strlen(formula);
      char curr_element = ' ';
      bool C_entered = false, H_entered = false, N_entered = false;
      int nC = 0, nH = 0, nN = 0, nO = 0;
  
      for (int i = 0; i &lt; len; i++) 
      {
  
          if (formula[i] &gt;= '0' &amp;&amp; formula[i] &lt;= '9')
          {
              switch (curr_element)
              {
              case 'C':
                  nC *= 10;
                  nC += formula[i] - '0';
                  break;
              case 'H':
                  nH *= 10;
                  nH += formula[i] - '0';
                  break;
              case 'N':
                  nN *= 10;
                  nN += formula[i] - '0';
                  break;
              case 'O':
                  nO *= 10;
                  nO += formula[i] - '0';
                  break;
              }
          }
          else
          {
              switch (formula[i])
              {
              case 'C':
                  curr_element = 'C';
                  C_entered = true;
                  break;
              case 'H':
                  curr_element = 'H';
                  H_entered = true;
                  break;
              case 'N':
                  curr_element = 'N';
                  N_entered = true;
                  break;
              case 'O':
                  curr_element = 'O';
                  break;
              default:
                  cout &lt;&lt; "Wrong formula! Please use the form CcHhNnOo!" &lt;&lt; endl;
                  return 0;
              }
  
              if ((curr_element == 'H' &amp;&amp; C_entered == false) ||
                  (curr_element == 'N' &amp;&amp; H_entered == false) ||
                  (curr_element == 'O' &amp;&amp; N_entered == false)) 
              {
                  cout &lt;&lt; "Wrong formula! Please use the form CcHhNnOo!" &lt;&lt; endl;
                  return 0;
              }
          }
  
      }
  
      float weight = nC * 12.011f + nH * 1.008f + nN * 14.007f + nO * 15.999f;
      cout &lt;&lt; "MW = " &lt;&lt; weight &lt;&lt; endl;
  
      return 0;
  }
  
  </code></pre>
  
  <p>Ideally the perfect scenario would be to simplify the above code. </p>