Request: Feedback on program that turns numbers into words

I had an exercise in a book I'm working out of that told me to write a program that turns numbers into text from -999,999 to 999,999.

I finished it and it works as expected (as far as I know), but I'm sure there are ways to turn this code into more efficient code. It feels like a lot of code for such a simple task.

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <iostream>
#include <string>

using namespace std;

int countDigits(int x);

int main()
{
    int number;

    string onesAndTeens[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
    "seventeen", "eighteen", "nineteen"};
    string tens[] = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};

    int firstNum;
    int secondNum;
    int thirdNum;
    int fourthNum;
    int fifthNum;
    int sixthNum;

    while(number != -0)
        {
            int digits = 0;
            number = 0;
            cout << "Please enter a number: (Type -0 to exit) " << endl;
            cin >> number;
            bool negative = false;

            if(number != -0)
            {
                if(number < 0)
                {
                    number = number * -1;
                    negative = true;

                }
                digits = countDigits(number);
                cout << "There are " << digits << " digits." << endl;
                if(digits == 1)
                {
                    if(negative == false)
                    {
                        cout << onesAndTeens[number] << endl;
                    }
                    else
                    {
                        cout << "negative " << onesAndTeens[number] << endl;
                    }

                }
                else if(digits == 2)
                {
                    if(negative == false)
                    {
                        if(number <= 19)
                        {
                            cout << onesAndTeens[number] << endl;
                        }
                        else{
                            firstNum = number % 10;
                            secondNum = number / 10;
                            cout << tens[secondNum - 2] << " ";
                            cout << onesAndTeens[firstNum] << endl;
                        }
                    }
                    else
                    {
                        if(number <= 19)
                        {
                            cout << onesAndTeens[number] << endl;
                        }
                        else{
                            firstNum = number % 10;
                            secondNum = number / 10;
                            cout << "negative " << tens[secondNum - 2] << " ";
                            cout << onesAndTeens[firstNum] << endl;
                        }
                    }


                }
                else if(digits == 3)
                {
                    if(negative == false)
                    {
                        firstNum = number % 10;
                        secondNum = number / 10 % 10;
                        thirdNum = number / 100;
                        cout << onesAndTeens[thirdNum] << " hundred ";
                        cout << tens[secondNum - 2] << " ";
                        cout << onesAndTeens[firstNum];
                        cout << endl;
                    }
                    else
                    {
                        firstNum = number % 10;
                        secondNum = number / 10 % 10;
                        thirdNum = number / 100;
                        cout << "negative " << onesAndTeens[thirdNum] << " hundred ";
                        cout << tens[secondNum - 2] << " ";
                        cout << onesAndTeens[firstNum];
                        cout << endl;
                    }


                }
                else if(digits == 4)
                {
                    if(negative == false)
                    {
                        firstNum = number % 10;
                        secondNum = (number / 10) % 10;
                        thirdNum = (number / 100) % 10;
                        fourthNum = number / 1000;
                        cout << onesAndTeens[fourthNum] << " thousand ";
                        cout << onesAndTeens[thirdNum] << " hundred ";
                        cout << tens[secondNum - 2] << " ";
                        cout << onesAndTeens[firstNum];
                    }
                    else
                    {
                        firstNum = number % 10;
                        secondNum = (number / 10) % 10;
                        thirdNum = (number / 100) % 10;
                        fourthNum = number / 1000;
                        cout << "negative " << onesAndTeens[fourthNum] << " thousand ";
                        cout << onesAndTeens[thirdNum] << " hundred ";
                        cout << tens[secondNum - 2] << " ";
                        cout << onesAndTeens[firstNum];
                    }

                }
                else if(digits == 5)
                {
                    if(negative == false)
                    {
                        firstNum = number % 10;
                        secondNum = (number / 10) % 10;
                        thirdNum = (number / 100) % 10;
                        fourthNum = (number / 1000) % 10;
                        fifthNum = number / 10000;
                        cout << tens[fifthNum - 2] << " ";
                        cout << onesAndTeens[fourthNum] << " thousand ";
                        cout << onesAndTeens[thirdNum] << " hundred ";
                        cout << tens[secondNum - 2] << " ";
                        cout << onesAndTeens[firstNum];
                        cout << endl;
                    }
                    else
                    {
                        firstNum = number % 10;
                        secondNum = (number / 10) % 10;
                        thirdNum = (number / 100) % 10;
                        fourthNum = (number / 1000) % 10;
                        fifthNum = number / 10000;
                        cout << "negative " << tens[fifthNum - 2] << " ";
                        cout << onesAndTeens[fourthNum] << " thousand ";
                        cout << onesAndTeens[thirdNum] << " hundred ";
                        cout << tens[secondNum - 2] << " ";
                        cout << onesAndTeens[firstNum];
                        cout << endl;
                    }

                }
                else if(digits == 6)
                {
                    if(negative == false)
                    {
                        firstNum = number % 10;
                        secondNum = (number / 10) % 10;
                        thirdNum = (number / 100) % 10;
                        fourthNum = (number / 1000) % 10;
                        fifthNum = (number / 10000) % 10;
                        sixthNum = (number / 100000);
                        cout << onesAndTeens[sixthNum] << " hundred ";
                        cout << tens[fifthNum - 2] << " ";
                        cout << onesAndTeens[fourthNum] << " thousand ";
                        cout << onesAndTeens[thirdNum] << " hundred ";
                        cout << tens[secondNum - 2] << " ";
                        cout << onesAndTeens[firstNum];
                        cout << endl;
                    }
                    else
                    {
                                                firstNum = number % 10;
                        secondNum = (number / 10) % 10;
                        thirdNum = (number / 100) % 10;
                        fourthNum = (number / 1000) % 10;
                        fifthNum = (number / 10000) % 10;
                        sixthNum = (number / 100000);
                        cout << "negative " << onesAndTeens[sixthNum] << " hundred ";
                        cout << tens[fifthNum - 2] << " ";
                        cout << onesAndTeens[fourthNum] << " thousand ";
                        cout << onesAndTeens[thirdNum] << " hundred ";
                        cout << tens[secondNum - 2] << " ";
                        cout << onesAndTeens[firstNum];
                        cout << endl;
                    }

            }
        else
        {
            cout << "Error" << endl;
        }
        cout << endl;
        }
    }
}

