Turn numbers into English text......URGENT!

I have been given a practice problem to implement the source code which turn numbers into English text.
I would appreciate if someone could explain this to me with the source code.
Urgent help needed.

Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <string>
int main(){
    unsigned long long nr;std::string number="";short order=1;
    std::cin>>nr;
    if(!nr) {number="zero "; std::cout<<number; return nr;}
    if(nr>=18446744073709551615||(long long)nr<0)
    {std::cout<<"The number must be between 0 and 18,446,744,073,709,551,615\n"; return 0;}
    beggining:
    if(nr%100==1||nr%10==1&&nr%100!=11) number="one "+number;
    if(nr%100==2||nr%10==2&&nr%100!=12) number="two "+number;
    if(nr%100==3||nr%10==3&&nr%100!=13) number="three "+number;
    if(nr%100==4||nr%10==4&&nr%100!=14) number="four "+number;
    if(nr%100==5||nr%10==5&&nr%100!=15) number="five "+number;
    if(nr%100==6||nr%10==6&&nr%100!=16) number="six "+number;
    if(nr%100==7||nr%10==7&&nr%100!=17) number="seven "+number;
    if(nr%100==8||nr%10==8&&nr%100!=18) number="eight "+number;
    if(nr%100==9||nr%10==9&&nr%100!=19) number="nine "+number;
    if(nr%100==10) number="ten "+number;
    if(nr%100==11) number="eleven "+number;
    if(nr%100==12) number="twelve "+number;
    if(nr%100==13) number="thirteen "+number;
    if(nr%100==14) number="fourteen "+number;
    if(nr%100==15) number="fifteen "+number;
    if(nr%100==16) number="sixteen "+number;
    if(nr%100==17) number="seventeen "+number;
    if(nr%100==18) number="eighteen "+number;
    if(nr%100==19) number="nineteen "+number;
    nr/=10;
    if(nr%10==2) number="twenty "+number;
    if(nr%10==3) number="thirty "+number;
    if(nr%10==4) number="fourty "+number;
    if(nr%10==5) number="fifty "+number;
    if(nr%10==6) number="sixty "+number;
    if(nr%10==7) number="seventy "+number;
    if(nr%10==8) number="eighty "+number;
    if(nr%10==9) number="ninety "+number;
    nr/=10;
    if(nr%10==1) number="one hundred "+number;
    if(nr%10==2) number="two hundred "+number;
    if(nr%10==3) number="three hundred "+number;
    if(nr%10==4) number="four hundred "+number;
    if(nr%10==5) number="five hundred "+number;
    if(nr%10==6) number="six hundred "+number;
    if(nr%10==7) number="seven hundred "+number;
    if(nr%10==8) number="eight hundred "+number;
    if(nr%10==9) number="nine hundred "+number;
    if(nr/10!=0)
    {
    nr/=10;
    if(order==1)
    {
    if(nr%100!=0||nr==100) {number="thousand "+number;}
    ++order;goto beggining;
    }
    if(order==2)
    {
    if(nr%100!=0||nr==100) {number="million "+number;}
    ++order;goto beggining;
    }
    if(order==3)
    {
    if(nr%100!=0||nr==100) {number="billion "+number;}
    ++order;goto beggining;
    }
    if(order==4)
    {
    if(nr%100!=0||nr==100) {number="trillion "+number;}
    ++order;goto beggining;
    }
    if(order==5)
    {
    if(nr%100!=0||nr==100) {number="quadrillion "+number;}
    ++order;goto beggining;
    }
    if(order==6)
    {
    if(nr%100!=0||nr==100) {number="quintillion "+number;}
    ++order;goto beggining;
    }
    if(order==7)
    {
    if(nr%100!=0||nr==100) {number="sextillion "+number;}
    ++order;goto beggining;
    }
    }
    std::cout<<number;
    return 0;}


EDIT:*UPDATED*
Last edited on
That code is a horrible. Horrible, horrible, horrible.

@OP - No.
Mats you are a real bitch
Last edited on
if you take the string version of the input number then the length of that string would give you your 'it's in the 10's' or 'its in the 100s' condition etc.

For example if the user entered "2622", the size of that string is 4, so you know straight away it's in the thousands.
Last edited on
Building on what mutexe just said, you could make a function which takes a character as argument and returns the word of that digit:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
string digitName(char digit){
    switch(digit){
    case '1':
        return "one";
    case '2':
        return "two";
    case '3':
        return "three";
    case '4':
        return "four";
    case '5':
        return "five";
    case '6':
        return "six";
    case '7':
        return "seven";
    case '8':
        return "eight";
    case '9':
        return "nine";
    default:
        return "";
    }
}


And another function could return the word of the number family based on the amount of digits in the number:

1
2
3
4
5
6
7
8
9
10
string nbFamilyName(unsigned int nbOfDigits){
    switch(nbOfDigits){
    case 2:
        return /* someothefunction */
    case 3:
        return "hundred";
    case 4:
        return "thousand"
    /* ... */
}


But you also have to deal with numbers with odd names like those from 10 to 20, then there are 30, 40, 5, 60, 70, 80 and 90 (that could be another function). It's not an easy task.
Last edited on
Another variant using functionS :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <string>
#include <iostream>

using namespace std;

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

string tens_to_text[] = { "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

string power_to_text[] = { "", "thousand", "million", "billion" };

string padIfNeeded (string ans)
{
        if ( ans == "" )
        {
                return "";
        }
        return " " + ans;
}

string translateHundred (int hundred_chunk)
{
        // handle special cases in the teens
        if ( hundred_chunk < 20 ) {
                return num_to_text[ hundred_chunk ];
        }
        int tens = hundred_chunk / 10;
        int ones = hundred_chunk % 10;
        return tens_to_text[ tens ] + padIfNeeded( num_to_text[ ones ] );
}


string translateThousand (int thousand_chunk)
{
        if ( thousand_chunk < 100 )
        {
                return translateHundred( thousand_chunk );
        }
        else
        {
                int hundreds = thousand_chunk / 100;
                int hundred_chunk = thousand_chunk % 100;
                return num_to_text[ hundreds ] + " hundred" + padIfNeeded( translateHundred( hundred_chunk ) );
        }
}
                
                
int main()
{
        int n;
        cin >> n;
        string number;
        bool is_negative = false;
        if ( n < 0 ) 
        {
                is_negative = true;
                n *= -1;
        }

        int chunk_count = 0;
        while ( n > 0 )
        {
                if ( n % 1000 != 0 ) {
                        number = translateThousand( n % 1000 ) + padIfNeeded( power_to_text[ chunk_count ] + padIfNeeded( number ) );
                }
                n /= 1000;
                chunk_count++;
        }       
        if ( number == "" )
        {
                number = "zero";
        }
        if ( is_negative )
        {
                number = "negative " + number;
        }
        cout << number << endl;
} 
Last edited on
Topic archived. No new replies allowed.