Turning numbers into text?

I just want some pointers in the right direction. And here is the practice problem from the book:

"Implement the source code that turns numbers into English text for numbers between -2 billion and 2 billion."

I haven't learned about arrays or switches yet, but I hear those help. Still, I'm wondering if there's a way to do them without. I've tried breaking the number down into chunks using "%" but then I don't know where to go from there. I think I'm mostly stuck on trying to find the most efficient way of turning the numbers into words. The only way I can think of is using a bunch of if statements.

1
2
3
4
5
6
  if (integer == 1)
  {
     cout << "One ";
  }

//And then doing this for pretty much every single number 
Try sstream aka string stream and to do what you are trying would take forever to count to two billion lol you would have to type each world manually unless using loops with multiple arrays/vectors
Last edited on
What does string stream do?

Yeah at first I thought maybe by splitting them into chunks I would really only have to type out several if statements that I could use over and over again. For example, the numbers between 1-20 I could use repeatedly for different chunks. But then my mind got boggled as to where I would store these statements and how I could call them back again.
sstream streams a string to an int/double or vice versa google std::sstring I would send link but on phone
Right, so I tried tackling it without sstream, and this is the code I have so far. I know it looks horrendous, but I tried. It seems that no matter what number I input, the computer coughs back up the same thing: " 11 Thousand, 0 Million, (and then a bunch of random numbers)".

If anyone could help me, that person would be showered with good mojo from yours truly.

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
int number;

int billions();

int millions();
int millionsChunkhundreds();
int millionsChunktens();
int millionsChunklessthan20();

int thousands();
int thousandsChunkhundreds();
int thousandsChunktens();
int thousandsChunklessthan20();

int hundreds();
int hundredsChunkhundreds();
int hundredsChunktens();
int hundredsChunklessthan20();

int main()
{
    cout <<"Hello, please enter a number between -2 billion and 2 billion. \n";
    cin >> number;

    if (number < -2000000000 || number > 2000000000)
    {
        cout << "ERROR.";
    }

    else
    {
        cout << billions() << millions() << thousands() << hundreds();
    }

}

//Billions

int billions()
{
    if (number > 999999999)
    {
        int billionsChunk = number / 1000000000;

        if (billionsChunk == 1)
        {
            cout << "One billion, ";
        }

        else if (billionsChunk == 2)
        {
            cout << "Two billion, ";
        }
    }

    else
    {
        cout << " ";
    }

    return 0;
}

//Millions

int millionsChunk = (number % 1000000000) * 100;
int millions()
{

    if (millionsChunk > 99)
    {
        cout << millionsChunkhundreds() <<" hundred ";

        if (millionsChunktens() > 19)
        {
            cout << millionsChunktens() <<" million, ";
        }

        else
        {
            cout << millionsChunklessthan20() <<" million, ";
        }
    }

    else if (millionsChunk < 100)
    {
        if (millionsChunktens() > 19)
        {
            cout << millionsChunktens <<" million, ";
        }

        else
        {
            cout << millionsChunklessthan20() <<" million, ";
        }
    }

    else if (millionsChunk == 0)
    {
        cout << " ";
    }
}

int millionsChunkhundreds()
{
    int mCh = millionsChunk / 100;

    if (mCh == 9)
    {
        cout << "Nine ";
    }

    else if (mCh == 8)
    {
        cout << "Eight ";
    }

    else if (mCh == 7)
    {
        cout << "Seven ";
    }

    else if (mCh == 6)
    {
        cout << "Six ";
    }

    else if (mCh == 5)
    {
        cout << "Five ";
    }

    else if (mCh == 4)
    {
        cout << "Four ";
    }

    else if (mCh == 3)
    {
        cout << "Three ";
    }

    else if (mCh == 2)
    {
        cout << "Two ";
    }

    else if (mCh == 1)
    {
        cout << "One ";
    }

}

int millionsChunktens()
{
    int mCtF = (millionsChunk % 100) * 10;

    if (mCtF == 2)
    {
        cout << "Twenty ";
    }

    else if (mCtF == 3)
    {
        cout << "Thirty ";
    }

    else if (mCtF == 4)
    {
        cout << "Forty ";
    }

    else if (mCtF == 5)
    {
        cout << "Fifty ";
    }

    else if (mCtF == 6)
    {
        cout << "Sixty ";
    }

    else if (mCtF == 7)
    {
        cout << "Seventy ";
    }

    else if (mCtF == 8)
    {
        cout << "Eighty ";
    }

    else if (mCtF == 9)
    {
        cout << "Ninety ";
    }

    int mCtS = millionsChunk % 10;

    if (mCtS == 1)
    {
        cout << "One ";
    }

    else if (mCtS == 2)
    {
        cout << "Two ";
    }

    else if (mCtS == 3)
    {
        cout << "Three ";
    }

    else if (mCtS == 4)
    {
        cout << "Four ";
    }

    else if (mCtS == 5)
    {
        cout << "Five ";
    }

    else if (mCtS == 6)
    {
        cout << "Six ";
    }

    else if (mCtS == 7)
    {
        cout << "Seven ";
    }

    else if (mCtS == 8)
    {
        cout << "Eight ";
    }

    else if (mCtS == 9)
    {
        cout << "Nine ";
    }


}