int countDigits(int x)
{
    int digits1 = 1;
    while(x >= 10)
    {
        x = x/10;
        digits1++;
    }

    return digits1;
}
Here's a shorter version. The trick is to work from left to right.

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
#include <iostream>
#include <string>

using namespace std;

const string onesAndTeens[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
    "seventeen", "eighteen", "nineteen"};
const string tens[] = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
    
int number_to_text (int num, int divisor, const string & name)
{   int digits = num / divisor;

    if (digits == 0)
        return num % divisor;
    if (digits > 100)        
        digits = number_to_text (digits, 100, "hundred");    
    if (digits >= 20)
    {   cout << tens[(digits/10)-2] << " ";    
        digits -= digits/10*10;
    }        
    if (digits)
        cout << onesAndTeens[digits] << " ";
    cout << name << " ";        
    return num % divisor;
}
                            
int main()
{   int num;

    while (true)
    {   cout << "Please enter a number: (Type -0 to exit) " << endl;
        cin >> num;
        
        if(num == -0)
            break;
        if (num < 0)
        {   num = num * -1;
            cout << "negative "; 
        }
        num = number_to_text (num, 1000000, "million");
        num = number_to_text (num, 1000, "thousand");
        num = number_to_text (num, 100, "hundred");
        num = number_to_text (num, 1, "");
        cout << endl;
    }
    system ("pause");        
    return 0;
}

That looks waaaay better than mine. Thanks. Im going to look over it and see what I did wrong.
Topic archived. No new replies allowed.