#include #include using namespace std; /** * Translate an integer value into words. * @param n the value to translate. * @return the words. */ string translate(int n); /** * Translate a ones digit into a word. * @param n the ones digit. * @return the word. */ string translate_ones(int n); /** * Translate a teens value into a word. * @param n the value to translate. * @return the word. */ string translate_teens(int n); /** * Translate a tens value into a word. * @param n the value to translate. * @return the word. */ string translate_tens(int n); /** * Translate the hundreds digit into words. * @param n the value to translate. * @return the words. */ string translate_hundreds(int n); int main() { int number; // Read and translate numbers until a value <= 0. do { cout << endl << "Number? "; cin >> number; if (number > 0) { cout << number << " : " << translate(number) << endl; } } while (number > 0); cout << "Done!" << endl; return 0; } string translate(int n) { string words = ""; int hundreds = n/100; // Make sure the number isn't too big. if (hundreds > 9) { return "??? number too big"; } // If there's a hundreds digit, translate it. if (hundreds > 0) { words += translate_hundreds(hundreds); n %= 100; // remove the hundreds digit. } // Is it a teens value? if ((n >= 11) && (n <= 19)) { words += translate_teens(n); } else { int tens = n/10; // get the tens digit words += translate_tens(tens); n %= 10; // If there's anything left, // it must be a ones digit. if (n > 0) { // Insert a hyphen. if (tens >= 2) { words += "-"; } words += translate_ones(n); } } return words; } string translate_ones(int n) { switch (n) { case 0: return ""; case 1: return "one"; case 2: return "two"; case 3: return "three"; case 4: return "four"; case 5: return "five"; case 6: return "six"; case 7: return "seven"; case 8: return "eight"; case 9: return "nine"; default: return ""; } } string translate_teens(int n) { switch (n) { case 11: return "eleven"; case 12: return "twelve"; case 13: return "thirteen"; case 14: return "fourteen"; case 15: return "fifteen"; case 16: return "sixteen"; case 17: return "seventeen"; case 18: return "eighteen"; case 19: return "nineteen"; default: return ""; } } string translate_tens(int n) { switch (n) { case 0: return ""; case 1: return "ten"; case 2: return "twenty"; case 3: return "thirty"; case 4: return "forty"; case 5: return "fifty"; case 6: return "sixty"; case 7: return "seventy"; case 8: return "eighty"; case 9: return "ninety"; default: return ""; } } string translate_hundreds(int n) { return translate_ones(n) + " hundred "; }