# Thread: [Jumping into C++] Need help with Chapter 7, problem 1

1. ## [Jumping into C++] Need help with Chapter 7, problem 1

So, I was strengthening my C++ foundation with this book, reading each chapter and doing each practice problem (even when I already knew the material). I'm currently on chapter 7, practice problem 1, which is:

Implement the source code that turns numbers into English text.

What I've tried thus far:
- See how I could do it with all the material up to that chapter. Seems I couldn't, so I relied on my overall knowledge (and some tutorials).
- Was stumped on how to begin coding it, so I spent a few days pseudocoding and brainstorming before I found an algorithm I thought would work.
- Spent about a week coding in that algorithm, changing some things (since the functions got longer than I expected). Took this long due to all the debugging, and making helper functions.

Now, I managed to make a functional program, but the thing is, it doesn't translate like it should. I'm not sure where I went wrong, but after spending a week, I thought I should post here to get some insight.

Here's the (long and ugly) source: show at bpaste

I'd very much appreciate help with this  2. Implement the source code that turns numbers into English text.
I'm assuming this means translate "17" into "seventeen".

I would say, start slow with this. Use a pen and paper and write out a bunch of sample data and look for patterns. For example, if we have a two digit number and it begins with a one, chances are it's going to be "teen". It could also be eleven or twelve.

But before that, write a function that can just handle 0 through 9. That should be simple enough. You can even use a giant switch-statement.

When it comes to larger digits, you're probably going to have to look at the number of digits first because that will help determine the grammar of the English representation. That's kind of what we're doing with "17". We know it's two digits and that it starts with a 1. If the next digit is a 1 we return "eleven". It's it' a "2" we return "twelve". If it's a "3" we return "thirteen".

Man, this sounds hard. 3. That was scary. Sorry I do not have the time to read all the code posted but I have to say you missed the target by a mile. Just two hints, you do not need to convert the number to string at all and you need a couple of arrays of strings for a fast conversion. I will take a detail look on your code at a later time and I'll be back with more details hopefully. If you have known numbers that it fails it would be helpful to post them too. 4. Originally Posted by MutantJohn Man, this sounds hard.
It's actually easy. Give it a try! 5. @MutantJohn: That part about finding patterns was explained in the chapter, and in the case of this problem, the pattern-finding was done for me. Look in the chapter, and you'll see that the pattern-finding for this was spelled out.

And based on that, I made my algorithm.

@taaz: I don't blame you xD Also, I'll see what I can do to figure it out with your hint. 6. Originally Posted by Tesp @MutantJohn: That part about finding patterns was explained in the chapter, and in the case of this problem, the pattern-finding was done for me. Look in the chapter, and you'll see that the pattern-finding for this was spelled out.
And based on that, I made my algorithm.
I just downloaded the book and took a quick look on the problem and as far as I can see the methodology was spelled out for you a number of samples of the patterns were given but they are neither complete nor accurate, you need to work on it your self for example how many "unique" numbers there are in English before you start adding postfixes for the "order of magnitute" (lacking of a better word). Originally Posted by Tesp @taaz: I don't blame you xD Also, I'll see what I can do to figure it out with your hint.
You have already used the methodology for my 1st hint on the problem with the primes in the same book. 7. So, I spent some more time on it, and I'm still stumped.

What I tried this time:

- Try to think of an algorithm that can turn a number from 0 to a cap of 999,999,999 into english text, all without using string conversions. It would iterate through each number from zero to that cap, using some basic arithmetic operations. These operations would help fill a string array with the english translations of each iterated integer, each integer corresponding to its index in that array. However, the best I could think of was one that'd work for numbers from 0-999, which is far from my initial goal.

- Go back to my previous program, and manually trace the logic based on a sample input of 123. I had no idea how 123 was translated as "one hundred twenty three". I would have used the Code Blocks or Netbeans debugger, but for some reason, they dont let me see the contents of vectors. That is, except for the first element added. That annoyance pretty much rendered debuggers useless for this problem (I need to know how the contents got changed around!), hence why I had to manually trace the logic.

- I forgot to mention this when making the OP, but I did try using the debuggers before asking here for help. As the above point explains, they didn't really help.

So, yeah. Any other advice? I'll spend another couple of days on this, seeing if I can stumble upon some solution in the meantime. 8. - Try to think of an algorithm that can turn a number from 0 to a cap of 999,999,999 into english text, all without using string conversions. It would iterate through each number from zero to that cap, using some basic arithmetic operations. These operations would help fill a string array with the english translations of each iterated integer, each integer corresponding to its index in that array. However, the best I could think of was one that'd work for numbers from 0-999, which is far from my initial goal.
A lookup table would take far too much memory if you stored all of the numbers.

It is better to break up the number into groups of three. Say I gave you a small number 123. There are three small place values in this number that you should be able to divide to get to digit form. 1=one hundred, 2=twenty 3=three.

Once you master this, then you can take your code that explains this group to much larger numbers like 31,415,930. You will notice that other, larger place values are also groups of three: "thirty one million four hundred fifteen thousand nine hundred thirty." So it is all about having the right words for place value after the group of three. 9. OP: if you are open to looking at what's available out there, you might wish to take a look at the following link. Unless of course you want to get to the end result on your own and I respect that:

c++ - Convert number to words - Code Review Stack Exchange 10. Originally Posted by whiteflags A lookup table would take far too much memory if you stored all of the numbers.

It is better to break up the number into groups of three. Say I gave you a small number 123. There are three small place values in this number that you should be able to divide to get to digit form. 1=one hundred, 2=twenty 3=three.

Once you master this, then you can take your code that explains this group to much larger numbers like 31,415,930. You will notice that other, larger place values are also groups of three: "thirty one million four hundred fifteen thousand nine hundred thirty." So it is all about having the right words for place value after the group of three.
I'll try that. When I finish the code that converts numbers up to three digits long, I'll post it here before tackling the initial goal.  Originally Posted by sean_cantab OP: if you are open to looking at what's available out there, you might wish to take a look at the following link. Unless of course you want to get to the end result on your own and I respect that:

c++ - Convert number to words - Code Review Stack Exchange
Those code examples don't quite solve my problem (I'm going for 9 digits, not 3), but I'll reverse engineer those code samples and see if that'll help.

Thanks to the both of you  11. Scroll down, it goes up to 999,999,999 12. Originally Posted by Tesp So, I spent some more time on it, and I'm still stumped.

What I tried this time:

- Try to think of an algorithm that can turn a number from 0 to a cap of 999,999,999 into english text, all without using string conversions. It would iterate through each number from zero to that cap, using some basic arithmetic operations. These operations would help fill a string array with the english translations of each iterated integer, each integer corresponding to its index in that array. However, the best I could think of was one that'd work for numbers from 0-999, which is far from my initial goal.

- Go back to my previous program, and manually trace the logic based on a sample input of 123. I had no idea how 123 was translated as "one hundred twenty three". I would have used the Code Blocks or Netbeans debugger, but for some reason, they dont let me see the contents of vectors. That is, except for the first element added. That annoyance pretty much rendered debuggers useless for this problem (I need to know how the contents got changed around!), hence why I had to manually trace the logic.

- I forgot to mention this when making the OP, but I did try using the debuggers before asking here for help. As the above point explains, they didn't really help.

So, yeah. Any other advice? I'll spend another couple of days on this, seeing if I can stumble upon some solution in the meantime.
OK lets forget for a second the numbers, how about the words, what are the smaller number of string constants, required to be able to combine them in all the numbers up to 100?
Have you thought through that already?
for example the number from 1 to 20 are all unique words, there is not a single repetition. what else is missing?

To get you started let me show you the way to break a number down to its parts.

You start with a number (lets do 3 digits) 539, now to get the first digit which is the smallest number aka the right most digit, you use modulus (known as mod) 10, so in order to get 9 out of 539 you calculate 539 mod 1, (if I'm not mistaken the character % is used to calculate mod in c/c++) and you get 9 as a result. To remove its value from the existing number leaving you with 53 you use an integer division eg 539 / 10 which it will return 53 as a result. Keep on going keeping a order some where (like 1 for 539, 2 for 53, 3 for 5 etc) while you work and you have all the information needed to solve your problem.

Can you work your way through this now?
Better now? 13. Originally Posted by sean_cantab Scroll down, it goes up to 999,999,999
Oh, I see it now! Though I'll see if I can solve the problem my own way, first; if I can't, I'll reverse engineer that example so I can at least get how it works.

@taazz: Could you please fix the grammar in your post? I really can't understand what you're trying to say, there ^^; 14. Actually, the grammar is fine. You're just having trouble visualizing the algorithm. 15. Originally Posted by MutantJohn Actually, the grammar is fine. You're just having trouble visualizing the algorithm.
I have no shame in admitting I'm having trouble with the algorithm. Still, before he edited his post, the grammar was too flawed for me to understand. Originally Posted by taazz OK lets forget for a second the numbers, how about the words, what are the smaller number of string constants, required to be able to combine them in all the numbers up to 100?
Have you thought through that already?
I have, though not for the numbers up to 100. Originally Posted by taazz for example the number from 1 to 20 are all unique words, there is not a single repetition. what else is missing?
I thought the only unique words were for the numbers 10-19, since the others single-to-double-digit numbers are easier to format. Originally Posted by taazz To get you started let me show you the way to break a number down to its parts.

You start with a number (lets do 3 digits) 539, now to get the first digit which is the smallest number aka the right most digit, you use modulus (known as mod) 10, so in order to get 9 out of 539 you calculate 539 mod 1, (if I'm not mistaken the character % is used to calculate mod in c/c++) and you get 9 as a result. To remove its value from the existing number leaving you with 53 you use an integer division eg 539 / 10 which it will return 53 as a result. Keep on going keeping a order some where (like 1 for 539, 2 for 53, 3 for 5 etc) while you work and you have all the information needed to solve your problem.

Can you work your way through this now?
Better now?
I think I get what you mean. In fact, I did have integer division and modulus in the pseudocode for my second attempt, but got stumped in terms of how to use it for numbers with more than 3 digits. I'll try reformulating my algorithm with this, and I'll report the results. Popular pages Recent additions beginner, c++, chapter, jumping into c++, problem 