int millionsChunklessthan20()
{
    int mCtF = (millionsChunk % 100) * 10;

    if (mCtF == 19)
    {
        cout << "Nineteen ";
    }

    else if (mCtF == 18)
    {
        cout << "Eighteen ";
    }

    else if (mCtF == 17)
    {
        cout << "Seventeen ";
    }

    else if (mCtF == 16)
    {
        cout << "Sixteen ";
    }

    else if (mCtF == 15)
    {
        cout << "Fifteen ";
    }

    else if (mCtF == 14)
    {
        cout << "Fourteen ";
    }

    else if (mCtF == 13)
    {
        cout << "Thirteen ";
    }

    else if (mCtF == 12)
    {
        cout << "Twelve ";
    }

    else if (mCtF == 11)
    {
        cout << "Eleven ";
    }

    else if (mCtF == 10)
    {
        cout << "Ten ";
    }

    else if (mCtF == 9)
    {
        cout << "Nine ";
    }

    else if (mCtF == 8)
    {
        cout << "Eight ";
    }

    else if (mCtF == 7)
    {
        cout << "Seven ";
    }

    else if (mCtF == 6)
    {
        cout << "Six ";
    }

    else if (mCtF == 5)
    {
        cout << "Five ";
    }

    else if (mCtF == 4)
    {
        cout << "Four ";
    }

    else if (mCtF == 3)
    {
        cout << "Three ";
    }

    else if (mCtF == 2)
    {
        cout << "Two ";
    }

    else if (mCtF == 1)
    {
        cout << "One ";
    }
}


The thousandsChunk and the hundredsChunk is the same as the millionsChunk, but modified appropriately.
1
2
int number = 123543;
std::string s = std::to_string(number);


returns a string of the number.
See http://en.cppreference.com/w/cpp/string/basic_string/to_string
Okay firstly put int number inside of the main function not in the global score and change a) make all the functions void if you are having them cout or b) return a string and not 0;
try something like this
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
void outputFunction( const signed int a ) //you aren't modify the number so 
//it is read only ( const) and it can be negative or positive so it's signed
{
     std::string str = std::string();
     if( a == 1 )
     {
            str = "One";
     }
     std::cout << "a: " << str << std::endl;
}

const std::string returnFunction( const signed int a)
{
     std::string str = std::string();
     if( a == 1 )
     {
          str = "One";
     }
     return( str );
}

int main( void )
{
     signed int number = 0;
     std::cout << "Please enter the number 1\n> " << std::flush;
     std::cin >> number;
     outputFunction( number );
     std::cout << returnFunction( number ) << std::endl;
}
PLease enter the number 1
> 1
One
One


secondly, you should invest into loops and arrays maybe?
Last edited on
yes arrays of strings to store string literals like "zero", "one", "two", "ten", "fifteen", ..., "twenty", "thirty", ..., "hundred", "thousand", "million", "billion", ...
Use those arrays as lookup tables.

write 2 functions as helper function for your spelling function: numSpLt100() - number spelling less than 100, numSpLt1000() - number spelling less than 1000


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

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

std::string numSpLt100(const std::string& n) //you can easily change integer (ex: 123) to string ("123")
{
    std::string result = "";
    if (n.length() == 1) {  //length of string n == digit count
        result = NUMSPL1[n[0] - '0']; //don't have to use 10 if else for n in range [0-9]
    } else if (n.length() == 2) {  //2-digit numbers
        if (n[0] == '1') {
            result = NUMSPL1[n[1] - '0' + 10]; //don't have to use another 10 if else for n in range [10-19]
        } else {  //n in range [20-99]
            result = NUMSPL2[n[0] - '2'];
            if (n[1] != '0') result += " " + NUMSPL1[n[1] - '0'];
        }
    }
    //for other lengths 0,3,4,5,... do nothing
    return result;
}

int main()
{
    std::cout << numSpLt100("0") << std::endl;
    std::cout << numSpLt100("7") << std::endl;
    std::cout << numSpLt100("16") << std::endl;
    std::cout << numSpLt100("24") << std::endl;
    std::cout << numSpLt100("50") << std::endl;
    std::cout << numSpLt100("99") << std::endl;
}

//forgot to set std:: for cout and endl...

output:
zero
seven
sixteen
twenty four
fifty
ninety nine


full answer's here http://ideone.com/w4yKSt
Last edited on
closed account (D80DSL3A)
Nice job tntxtnt. Too bad you missed the lounge challenge.
Several more solutions are there:
http://www.cplusplus.com/forum/lounge/74394/
i have like this in my program
Okay so it looks like the basic idea is to convert the number into a string, then check how many digits there are, and then depending on that, have it look up the corresponding array to have it change to the actual word. Am I right in this conclusion? Also, how would you factor in user input?

Also, in response to giblet, don't I want int number to be a global variable? All the other functions rely on it as well.
Last edited on
Save yourself some time and instead of coding one through nine every time make a digit function that accepts as an argument the digit in that decimal place and returns "one", "two", "three" etc. Then make another for "twenty, thirty, forty" and one hundred" "two hundred" etc, then string your function calls together accordingly.
I'm an intermediate learner and have been meaning to try this myself with a calculator program but haven't gotten around to it so if my suggestion is not well thought out I apologize.
Topic archived. No new replies allowed.