Converting number into words jumping into c++

Hi everyone. The problem is:
"implement source to convert number into words. eg: if input is 454, result is four hundred fifty four.
I spent my entire noon and afternoon in this single problem and finally came up with the this code. Yet I feel that this is too long. Can anyone suggest a more concise code for the same problem.(please note that i only know concept of if,loop, and function.4 days into c++) My code is:
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
  //works for only upto millionth number

#include <iostream>
using namespace std;
void millionth_function(int x);
void thousandth_function(int x);
void no_th_function(int x);
void millionth_3_digits(int y);
void thousandth_3_digits(int x);
void no_th_3_digits(int x);
void hundredth_number(int x);
void tens_number(int x);
int main()
{
    int number;
    cout<< "enter number: ";
    cin>>number;
    millionth_3_digits(number);
    thousandth_3_digits(number);
    no_th_function(number);
}
void millionth_3_digits(int x)
{
    int three_digits=((x%1000000000)/1000000);
    hundredth_number(three_digits);
    tens_number(three_digits);
    if(three_digits!=0)
    {
        cout<<" million ";
    }
}
void thousandth_3_digits  (int x)
{
    int three_digits=((x%1000000)/1000);
    hundredth_number(three_digits);
    tens_number(three_digits);
    if (three_digits!=0)
    {
        cout<<" thousand";
    }
}
void no_th_function(int x)
{
    int three_digits=(x%1000);
    hundredth_number(three_digits);
    tens_number(three_digits);
    }
void hundredth_number(int x)
{
    int first_number=x/100;
    if (first_number==1)
    {
        cout<<" One hundred ";
    }
    else if (first_number==2)
    {
        cout<<" Two hundred ";
    }
    else if (first_number==3)
    {
        cout<<" Three hundred ";
    }
    else if (first_number==4)
    {
        cout<<" Four hundred ";
    }
    else if (first_number==5)
    {
        cout<<" Five hundred ";
    }
    else if (first_number==6)
    {
        cout<<" Six hundred ";
    }
    else if (first_number==7)
    {
        cout<<" Seven hundred ";
    }
    else if (first_number==8)
    {
        cout<<" Eight hundred ";
    }
    else if (first_number==9)
    {
        cout<<" Nine hundred ";
    }
    }
void tens_number(int x)
{
    int last_two_number=x%100;
    if(last_two_number<20)
    {
        if (last_two_number==1)
        {
            cout<<" One ";
        }
        if (last_two_number==2)
        {
            cout<<" Two ";
        }
        if (last_two_number==3)
        {
            cout<<" Three ";
        }
        if (last_two_number==4)
        {
            cout<<" Four ";
        }
        if (last_two_number==5)
        {
            cout<<" Five ";
        }
        if (last_two_number==6)
        {
            cout<<" Six ";
        }
        if (last_two_number==7)
        {
            cout<<"Seven ";
        }
        if (last_two_number==8)
        {
            cout<<" Eight ";
        }
        if (last_two_number==9)
        {
            cout<<" Nine ";
        }
        if (last_two_number==10)
        {
            cout<<" Ten ";
        }
        if (last_two_number==11)
        {
            cout<<" Eleven ";
        }
        if (last_two_number==12)
        {
            cout<<" Twelve ";
        }
        if (last_two_number==13)
        {
            cout<<" Thirteen ";
        }
        if (last_two_number==14)
        {
            cout<<" Fourteen ";
        }
        if (last_two_number==15)
        {
            cout<<" Fifteen ";
        }
        if (last_two_number==16)
        {
            cout<<" Sixteen ";
        }
        if (last_two_number==17)
        {
            cout<<" Seventeen ";
        }
        if (last_two_number==18)
        {
            cout<<" Eighteen ";
        }
        if (last_two_number==19)
        {
            cout<<" Ninteen ";
        }
        }
        if (last_two_number>19)
        {
            int tens_digit=last_two_number/10;
            if (tens_digit==2)
            {
                cout<<" Twenty ";
            }
            if (tens_digit==3)
            {
                cout<<" Thirty ";
            }
            if (tens_digit==4)
            {
                cout<<" Forty ";
            }
            if (tens_digit==5)
            {
                cout<<" Fifty ";
            }
            if (tens_digit==6)
            {
                cout<<" Sixty ";
            }
            if (tens_digit==7)
            {
                cout<<" Seventy ";
            }
            if (tens_digit==8)
            {
                cout<<" Eighty ";
            }
            if (tens_digit==9)
            {
                cout<<" Ninty ";
            }
            int units_digit=last_two_number%10;
            if (units_digit==1)
            {
                cout<<" One ";
            }
            if (units_digit==2)
            {
                cout<<" Two ";
            }
            if (units_digit==3)
            {
                cout<<" Three ";
            }
            if (units_digit==4)
            {
                cout<<" Four ";
            }
            if (units_digit==5)
            {
                cout<<" Five ";
            }
            if (units_digit==6)
            {
                cout<<" Six ";
            }
            if (units_digit==7)
            {
                cout<<" Seven ";
            }
            if (units_digit==8)
            {
                cout<<" Eight ";
            }
            if (units_digit==9)
            {
                cout<<" Nine ";
            }
            }
}




Any suggestion would be highly appreciated!
there are multiple spaces in your program output, keep only one space.
and,
"123" >> "one hundred twenty-three"
notice where hyphen is used.
Last edited on
notice where hyphen is used.

The hyphen is not necessary.


[edit]
Those ifs can be turned into switches.

Also, when you get to arrays, consider rewriting using an array lookup. Here's a link to my old numbers-to-words challenge:
http://www.cplusplus.com/forum/lounge/74394/
(for more ideas in the future)

Hope this helps.
Last edited on
hmm, very big codes. can it be done less than 100 lines!
@koopey,
1
2
3
4
5
6
7
8
9
10
11
12
if (tens_digit==7)
            {
                cout<<" Seventy ";
            }
if (tens_digit==8)
            {
                cout<<" Eighty ";
            }
if (tens_digit==9)
            {
                cout<<" Ninty ";
            }


if you write following way, it would be much shorter and easier to read:
1
2
3
if (tens_digit==7) cout<<" Seventy ";
if (tens_digit==8) cout<<" Eighty ";
if (tens_digit==9) cout<<" Ninty ";
anup30 wrote:
hmm, very big codes. can it be done less than 100 lines!

Are you being smug about something, or just making an observation about the code in my link?

(Because you'll notice that all the code in my link does it in less than 100 lines, discounting commentary and the requirement of making a fully functioning program. Not to mention that everyone's examples are pretty robust about handling fairly large numbers.)


 Re: compressing vertical whitespace:
if you write following way, it would be much shorter and easier to read:
1
2
3
if (tens_digit==7) cout<<" Seventy ";
if (tens_digit==8) cout<<" Eighty ";
if (tens_digit==9) cout<<" Ninty ";
+ 1

Using arrays to look stuff up would also do that, but it is still a good comment if not using arrays.
Thanks guys for the responses. @duoas I will try and use arrays when I get there. @anup30. just finished reading switches. Thanks again!
@duoas, latter. in fact previously i only glanced there, and it seemed little confusing seeing 100+ lines from experienceds (while OPs code will be under 100 if he formats as suggested, or even shorter if arrays are used.)
you are right, i didn't notice that nearly half of their length were comments.
Topic archived. No new replies allowed.