How to translate numbers into words?

Apr 11, 2009 at 5:57am
I have to write a program using a class to where, if a user puts in a number, such as 5678, the program will output "Five thousand six hundred seventy eight". I haven't started on the program yet, but how would I go about translating? Can someone lead me in the right direction?

Also, the question says:
The class should have a single integer member variable

int number;

and a collection of static string members that specify how to translate key dollar amounts into the desired format. For example, you might use static strings such as

string lessThan20[] = {"zero", "one"};
string hundred = "hundred";
etc..

I'm not asking for the whole program of course, just how to do it, because I'm really confused on how to do it. Thank you!
Apr 11, 2009 at 9:29am
Having the number 5678, you can convert to "5678" ( http://www.cplusplus.com/forum/articles/9645/ )
get the number of characters on the string (in this case 4).
104-1=103=1000 = "thousand"
now you get the first character in the string ('5') and convert it to "five"
so your string should be built like this: "five thousands"
Do something similar with the other digits.

Another way of getting digits (without using strings) is something like this:
(5678-5678%1000)/1000 = 5
Last edited on Apr 11, 2009 at 9:30am
Topic archived. No new replies allowed.