Thread: Need help with Jumping into C++ - display numbers as text

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    1

    Need help with Jumping into C++ - display numbers as text

    Hello, for the past few days I've been stuck on the practice problem in the book Jumping into C++, I'd like to go to further chapters but this task is really hard, for me at least.

    Could you give me any help? This is what I've done so far, no idea what I did wrong (probably a lot), what right and what to do next... It's just a big mess.

    So far, I only implemented displaying the numbers under lesser than 20.

    Thanks in advance, here's the current state of my code.

    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    int main() {
        int input, tens, hundreds, thousands, units;
        string one, two, three, four, five, six, seven, eight, nine, ten;
        string eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen;
        string twenty, thirty, fourty, fifty, sixty, seventy, eighty, ninety;
        string hundred, thousand, million;
    
    
        //DATABASE of numbers
        one = "one";
        two = "two";
        three = "three";
        four = "four";
        five = "five";
        six = "six";
        seven = "seven";
        eight = "eight";
        nine = "nine";
        ten = "ten";
        eleven = "eleven";
        twelve = "twelve";
        thirteen = "thirteen";
        fourteen = "fourteen";
        fifteen = "fifteen";
        sixteen = "sixteen";
        seventeen = "seventeen";
        eighteen = "eighteen";
        nineteen = "nineteen";
        twenty = "twenty";
        thirty = "thiry";
        fourty = "fourty";
        fifty = "fifty";
        sixty = "sixty";
        seventy = "seventy";
        eighty = "eighty";
        ninety = "ninety";
        hundred = "hundred";
        thousand = "thousand";
        million = "million";
    
    
        cin>> input;
    
    
        if ( input > 1000000 || input < -1000000 ) {
            cout<< "The number is out of range!";
        }
        else
        {
            if ( input < 20 )
            {
                switch( input ) {
                case 1:
                    cout<< one;
                    break;
                case 2:
                    cout<< two;
                    break;
                case 3:
                    cout<< three;
                    break;
                case 4:
                    cout<< four;
                    break;
                case 5:
                    cout<< five;
                    break;
                case 6:
                    cout<< six;
                    break;
                case 7:
                    cout<< seven;
                    break;
                case 8:
                    cout<< eight;
                    break;
                case 9:
                    cout<< nine;
                    break;
                case 10:
                    cout<< ten;
                    break;
                case 11:
                    cout<< eleven;
                    break;
                case 12:
                    cout<< twelve;
                    break;
                case 13:
                    cout<< thirteen;
                    break;
                case 14:
                    cout<< fourteen;
                    break;
                case 15:
                    cout<< fifteen;
                    break;
                case 16:
                    cout<< sixteen;
                    break;
                case 17:
                    cout<< seventeen;
                    break;
                case 18:
                    cout<< eighteen;
                    break;
                case 19:
                    cout<< nineteen;
                    break;
                }
            }
            else {
                units = input - tens*10;
                tens = input / 10;
                hundreds = input / 100;
                thousands = input / 1000;
    
    
            }
        }
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I mean, it looks like you got most of it. One through twenty are most of the unique words.

    Now you just gotta realize:
    howmany = input / 100000; This tells you how many 100 thousands there are, so you can say "x hundred thousand"
    howmany = input / 1000; This tells you how many thousands there are, so you can say "xy thousand"
    howmany = input / 100; This tells you how many 100s there are so you can say "x hundred yz"

    Now you already did 1 through 20, so you should be able to see using howmany that way you can reuse the code that works, and cover lots of numbers.

    You can get very fancy with this, but the simplest version to understand (probably) uses recursion:
    Code:
    #include <string>
    #include <iostream>
    #include <iomanip>
    
    
    using namespace std;
    
    
    string explain(long n);
    
    
    int main()
    {
        long tests[] = {0, 11, 43, 266, 1403, 314159, 1000000, -1000000, 1234567890};
        for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i) {
            cout << setw(10) << tests[i] << ": " << explain(tests[i]) << endl;
        }
    }
    
    
    string explain(long n) 
    {
        const static string words[] = 
        {
            "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", 
            "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", 
            "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "thirty", 
            "forty", "fifty", "sixty", "seventy", "eighty", "ninety"
        };
        
        if (n < -1000000 || n > 1000000) return "unsupported";
        
        string out;
        if (n < 0) {
            out = "minus ";
            n = -n;
        }
        
        if (n == 1000000) return out + "one million";
        else if (n >= 1000) {
            out = out + explain(n / 1000) + " thousand";
            n %= 1000;
    
            if (n > 0) out = out + ' ' + explain(n);
        }
        else if (n >= 100) {
            out = out + words[n / 100] + " hundred";
            n %= 100;
            
            if (n > 0) out = out + ' ' + explain(n);
        }
        else if (n >= 20) {
            out += words[n / 10 + 18];
            n %= 10;
    
            if (n > 0) out = out + '-' + words[n]; 
        }
        else out += words[n];
    
        return out;
     }
    
    
             0: zero
            11: eleven
            43: forty-three
           266: two hundred sixty-six
          1403: one thousand four hundred three
        314159: three hundred fourteen thousand one hundred fifty-nine
       1000000: one million
      -1000000: minus one million
    1234567890: unsupported
    That's very convertible to your own algorithm, I think.
    Last edited by whiteflags; 07-01-2016 at 11:55 PM.

  3. #3
    Banned
    Join Date
    Oct 2014
    Location
    Home
    Posts
    135
    Quote Originally Posted by whiteflags View Post
    I mean, it looks like you got most of it. One through twenty are most of the unique words.

    Now you just gotta realize:
    howmany = input / 100000; This tells you how many 100 thousands there are, so you can say "x hundred thousand"
    howmany = input / 1000; This tells you how many thousands there are, so you can say "xy thousand"
    howmany = input / 100; This tells you how many 100s there are so you can say "x hundred yz"

    Now you already did 1 through 20, so you should be able to see using howmany that way you can reuse the code that works, and cover lots of numbers.

    You can get very fancy with this, but the simplest version to understand (probably) uses recursion:
    Code:
    #include <string>
    #include <iostream>
    #include <iomanip>
    
    
    using namespace std;
    
    
    string explain(long n);
    
    
    int main()
    {
        long tests[] = {0, 11, 43, 266, 1403, 314159, 1000000, -1000000, 1234567890};
        for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i) {
            cout << setw(10) << tests[i] << ": " << explain(tests[i]) << endl;
        }
    }
    
    
    string explain(long n) 
    {
        const static string words[] = 
        {
            "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", 
            "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", 
            "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "thirty", 
            "forty", "fifty", "sixty", "seventy", "eighty", "ninety"
        };
        
        if (n < -1000000 || n > 1000000) return "unsupported";
        
        string out;
        if (n < 0) {
            out = "minus ";
            n = -n;
        }
        
        if (n == 1000000) return out + "one million";
        else if (n >= 1000) {
            out = out + explain(n / 1000) + " thousand";
            n %= 1000;
    
            if (n > 0) out = out + ' ' + explain(n);
        }
        else if (n >= 100) {
            out = out + words[n / 100] + " hundred";
            n %= 100;
            
            if (n > 0) out = out + ' ' + explain(n);
        }
        else if (n >= 20) {
            out += words[n / 10 + 18];
            n %= 10;
    
            if (n > 0) out = out + '-' + words[n]; 
        }
        else out += words[n];
    
        return out;
     }
    
    
             0: zero
            11: eleven
            43: forty-three
           266: two hundred sixty-six
          1403: one thousand four hundred three
        314159: three hundred fourteen thousand one hundred fifty-nine
       1000000: one million
      -1000000: minus one million
    1234567890: unsupported
    That's very convertible to your own algorithm, I think.
    Congrats.

  4. #4
    Banned
    Join Date
    Oct 2014
    Location
    Home
    Posts
    135
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    
    int main()
    {
    long numbers, tempx, tempy;
    
    
    scanf("%D", &numbers);
    if(numbers == 0)
    {
    printf("\n zero");
    }
    
    
    tempx = numbers/10*10;
    tempy = numbers - tempx;
    tempx = numbers;
    numbers = tempy;
    
    
    if(numbers == 1)
    {
    printf("\n one");
    }
    
    
    if(numbers == 2)
    {
    printf("\n two");
    }
    
    
    if(numbers == 3)
    {
    printf("\n three");
    }
    
    
    if(numbers == 4)
    {
    printf("\n four");
    }
    if(numbers == 5)
    {
    printf("\n five");
    }
    if(numbers == 6)
    {
    printf("\n six");
    }
    if(numbers == 7)
    {
    printf("\n seven");
    }
    if(numbers == 8)
    {
    printf("\n eight");
    }
    if(numbers == 9)
    {
    printf("\n nine");
    }
    
    
    numbers = tempx;
    
    
    if(numbers > 9)
    {
    numbers = numbers/10;
    tempx = numbers/10*10;
    tempy = numbers - tempx;
    tempx = numbers;
    numbers = tempy;
    }
    else
    {
    goto exitroutine;
    }
    
    
    if(numbers == 1)
    {
    printf("\n ten");
    }
    if(numbers == 2)
    {
    printf("\n twenty");
    }
    if(numbers == 3)
    {
    printf("\n thirty");
    }
    if(numbers == 4)
    {
    printf("\n fourty");
    }
    if(numbers == 5)
    {
    printf("\n fifty");
    }
    if(numbers == 6)
    {
    printf("\n sixty");
    }
    if(numbers == 7)
    {
    printf("\n seventy");
    }
    if(numbers == 8)
    {
    printf("\n eighty");
    }
    if(numbers == 9)
    {
    printf("\n ninety");
    }
    
    
    numbers = tempx;
    
    
    numbers = numbers/10;
    tempx = numbers/10*10;
    tempy = numbers - tempx;
    tempx = numbers;
    numbers = tempy;
    
    
    if(tempx > 0)
    {
    goto process;
    }
    else
    {
    goto exitroutine;
    }
    
    
    process:;
    if(numbers == 0)
    {
    goto preprocess1000;
    }
    
    
    if(numbers == 1)
    {
    printf("\n one");
    }
    
    
    if(numbers == 2)
    {
    printf("\n two");
    }
    
    
    if(numbers == 3)
    {
    printf("\n three");
    }
    
    
    if(numbers == 4)
    {
    printf("\n four");
    }
    if(numbers == 5)
    {
    printf("\n five");
    }
    if(numbers == 6)
    {
    printf("\n six");
    }
    if(numbers == 7)
    {
    printf("\n seven");
    }
    if(numbers == 8)
    {
    printf("\n eight");
    }
    if(numbers == 9)
    {
    printf("\n nine");
    }
    
    
    printf(" hundred");
    
    
    preprocess1000:;
    
    
    numbers = tempx;
    numbers = numbers/10;
    
    
    if(numbers > 0)
    {
    tempx = numbers/10*10;
    tempy = numbers - tempx;
    tempx = numbers;
    numbers = tempy;
    
    
    goto process1000;
    }
    else
    {
    goto exitroutine;
    }
    
    
    process1000:;
    if(numbers == 0)
    {
    goto preprocess10000;
    }
    
    
    if(numbers == 1)
    {
    printf("\n one");
    }
    
    
    if(numbers == 2)
    {
    printf("\n two");
    }
    
    
    if(numbers == 3)
    {
    printf("\n three");
    }
    
    
    if(numbers == 4)
    {
    printf("\n four");
    }
    if(numbers == 5)
    {
    printf("\n five");
    }
    if(numbers == 6)
    {
    printf("\n six");
    }
    if(numbers == 7)
    {
    printf("\n seven");
    }
    if(numbers == 8)
    {
    printf("\n eight");
    }
    if(numbers == 9)
    {
    printf("\n nine");
    }
    
    
    printf(" thousand");
    
    
    numbers = tempx;
    
    
    preprocess10000:;
    
    
    numbers = tempx;
    numbers = numbers/10;
    
    
    if(numbers > 0)
    {
    tempx = numbers/10*10;
    tempy = numbers - tempx;
    tempx = numbers;
    numbers = tempy;
    
    
    goto process10000;
    }
    else
    {
    goto exitroutine;
    }
    
    
    process10000:;
    if(numbers == 0)
    {
    goto preprocess100000;
    }
    
    
    if(numbers == 1)
    {
    printf("\n ten");
    }
    if(numbers == 2)
    {
    printf("\n twenty");
    }
    if(numbers == 3)
    {
    printf("\n thirty");
    }
    if(numbers == 4)
    {
    printf("\n fourty");
    }
    if(numbers == 5)
    {
    printf("\n fifty");
    }
    if(numbers == 6)
    {
    printf("\n sixty");
    }
    if(numbers == 7)
    {
    printf("\n seventy");
    }
    if(numbers == 8)
    {
    printf("\n eighty");
    }
    if(numbers == 9)
    {
    printf("\n ninety");
    }
    
    
    printf(" thousand");
    
    
    numbers = tempx;
    
    
    preprocess100000:;
    
    
    numbers = tempx;
    numbers = numbers/10;
    
    
    if(numbers > 0)
    {
    tempx = numbers/10*10;
    tempy = numbers - tempx;
    tempx = numbers;
    numbers = tempy;
    
    
    goto process100000;
    }
    else
    {
    goto exitroutine;
    }
    
    
    process100000:;
    if(numbers == 0)
    {
    goto preprocess1000000;
    }
    
    
    if(numbers == 1)
    {
    printf("\n one");
    }
    
    
    if(numbers == 2)
    {
    printf("\n two");
    }
    
    
    if(numbers == 3)
    {
    printf("\n three");
    }
    
    
    if(numbers == 4)
    {
    printf("\n four");
    }
    if(numbers == 5)
    {
    printf("\n five");
    }
    if(numbers == 6)
    {
    printf("\n six");
    }
    if(numbers == 7)
    {
    printf("\n seven");
    }
    if(numbers == 8)
    {
    printf("\n eight");
    }
    if(numbers == 9)
    {
    printf("\n nine");
    }
    
    
    printf(" hundred thousand");
    
    
    numbers = tempx;
    
    
    preprocess1000000:;
    
    
    numbers = tempx;
    numbers = numbers/10;
    
    
    if(numbers > 0)
    {
    tempx = numbers/10*10;
    tempy = numbers - tempx;
    tempx = numbers;
    numbers = tempy;
    
    
    goto process1000000;
    }
    else
    {
    goto exitroutine;
    }
    
    
    process1000000:;
    if(numbers == 0)
    {
    goto exitroutine;
    /*
    goto preprocess10000000;
    */
    }
    
    
    if(numbers == 1)
    {
    printf("\n one");
    }
    
    
    if(numbers == 2)
    {
    printf("\n two");
    }
    
    
    if(numbers == 3)
    {
    printf("\n three");
    }
    
    
    if(numbers == 4)
    {
    printf("\n four");
    }
    if(numbers == 5)
    {
    printf("\n five");
    }
    if(numbers == 6)
    {
    printf("\n six");
    }
    if(numbers == 7)
    {
    printf("\n seven");
    }
    if(numbers == 8)
    {
    printf("\n eight");
    }
    if(numbers == 9)
    {
    printf("\n nine");
    }
    
    
    printf(" million");
    
    
    numbers = tempx;
    
    
    exitroutine:;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Mmm,

    A 40 line function (nicely indented, easy to read, VERY easy to extend to cover billions)

    vs

    A 500+ line horror of unindented code riddled with goto statements.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-20-2012, 07:09 AM
  2. Type text = Press button = Display text in Google?
    By Raze88 in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2008, 08:39 AM
  3. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM
  4. Better way to display lf numbers?
    By willc0de4food in forum C Programming
    Replies: 5
    Last Post: 03-09-2005, 04:29 PM
  5. Replies: 4
    Last Post: 03-03-2003, 03:52 PM

Tags for this Thread