    001: #include <iostream>
    002: #include <string>
    003:
    004: using namespace std;
    005:
    006: /**
    007:    Turn a digit into its English name
    008:    @param n an integer between 1 and 9
    009:    @return the name of n ("one" . . . "nine")
    010: */
    011: string digit_name(int n)
    012: {
    013:    if (n == 1) return "one";
    014:    if (n == 2) return "two";
    015:    if (n == 3) return "three";
    016:    if (n == 4) return "four";
    017:    if (n == 5) return "five";
    018:    if (n == 6) return "six";
    019:    if (n == 7) return "seven";
    020:    if (n == 8) return "eight";
    021:    if (n == 9) return "nine";
    022:    return "";
    023: }
    024:
    025: /**
    026:    Turn a number between 10 and 19 into its English name
    027:    @param n an integer between 10 and 19
    028:    @return the name of n ("ten" . . . "nineteen")
    029: */
    030: string teen_name(int n)
    031: {
    032:    if (n == 10) return "ten";
    033:    if (n == 11) return "eleven";
    034:    if (n == 12) return "twelve";
    035:    if (n == 13) return "thirteen";
    036:    if (n == 14) return "fourteen";
    037:    if (n == 15) return "fifteen";
    038:    if (n == 16) return "sixteen";
    039:    if (n == 17) return "seventeen";
    040:    if (n == 18) return "eighteen";
    041:    if (n == 19) return "nineteen";
    042:    return "";
    043: }
    044:
    045: /**
    046:    Give the English name of a multiple of 10
    047:    @param n an integer between 2 and 9
    048:    @return the name of 10 * n ("twenty" . . . "ninety")
    049: */
    050: string tens_name(int n)
    051: {
    052:    if (n == 2) return "twenty";
    053:    if (n == 3) return "thirty";
    054:    if (n == 4) return "forty";
    055:    if (n == 5) return "fifty";
    056:    if (n == 6) return "sixty";
    057:    if (n == 7) return "seventy";
    058:    if (n == 8) return "eighty";
    059:    if (n == 9) return "ninety";
    060:    return "";
    061: }
    062:
    063: /**
    064:    Turn a number into its English name
    065:    @param n a positive integer < 1,000,000
    066:    @return the name of n (e.g. "two hundred seventy four")
    067: */
    068: string int_name(int n)
    069: {  int c = n; /* the part that still needs to be converted */
    070:    string r; /* the return value */
    071:
    072:    if (c >= 1000)
    073:    {
    074:       r = int_name(c / 1000) + " thousand";
    075:       c = c % 1000;
    076:    }
    077:
    078:    if (c >= 100)
    079:    {
    080:       r = r + " " + digit_name(c / 100) + " hundred";
    081:       c = c % 100;
    082:    }
    083:
    084:    if (c >= 20)
    085:    {
    086:       r = r + " " + tens_name(c / 10);
    087:       c = c % 10;
    088:    }
    089:
    090:    if (c >= 10)
    091:    {
    092:       r = r + " " + teen_name(c);
    093:       c = 0;
    094:    }
    095:
    096:    if (c > 0)
    097:       r = r + " " + digit_name(c);
    098:
    099:    return r;
    100: }
    101:
    102: int main()
    103: {
    104:    int n;
    105:    cout << "Please enter a positive integer: ";
    106:    cin >> n;
    107:    cout << int_name(n);
    108:    return 0;
    109: }
    110:
    111: