title: Project Euler Problem 17 - What's wrong?
body: <p>I decided to try project euler problem 17 today, and I quickly wrote a pretty fast code in C++ to solve it. However, for some reason, the result is wrong.
  The question is:</p>
  
  <p>If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.</p>
  
  <p>If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?</p>
  
  <p>NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.</p>
  
  <p>I seriously don't know why, as I have checked every part of my program thoroughly and I can't find anything wrong. The only thing bad I could find is when checking for 1000, my while loop doesn't check correctly. I fixed that by lowering the limit of my while loop to &lt;1000 instead of &lt;1001 and just added 11(onethousand = 11) manually to the sum. And yet, it doesn't work. I would really appreciate it if you could tell me what's wrong. I'm sure my code is pretty bad, but it's something done in a few minutes. So here it is:</p>
  
  <pre><code>int getDigit (int x, int y)
  {
   return (x / (int)pow(10.0, y)) % 10;
  }
  
  int main()
  {
   string dictionary[10] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
   string dictionary2[18] = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
   string dictionary3[10] = { "onehundred", "twohundred", "threehundred", "fourhundred", "fivehundred", "sixhundred", "sevenhundred", "eighthundred", "ninehundred", "onethousand" };
  
   int i = 1;
   int last;
   int first;
   int middle;
  
   _int64 sumofletters = 0;
  
   while (i &lt; 10)     //OK
   {
    sumofletters += dictionary[i].length();
  
    i++;
   }
  
   cout &lt;&lt; sumofletters &lt;&lt; endl;
  
   while (i &lt; 20)     //OK
   {
    last = i % 10;
  
    sumofletters += dictionary2[last].length();
  
    i++;
   }
  
   while (i &lt; 100)     //OK 
   {
    first = (i / 10) + 8;
    last = i % 10;
  
    if (last != 0)
    {
     sumofletters += dictionary2[first].length() + dictionary[last].length();
    }
  
    else
     sumofletters += dictionary2[first].length();
  
    i++;
   }
  
   cout &lt;&lt; sumofletters &lt;&lt; endl;
  
   while (i &lt; 1000)       //OK
   {
    last = i % 10;
    first = (i / 100) - 1;
    middle = (getDigit(i, 1)) + 8;
  
    if (middle != 0 &amp;&amp; last != 0)   //OK
    {
     if (middle == 1)
      sumofletters += dictionary3[first].length() + dictionary2[last].length() + 3;
     else
      sumofletters += dictionary3[first].length() + dictionary2[middle].length() + dictionary[last].length() + 3;
    }
  
    else if (last == 0 &amp;&amp; middle != 0)  //OK
    {
     if (middle == 1)
      sumofletters += dictionary3[first].length() + 6;
     else
      sumofletters += dictionary3[first].length() + dictionary2[middle].length() + 3;
    }
  
    else if (middle == 0 &amp;&amp; last != 0)   //OK
     sumofletters += dictionary3[first].length() + dictionary[last].length() + 3;
  
    else
     sumofletters += dictionary3[first].length();
  
    i++;
   }
  
   sumofletters += 11;
  
   cout &lt;&lt; sumofletters &lt;&lt; endl;
  
   return 0;
  }
  </code></pre>