Help me about homework

Pages: 12
Write a program that reads a whole number of up to NINE digits and prints it in words. For example, the input 13247 ought to produce thirteen thousand two hundred forty seven. Just only use #include <iostream>, use SWITCH statements. Thank you so much.
you forgot to post your attempt and tell us why it is not working?
If you don't know where to start
* begin by doing yourself on paper
* write down the steps you used to do it

That'll give you an algorithm you can use.

Try to implement that algorithm in C++. If you're stuck, we can help, but we shouldn't do it for you; it's bad for everyone.
That question has been asked before on these forums - so if you search you'll find it.
yea but I don't think we did it with a bogus switch. There is always some 'do it a screwy way' addendum to prevent copying old ones.
1
2
3
4
5
6
7
8
9
10
switch (number)
{
    case 1:
        cout << "one";
    case 2:
        cout << "two";
    // ... < left as exercise for the reader >
    case 999999999:
        cout << "nine hundred ninety nine million nine hundred ninety nine thousand nine hundred ninety nine";
}
@ganado - Haha.
I'd be surprised if there is a compiler out there that can handle more than 65,535 branches.
BTW, you forgot the break statements.
How embarrassing :( Guess I failed whatever class OP has.
Write a program that reads a whole number of up to NINE digits and prints it in words.

Done!

What? You want me to show you what I did -- for free -- without even a hint you tried first yourself?
Hi Ganado

Big Lol. I am going to help too.

Hi sonnguyen just make program divide the input number by 10 and write "ten" n times. Then add the remainder from 1 to 9.

input number 4 just write "four"
input number 11 just write "ten one"
input number 23 just write "ten ten three"
input number 106 just write "ten ten ten ten ten ten ten ten ten ten six"
...
input number 999999999 just write "ten ... ... ... nine"

Last edited on
reminds me of an old puzzler
O T T F F S S E
what is next in the pattern...

I try code but Unexpected results. Can you help me fix it? Thank you.
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
368
369
370
371
372
373
374
375
#include <iostream>

using namespace std;

//Read number one digit
void one(int x)
{
    switch(x){
        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;
    }
}

//Read ten number
void ten(int x)
{
    switch(x){
        case 1:
            cout<<"ten";
            break;
        case 2:
            cout<<"twenty";
            break;
        case 3:
            cout<<"thirty";
            break;
        case 4:
            cout<<"forty";
            break;
        case 5:
            cout<<"fifrty";
            break;
        case 6:
            cout<<"sixty";
            break;
        case 7:
            cout<<"seventy";
            break;
        case 8:
            cout<<"eighty";
            break;
        case 9:
            cout<<"ninety";
            break;
    }
}

//Read number hundred
void hundred(int x)
{
    switch(x){
        case 1:
            cout<<"one hundred";
            break;
        case 2:
            cout<<"two hundred";
            break;
        case 3:
            cout<<"three hundred";
            break;
        case 4:
            cout<<"four hundred";
            break;
        case 5:
            cout<<"five hundred";
            break;
        case 6:
            cout<<"six hundred";
            break;
        case 7:
            cout<<"seven hundred";
            break;
        case 8:
            cout<<"eight hundred";
            break;
        case 9:
            cout<<"nine hundred";
            break;
    }
}

//Read a thousand number
void thousand(int x)
{
    switch(x){
        case 1:
            cout<<"one thousand";
            break;
        case 2:
            cout<<"two thousand";
            break;
        case 3:
            cout<<"three thousand";
            break;
        case 4:
            cout<<"four thousand";
            break;
        case 5:
            cout<<"five thousand";
            break;
        case 6:
            cout<<"six thousand";
            break;
        case 7:
            cout<<"seven thousand";
            break;
        case 8:
            cout<<"eight thousand";
            break;
        case 9:
            cout<<"nine thousand";
            break;
    }
}

//Read a thousand number
void tensthousand(int x)
{
    switch(x){
        case 1:
            cout<<"one thousand";
            break;
        case 2:
            cout<<"two thousand";
            break;
        case 3:
            cout<<"three thousand";
            break;
        case 4:
            cout<<"four thousand";
            break;
        case 5:
            cout<<"five thousand";
            break;
        case 6:
            cout<<"six thousand";
            break;
        case 7:
            cout<<"seven thousand";
            break;
        case 8:
            cout<<"eight thousand";
            break;
        case 9:
            cout<<"nine thousand";
            break;
    }
}

//Read a number tens of thousands
void tenshundredthousands(int x)
{
    switch(x){
        case 1:
            cout<<"ten thousand";
            break;
        case 2:
            cout<<"twenty thousand";
            break;
        case 3:
            cout<<"thirty thousand";
            break;
        case 4:
            cout<<"fourty thousand";
            break;
        case 5:
            cout<<"fifty thousand";
            break;
        case 6:
            cout<<"sixty thousand";
            break;
        case 7:
            cout<<"seventy thousand";
            break;
        case 8:
            cout<<"eighty thousand";
            break;
        case 9:
            cout<<"ninety thousand";
            break;
    }
}

//Read a number million
void million(int x)
{
    switch(x){
        case 1:
            cout<<"one million";
            break;
        case 2:
            cout<<"two million";
            break;
        case 3:
            cout<<"three million";
            break;
        case 4:
            cout<<"four million";
            break;
        case 5:
            cout<<"five million";
            break;
        case 6:
            cout<<"six million";
            break;
        case 7:
            cout<<"seven million";
            break;
        case 8:
            cout<<"eight million";
            break;
        case 9:
            cout<<"nine million";
            break;
    }
}

//Read a number ten million
void tenmillion(int x)
{
    switch(x){
        case 1:
            cout<<"ten million";
            break;
        case 2:
            cout<<"twenty million";
            break;
        case 3:
            cout<<"thirty million";
            break;
        case 4:
            cout<<"fourty million";
            break;
        case 5:
            cout<<"fifty million";
            break;
        case 6:
            cout<<"sixty million";
            break;
        case 7:
            cout<<"seventy million";
            break;
        case 8:
            cout<<"eighty million";
            break;
        case 9:
            cout<<"ninety million";
            break;
    }
}

//Read a number hundred  million
void hundredmillion(int x)
{
    switch(x){
        case 1:
            cout<<"one hundred million";
            break;
        case 2:
            cout<<"two hundred million";
            break;
        case 3:
            cout<<"three hundred million";
            break;
        case 4:
            cout<<"four hundred million";
            break;
        case 5:
            cout<<"five hundred million";
            break;
        case 6:
            cout<<"six hundred million";
            break;
        case 7:
            cout<<"seven hundred million";
            break;
        case 8:
            cout<<"eight hundred million";
            break;
        case 9:
            cout<<"nine hundred million";
            break;
    }
}

int main()
{
    int n, a, b, c, d, e, f, g, h, t;
    cout<<"Input a 9 digit positive: ";
    cin>>n;
    
    //Separate numbers in millions, hundreds of thousands, tens of thousands, thousands, hundreds, tens, and units
    a = n%10;
    b = (n/10)%10;
    c = (n/100)%10;
    d = (n/1000)%10;
    e = (n/10000)%10;
    f = (n/100000)%10;
    g = (n/1000000)%10;
    h = (n/10000000)%10;
    t = (n/100000000)%10;

    //Read the numbers just split above
    if(n<10)
    {
        one(a); 
        cout<<a<<endl;
    }
    else if(n>=10 && n<100)
    {
        ten(b); 
        cout<<b<<endl;
    }    
    else if(n>=100 && n<1000)
    {
        hundred(c);
        cout<<c<<endl;
    }
    else if(n>=1000 && n<10000)
    {
        thousand(d);
        cout<<d<<endl;
    }
    else if(n>=10000 && n<100000)
    {
        tensthousand(f);
        cout<<f<<endl;
    }
    else if(n>=100000 && n<1000000)
    {
        tenshundredthousands(g);
        cout<<g<<endl;
    }
    else if(n>=1000000 && n<10000000)
    {
        million(h);
        cout<<h<<endl;
    }
    else if(n>=10000000 && n<100000000)
    {
        tenmillion(f);
        cout<<f<<endl;
    }
    else
    {
        hundredmillion(t);
        cout<<t<<endl;
    }
    return 0;
}
Last edited on
Can you be more specific than just saying "unexpected results"?

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Do you see a pattern in your functions?
You can shorten your code significantly.
Consider:
1
2
3
4
void million (int x)
{   one(x);  //  prints one-nine
    cout << " million";
}


Don't forget that eleven through nineteen require special handling.
Last edited on
Yes, I edit the topic. I will fix it.
Last edited on
You can help me how to handle eleven through nineteen. Thank you
there is no way to do 11-19 any better than one at a time.
one way... you can make a table of strings such that table[11] returns "eleven" and so on.
after 20 the patterns are nice and easy to work with, though. its typically 0-100 of a thing, eg "forty two thousand"

or since you are doing switches, a switch.
Last edited on
This should get you started:

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

using namespace std;

void ones(int x)
{
	switch (x) {
	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;
	}
	cout << " ";
}

void tens(int x)
{
	switch (x) {
	case 1:
		cout << "ten";
		break;
	case 2:
		cout << "twenty";
		break;
	case 3:
		cout << "thirty";
		break;
	case 4:
		cout << "forty";
		break;
	case 5:
		cout << "fifrty";
		break;
	case 6:
		cout << "sixty";
		break;
	case 7:
		cout << "seventy";
		break;
	case 8:
		cout << "eighty";
		break;
	case 9:
		cout << "ninety";
		break;
	}
	cout << " ";
}

void hundreds(int x)
{
	ones(x);
	cout << "hundred ";
}

void format(int n)
{
	int a, b, c, d, e, f, g, h, t;

	//Separate numbers in millions, hundreds of thousands, tens of thousands, thousands, hundreds, tens, and units
	a = n % 10;					//	1
	b = (n / 10) % 10;			//	10
	c = (n / 100) % 10;			//	100
	d = (n / 1000) % 10;		//	1,000	
	e = (n / 10000) % 10;		//	10,000
	f = (n / 100000) % 10;		//	100,000
	g = (n / 1000000) % 10;		//	1,000,000
	h = (n / 10000000) % 10;	//	10,000,000
	t = (n / 100000000) % 10;	//	100,000,000
	
	if (t || h || g)
	{
		if (t)
			hundreds(t);
		if (h)
			tens(h);
		if (g)
			ones(g);
		cout << "millions ";
	}
	if (f || e || d)
	{
		if (f)
			hundreds(f);
		if (e)
			tens(e);
		if (d)
			ones(d);
		cout << "thousands ";
	}
	if (c)
		hundreds(c);
	if (b)
		tens(b);
	if (a)
		ones(a);
	cout << endl;
}

int main()
{
	int n;

	do
	{
		cout << "Input a 9 digit positive (0 to stop): ";
		cin >> n;
		if (n)
			format(n);
	} while (n);
	return 0;
}


I leave you to deal with 11-19 correctly.
Yes, thank you so much. I fix my code but have a problem when my code output. I input 6 --> hundred million hundred thousand hundred six. I do not how to remove hundred million hundred thousand hundred six. Can you help me?

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
90
91
92
93
94
95
#include <iostream>
using namespace std;

//Break the number into three three-digit numbers
void breakapart(int n, int &a, int &b, int &c)
{   
    c = n%1000;
    n = n/1000;
    
    b = n%1000;
    n = n/1000;
    
    a = n%1000;
    n = n/1000;
}

void writeSingle(int digit)
{
    switch(digit)
    {
        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;
    }
}

void writeTens(int tensD, int onesD)
{
    switch(tensD)
    {
        case 1: 
        switch(onesD)
        {
            case 0: cout<<"ten "; break;
            case 1: cout<<"eleven "; break;
            case 2: cout<<"twelve "; break;
            case 3: cout<<"thirteen "; break;
            case 4: cout<<"fouteen "; break;
            case 5: cout<<"fiftenn "; break;
            case 6: cout<<"sixteen "; break;
            case 7: cout<<"seventeen "; break;
            case 8: cout<<"eighteen "; break;
            case 9: cout<<"nineteen "; break;
        }
        break;
        case 2: cout<<"twenty "; break;
        case 3: cout<<"thirty "; break;
        case 4: cout<<"fourty "; break;
        case 5: cout<<"fifty "; break;
        case 6: cout<<"sixty "; break;
        case 7: cout<<"seventy "; break;
        case 8: cout<<"eighty "; break;
        case 9: cout<<"ninty "; break;
    }
}

void writeNum(int n)
{
    // break number into single digit
    int one, two, three;
    
    three = n%10;
    n = n/10;
    
    two = n%10;
    n = n/10;
    
    one = n;
    
    writeSingle(one);
    cout<<"hundred ";
    writeTens(two, three);
    writeSingle(three);
}

int main()
{
    int num, first, second, third;
    cout<<"Input a 9 digit number ";
    cin>>num;
    // Break the number into three three-digit numbers
    breakapart(num,first,second,third);
    writeNum(first);
    cout<<"million ";
    writeNum(second);
    cout<<"thousand ";
    writeNum(third);
    return 0;
}
Last edited on
Hi sonnguyen

hint:

if my number has one digit start calculations for one digit
if my number has two digits start calculations for two digits
...
the number of digits in an integer is log10()+1 (in decimal format).
Last edited on
Pages: 12