numbers to words - criticize my code

Hello,


our teacher has given us the task of writing a program that converts numbers to words. Here is my approach:


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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include <iostream>
#include <string>

int modulo (int number);

using namespace std;

int number;


int digits_1;
int digits_2;
int digits_3;


int modulo (int number)
{
        int z;
        int digit1;
        int digit2;
        int digit3;


        digit1 = number%10;
        number = (number/10);
        digit2 = number%10;
        number = (number/10);
        digit3 = number%10;
        number = (number/10);

        z = (digit1+(10*digit2)+(100*digit3));
        return z;
}

int translation_digits (int x)

{
    int lasttwodigits = x%100;
    int firstdigit = ((x-lasttwodigits)/100);


    // output first digit

        if (firstdigit == 1)
            {
            cout << "one";
            }

        else if (firstdigit == 2)
            {
            cout << "two";
            }

        else if (firstdigit == 3)
            {
            cout << "three";
            }

        else if (firstdigit == 4)
            {
            cout << "four";
            }

        else if (firstdigit == 5)
            {
            cout << "five";
            }

        else if (firstdigit == 6)
            {
            cout << "six";
            }

        else if (firstdigit == 7)
            {
            cout << "seven";
            }

        else if (firstdigit == 8)
            {
            cout << "eight";
            }

        else if (firstdigit == 9)
            {
            cout << "nine";
            }

        if (firstdigit != 0)
            {
            cout << "hundred";
            }

//nächste zwei digits auslesen

            if (lasttwodigits == 1)
            {
            cout << "one";
            }

        else if (lasttwodigits == 2)
            {
            cout << "two";
            }

        else if (lasttwodigits == 3)
            {
            cout << "three";
            }

        else if (lasttwodigits == 4)
            {
            cout << "four";
            }

        else if (lasttwodigits == 5)
            {
            cout << "five";
            }

        else if (lasttwodigits == 6)
            {
            cout << " six";
            }

        else if (lasttwodigits == 7)
            {
            cout << "seven";
            }

        else if (lasttwodigits == 8)
            {
            cout << "eight";
            }

        else if (lasttwodigits == 9)
            {
            cout << "nine";
            }

        else if (lasttwodigits == 10)
            {
            cout << "ten";
            }

        else if (lasttwodigits == 11)
            {
            cout << "eleven";
            }

        else if (lasttwodigits == 12)
            {
            cout << "twelve";
            }

        else if (lasttwodigits == 13)
            {
            cout << "thirteen";
            }

        else if (lasttwodigits == 14)
            {
            cout << "fourteen";
            }

        else if (lasttwodigits == 15)
            {
            cout << "fifteen";
            }

        else if (lasttwodigits == 16)
            {
            cout << "sixteen";
            }

        else if (lasttwodigits == 17)
            {
            cout << "seventeen";
            }

        else if (lasttwodigits == 18)
            {
            cout << "eighteen";
            }

        else if (lasttwodigits == 19)
            {
            cout << "nineteen";
            }
        else // Zerlegung der zwei digits
            {
                int lastdigit = lasttwodigits%10;
                int middigit = ((lasttwodigits-lastdigit)/10);



//middigit


            if (middigit == 2)
            {
            cout << "twenty";
            }

        else if (middigit == 3)
            {
            cout << "thirty";
            }

        else if (middigit == 4)
            {
            cout << "fourty";
            }

        else if (middigit == 5)
            {
            cout << "fifty";
            }

        else if (middigit == 6)
            {
            cout << "sixty";
            }

        else if (middigit== 7)
            {
            cout << "seventy";
            }

        else if (middigit == 8)
            {
            cout << "eighty";
            }

        else if (middigit == 9)
            {
            cout << "ninety";
            }


//lastdigit

                    if (lastdigit == 1)
                        {
                        cout << "one";
                        }

                    else if (lastdigit == 2)
                        {
                        cout << "two";
                        }

                    else if (lastdigit == 3)
                        {
                        cout << "three";
                        }

                    else if (lastdigit == 4)
                        {
                        cout << "four";
                        }

                    else if (lastdigit == 5)
                        {
                        cout << "five";
                        }

                    else if (lastdigit == 6)
                        {
                        cout << "six";
                        }

                    else if (lastdigit == 7)
                        {
                        cout << "seven";
                        }

                    else if (lastdigit == 8)
                        {
                        cout << "eight";
                        }

                    else if (lastdigit == 9)
                        {
                        cout << "nine";
                        }
            }

}





