title: C++ converting number to words
body: <p>I came across this program in a book to convert a number to words. The initial program converts numbers 1-1000 but then you are asked to modify the program to accept numbers up to 1,000,000. I got it working with numbers up to 20,999 but can't get it to work past that. I've been tinkering with it all day and looked at a lot of program examples online but everyone that I looked at is using techniques that are beyond me as a beginner. Any advice would be greatly appreciated. Here is what I have so far.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  string digitName(int digit);
  string teenName(int number);
  string tensName(int number);
  string intName(int number);
  
  
  int main()
  {
      int input;
  
      do
      {
  
      cout &lt;&lt; "Please enter a positive integer: ";
  
      cin &gt;&gt; input;
  
      cout &lt;&lt; "\n" &lt;&lt; intName(input) &lt;&lt; endl;
  
      cout &lt;&lt; "\n\n" &lt;&lt; endl;
  
      }while (input &gt; 0);
  
      return 0;
  }
  
  string digitName(int digit)
  {
      if (digit == 1) return "one";
      if (digit == 2) return "two";
      if (digit == 3) return "three";
      if (digit == 4) return "four";
      if (digit == 5) return "five";
      if (digit == 6) return "six";
      if (digit == 7) return "seven";
      if (digit == 8) return "eight";
      if (digit == 9) return "nine";
  
      return "";
  }
  
  string teenName(int number)
  {
      if (number == 10) return "ten";
      if (number == 11) return "eleven";
      if (number == 12) return "twelve";
      if (number == 13) return "thirteen";
      if (number == 14) return "fourteen";
      if (number == 15) return "fifteen";
      if (number == 16) return "sixteen";
      if (number == 17) return "seventeen";
      if (number == 18) return "eighteen";
      if (number == 19) return "nineteen";
  
      return "";
  }
  
  string tensName(int number)
  {
      if (number &gt;= 90) return "ninety";
      if (number &gt;= 80) return "eighty";
      if (number &gt;= 70) return "seventy";
      if (number &gt;= 60) return "sixty";
      if (number &gt;= 50) return "fifty";
      if (number &gt;= 40) return "fourty";
      if (number &gt;= 30) return "thirty";
      if (number &gt;= 20) return "twenty";
  
      return "";
  }
  
  string intName(int number)
  {
      int part = number;
      string name;
  
      if (part &gt;= 20000)
      {
          if (part % 10000 == 0)
          {
              name = tensName(part / 1000) + " thousand ";
              part = part % 1000;
  
          }else
          {
              name = tensName(part / 1000) + " ";
              part = part % 10000;
          }
      }
  
      if (part &gt;= 10000)
      {
          name = teenName(part / 1000) + " thousand ";
          part = part % 1000;
      }
  
      if (part &gt;= 1000)
      {
          name = digitName(part / 1000) + " thousand ";
          part = part % 1000;
      }
  
      if (part &gt;= 100)
      {
          name = name + digitName(part / 100) + " hundred";
          part = part % 100;
      }
  
      if (part &gt;= 20)
      {
          name = name + " " + tensName(part);
          part = part % 10;
  
      }else if (part &gt;= 10)
      {
          name = name + " " + teenName(part);
          part = 0;
      }
  
      if (part &gt; 0)
      {
          name = name + " " + digitName(part);
      }
  
      return name;
  }
  </code></pre>