Danny Petrunov
Danny Petrunov

Reputation: 101

Numbers to words using arrays

I am trying to make a simple number to english words program and I've decided to use arrays. However whenever I enter a number greater than 99, I get an error in the third if clause.. What do I need to change to fix that? Here's my code;

    class Program
    {
        static void Main(string[] args)
        {
         string[] uptonineteen = {"Zero","One","Two","Three","Four",
        "Five","Six","Seven","Eight","Nine","Ten",
        "Eleven","Twelve","Thirteen","Fourteen","Fifteen",
        "Sixteen","Seventeen","Eighteen","Nineteen"};
        string[] ten = {"","","Twenty","Thirty","Forty","Fifty",
        "Sixty","Seventy","Eighty","Ninety",};
        Console.WriteLine(" ---------------");
        int i = int.Parse(Console.ReadLine());
            if (i < 20)
            {
                Console.WriteLine(uptonineteen[i]);
            }
            if (i < 100)
            {
                Console.WriteLine(ten[i / 10] + ((i % 10 > 0) ? "" + uptonineteen[i%10] : ""));
            }
            if (i <= 999)
            {
                object lenthree = ten[i / 100] + "hundred"+" " + ((i % 100 > 0) ? "and" +" "+  uptonineteen[i % 1000] : "");
                Console.WriteLine(lenthree);
            } 
            Console.ReadKey();
        }
    }
}

Upvotes: 0

Views: 3694

Answers (5)

trademark
trademark

Reputation: 1

string[] ones = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", 
                            "eleven", "twelve", "thirtheen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen","nineteen", "twenty" };
string[] tens = { "Ten", "Twenty", "Thirthy", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};

        Console.WriteLine("Enter a number");
        int i = int.Parse(Console.ReadLine());
        if (i < 20)
        {
            Console.WriteLine(ones[i]);
        }
        if (i > 30  & i < 100)
        {
            Console.WriteLine(tens[((i%100)/10)-1]+ " " + ones[(i % 10)]);
        }
        if (i > 100 & i < 1000)
        {
            if (((i % 100) % 10) == 0)
            {
                Console.WriteLine(ones[(i / 100)] + " hundred " + tens[((i % 100) / 10) - 1]);
            }
            else
            {
                Console.WriteLine(ones[(i / 100)] + " hundred " + tens[((i % 100) / 10) - 1] + " and " + ones[((i % 100) % 10)]);
            }
        }
        Console.ReadLine();

Upvotes: 0

DRapp
DRapp

Reputation: 48139

I've seen this too many times... This one handles, such as doing check writing and you may need to go into millions, thousands and includes cents at the end...

public class NumToWords
{
    public NumToWords()
    {}

    string[] OneToNineteen = 
        { "zero",   
          "one",    "two",    "three",    "four",     "five",    "six",     "seven",     "eight",    "nine",   "ten",   
          "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };

    // empty string for zero-place-holder for tens grouping
    string[] Tens = { "", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

    // leave blank for "hundreds" because you would NOT do 123 as One hundred twenty-three hundred.
    string[] Block3Section = { "", "thousand", "million", "billion" };


    public string ToEnglish(int num)
    {
        int Block3Seq = 0;
        int curSegment;
        int tmp;
        string FinalWords = "";
        string curWord = "";

        while (num > 0)
        {
            curSegment = num % 1000;    // get only 3 digits worth (right) of the number
            num = (num - curSegment) / 1000;  // subtract this portion from the value

            if (curSegment > 0)
            {
                // always add the closing word as we HAVE something to build out
                curWord = "";

                // how many "hundreds" of the current segment
                tmp = (int)curSegment / 100;
                if (tmp > 0)
                    curWord += " " + OneToNineteen[tmp] + " hundred";

                // what is remainder of this 100 based segment
                tmp = curSegment % 100;

                if (tmp < 20)
                    curWord += " " + OneToNineteen[tmp];
                else
                {
                    curWord += " " + Tens[(int)tmp / 10]
                                + '-' + OneToNineteen[tmp % 10];
                }

                // always add the closing word as we HAVE something to build out
                curWord += " " + Block3Section[Block3Seq];

                // add the section above to the overall words
                FinalWords = curWord + FinalWords;
            }

            // to allow the next closing word for segment such as thousand, million, billion, etc
            Block3Seq++;
        }

        return FinalWords;
    }

}

Upvotes: 1

Kevin
Kevin

Reputation: 704

Try this...

class Program
{
    static void Main(string[] args)
    {
        string[] uptonineteen = {"Zero","One","Two","Three","Four",
        "Five","Six","Seven","Eight","Nine","Ten",
        "Eleven","Twelve","Thirteen","Fourteen","Fifteen",
        "Sixteen","Seventeen","Eighteen","Nineteen"};
        string[] ten = {"", "","Twenty","Thirty","Forty","Fifty",
        "Sixty","Seventy","Eighty","Ninety",};
        Console.WriteLine(" ---------------");
        int i = int.Parse(Console.ReadLine());
        if (i < 20)
        {
            Console.WriteLine(uptonineteen[i]);
        }
        else if (i < 100)
        {
            Console.WriteLine(((((i % 100) / 10) > 1) ? ten[((i % 100) / 10)] + ((i % 10) > 0 ? " " + uptonineteen[(i % 10)] : "") : " and " + uptonineteen[i % 100]));
        }
        else if (i <= 999)
        {
            object lenthree = uptonineteen[(i % 1000) / 100] + " hundred " + ((((i % 100) / 10) > 1) ? ten[((i % 100) / 10)] + ((i % 10) > 0 ? " " + uptonineteen[(i % 10)] : "") : " and " + uptonineteen[i % 100]);
            Console.WriteLine(lenthree);
        } 
        Console.ReadLine();
    }
}

Upvotes: 1

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112392

After handling a case number > x you should continue with the remainder number = number % x or number %= x which is the same. Also you should really make the distinction between getting the English text and doing tests on the console. Console.WriteLines and Console.ReadKeys should not appear withing the logic. Build the English string using a StringBuilder by using this general structure:

public static string ToEnglish(int number)
{
    var sb = new StringBuilder();
    if (number >= 100) {
        ...
        number %= 100;
    }
    if (sb.Length > 0 && number != 0) {
        sb.Append("and ");
    }
    if (number >= 20) {
        ...
        number %= 10;
    }
    if (sb.Length == 0 || number != 0) {
        ...
    }
    return sb.ToString();
}

Note: I don't post a complete solution since I assume its homework.


The console test looks like this

while (true) {
    Console.Write("Please enter a number or `q` to quit: ");
    string s = Console.ReadLine();
    if (s == "q") {
        break;
    }
    int i = int.Parse(s);
    Console.WriteLine(ToEnglish(i));
}

Upvotes: 0

Yuriy Faktorovich
Yuriy Faktorovich

Reputation: 68687

string lenthree = ten[i / 100] + "hundred" + " " + 
    ((i % 100 > 0) ? "and" + " " + uptonineteen[i % 100] : "");

The last 1000 should be 100. But since your uptonineteen isn't full, you still can't enter full numbers correctly.

Upvotes: 0

Related Questions