I mean, it looks like you got most of it. One through twenty are most of the unique words.
Now you just gotta realize:
howmany = input / 100000; This tells you how many 100 thousands there are, so you can say "x hundred thousand"
howmany = input / 1000; This tells you how many thousands there are, so you can say "xy thousand"
howmany = input / 100; This tells you how many 100s there are so you can say "x hundred yz"
Now you already did 1 through 20, so you should be able to see using howmany that way you can reuse the code that works, and cover lots of numbers.
You can get very fancy with this, but the simplest version to understand (probably) uses recursion:
Code:
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
string explain(long n);
int main()
{
long tests[] = {0, 11, 43, 266, 1403, 314159, 1000000, -1000000, 1234567890};
for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i) {
cout << setw(10) << tests[i] << ": " << explain(tests[i]) << endl;
}
}
string explain(long n)
{
const static string words[] =
{
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
"nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen", "twenty", "thirty",
"forty", "fifty", "sixty", "seventy", "eighty", "ninety"
};
if (n < -1000000 || n > 1000000) return "unsupported";
string out;
if (n < 0) {
out = "minus ";
n = -n;
}
if (n == 1000000) return out + "one million";
else if (n >= 1000) {
out = out + explain(n / 1000) + " thousand";
n %= 1000;
if (n > 0) out = out + ' ' + explain(n);
}
else if (n >= 100) {
out = out + words[n / 100] + " hundred";
n %= 100;
if (n > 0) out = out + ' ' + explain(n);
}
else if (n >= 20) {
out += words[n / 10 + 18];
n %= 10;
if (n > 0) out = out + '-' + words[n];
}
else out += words[n];
return out;
}
0: zero
11: eleven
43: forty-three
266: two hundred sixty-six
1403: one thousand four hundred three
314159: three hundred fourteen thousand one hundred fifty-nine
1000000: one million
-1000000: minus one million
1234567890: unsupported
That's very convertible to your own algorithm, I think.