convert number into words

user2651033 picture user2651033 · Aug 4, 2013 · Viewed 12.3k times · Source

even though this question has been posted and answered before. I wanted help with my code. Task is to convert a number into words from 0 to 10 million. I have tried to do that with my code using GUI, problem is it doesn't return an answer at all. Can anyone help me in identifying what the problem on the code could possibly be.

code below:

private void btnConvertToText_Click(object sender, EventArgs e)
    {
        string ConvertedNumber = " ";
        int number = Convert.ToInt32(txtNumber.Text);
        int Count = 0;

           string [] ones =  {" One ", " Two ", " Three ", " Four ", " Five ", " Six ", " Seven ", " Eight ", " Nine "};
           string [] teens= {" Eleven ", " Twelve ", " Thirteen ", " Fourtte n ", " Fifteen ", " Sixteen ", " Seventeen ", " Eighteen ", " Nineteen "};
           string [] tens = {" Ten ", " Twenty ", " Thirty ", " Fourty ", " Fifty ", " Sixty ", " Seventy ", " Eighty ", " Ninenty "};
           string [] moreThenTens = {" hundred ", " thousand ", " million "};

           while (number >= 0)
           {
               if (number == 0)
               {
                   ConvertedNumber = "Zero";
               }

              else if (number < 10)
               {
                   for (int Counter = 1; Counter < 10; Counter++)
                   {
                       ConvertedNumber += ones[Counter-1];
                   }
               }

               else if (number >= 10 && number < 100)
              {
                  while (number > 10)
                  {
                      if (number > 10 && number < 20)
                      {
                          for (int x = 11; x < 20; x++)
                          {
                              if (number == x)
                              {
                                  ConvertedNumber = teens[x - 11];
                              }
                          }
                      }

                      else
                      {
                          number -= 10;
                          Count++;
                      }

                      ConvertedNumber += tens[Count - 1];
                  }
              }

               else if (number >= 100 && number < 1000)
               {
                   while (number > 100)
                   {
                       number -= 100;
                       Count++;
                   }
                   ConvertedNumber += ones[Count - 1] + moreThenTens[0];
               }

               else if (number >= 1000 && number < 10000)
               {
                   while (number > 1000)
                   {
                       number -= 1000;
                       Count++;
                   }

                   ConvertedNumber += ones[Count - 1] + moreThenTens[1];
               }

               else if (number >= 10000 && number < 100000)
               {
                   while (number > 10000)
                   {
                       if (number < 20000)
                       {
                           number -= 10000;
                           Count++;
                       }

                       else if (number >= 20000)
                       {
                           number -= 10000;
                           Count++;
                       }

                   }

                   if (Count >= 1)
                   {
                       if (number < 1000)
                       {
                           ConvertedNumber += tens[0] + moreThenTens[1];
                       }

                       else if (number >= 1000)
                       {
                           ConvertedNumber += teens[Count - 1] + moreThenTens[1];
                       }
                   }

                   else if (Count > 1)
                   {
                       ConvertedNumber += tens[Count - 1] + moreThenTens[1];
                   }
               }

               else if (number >= 100000 && number < 1000000)
               {
                   while (number > 100000)
                   {
                       number -= 100000;
                       Count++;
                   }

                   ConvertedNumber += ones[Count - 1] + moreThenTens[0] + moreThenTens[1];
               }

               else if (number >= 1000000 && number < 10000000)
               {
                   while (number > 1000000)
                   {
                       number -= 1000000;
                       Count++;
                   }

                   ConvertedNumber += ones[Count - 1] + moreThenTens[2];
               }

               else if (number == 10000000)
               {
                   while (number > 10000000)
                   {
                       number -= 10000000;
                       Count++;
                   }

                   ConvertedNumber += tens[0] + moreThenTens[2];
               }
           }

           txtConvertedNumber.Text = ConvertedNumber;
    }
}

Answer

bas picture bas · Aug 4, 2013
while (number >= 0)

It seems like your code is creating an infinite loop. Take for example:

if (number == 0)
{
    ConvertedNumber = "Zero";
}

This does not quit your loop, and therefore never arrives at

txtConvertedNumber.Text = ConvertedNumber;

Use a check to convert your number, like:

if (number == 0)
{
    ConvertedNumber = "Zero";
    number = -1;
}