int main()
{
    while (1)

    {


    // User input;


    cout << endl;
    cout << "Enter a number between 1 - 999 999 999!" << endl;
    cin >> number;

    // check for zero case

    if (number == 0)
        {
        cout << "You entered zero.";
        }



        //separate into blocks of 3, calling the modulo function

        digits_1 = modulo(number);

            if (number > 0)
           {
                // number = ((number - digits_1)/1000);

                digits_2 = modulo((number - digits_1)/1000);


                if (number > 0)
                {
                    // number = ((number - digits_2)/1000);

                    digits_3 = modulo( ( ( (number - digits_1)/1000) - digits_2)/1000);

                }

            }



// calling the translating function & output


            if (number > 999999)
                    {
                    translation_digits (digits_3);
                    cout << " million ";
                    }

            if (number > 999)
                    {
                    translation_digits (digits_2);
                    cout << "thousand ";
                    }

            if (number > 0)
                    {
                    translation_digits (digits_1);
                    }


    }



}



Now this code looks very messy and i'm looking for shortcuts, ie loops to shorten the code. Note: we didnt learn about arrays yet.

Thanks in advance.
I'd trade your large if-else statements for switch statements, also called case statements. I'm not sure if you've learned about these yet, but here you go.
1
2
3
4
5
6
7
8
9
10
switch (lastdigit) {
	case 1:
		cout << "one";
		break;
	case 2:
		cout << "two";
		break;
	default:
		break;
}

The code above says, "In the case that last digit is equal to 1, print 'one' and exit the switch statement. In the case that lastdigit is equal to 2, print 'two' and exit the switch statement. In the case that the value of lastdigit does not meet any of the conditions above, exit the switch statement." You might not always need default, but it is good practice to throw it in there with some error code to tell the user that something went wrong.

Best of Luck,
--NolanNJW
Check out this change I made to your translation_digits function. I reduced the amount of whitespace and took advantage of the fact that most of your blocks after your if statements were single lines of 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
int translation_digits (int x)
{
    int lasttwodigits = x%100;
    int firstdigit = ((x-lasttwodigits)/100);

    // output first digit
    if (firstdigit == 1) cout << "one";
    else if (firstdigit == 2) cout << "two";
    else if (firstdigit == 3) cout << "three";
    else if (firstdigit == 4) cout << "four";
    else if (firstdigit == 5) cout << "five";
    else if (firstdigit == 6) cout << "six";
    else if (firstdigit == 7) cout << "seven";
    else if (firstdigit == 8) cout << "eight";
    else if (firstdigit == 9) cout << "nine";

    if (firstdigit != 0) cout << "hundred";

    //nächste zwei digits auslesen
    if (lasttwodigits == 1)cout << "one";
    else if (lasttwodigits == 2) cout << "two";
    else if (lasttwodigits == 3) cout << "three";
    else if (lasttwodigits == 4) cout << "four";
    else if (lasttwodigits == 5) cout << "five";
    else if (lasttwodigits == 6) cout << " six";
    else if (lasttwodigits == 7) cout << "seven";
    else if (lasttwodigits == 8) cout << "eight";
    else if (lasttwodigits == 9) cout << "nine";
    else if (lasttwodigits == 10) cout << "ten";
    else if (lasttwodigits == 11) cout << "eleven";
    else if (lasttwodigits == 12) cout << "twelve";
    else if (lasttwodigits == 13) cout << "thirteen";
    else if (lasttwodigits == 14) cout << "fourteen";
    else if (lasttwodigits == 15) cout << "fifteen";
    else if (lasttwodigits == 16) cout << "sixteen";
    else if (lasttwodigits == 17) cout << "seventeen";
    else if (lasttwodigits == 18) cout << "eighteen";
    else if (lasttwodigits == 19) cout << "nineteen";
    else // Zerlegung der zwei digits
    {
        int lastdigit = lasttwodigits%10;
        int middigit = ((lasttwodigits-lastdigit)/10);

        //middigit
        if (middigit == 2)  cout << "twenty";
        else if (middigit == 3) cout << "thirty";
        else if (middigit == 4) cout << "fourty";
        else if (middigit == 5) cout << "fifty";
        else if (middigit == 6) cout << "sixty";
        else if (middigit == 7) cout << "seventy";
        else if (middigit == 8) cout << "eighty";
        else if (middigit == 9) cout << "ninety";

        //lastdigit
        if (lastdigit == 1) cout << "one";
        else if (lastdigit == 2) cout << "two";
        else if (lastdigit == 3) cout << "three";
        else if (lastdigit == 4) cout << "four";
        else if (lastdigit == 5) cout << "five";
        else if (lastdigit == 6) cout << "six";
        else if (lastdigit == 7) cout << "seven";
        else if (lastdigit == 8) cout << "eight";
        else if (lastdigit == 9) cout << "nine";
    }

}

I believe this makes it much easier to read. I haven't checked your logic to see if there are errors, but this code should be functionally equivalent to the function you posted originally.
@Nolan: We didnt get around to switch/case stuff yet, i believe thats up next.
@booradley: I agree, your formating makes the code way more readable.

thanks guys
Topic archived. No new replies allowed.