Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ converting number to words

Tags:

c++

string

int

I came across this program in a book to convert a number to words. The initial program converts numbers 1-1000 but then you are asked to modify the program to accept numbers up to 1,000,000. I got it working with numbers up to 20,999 but can't get it to work past that. I've been tinkering with it all day and looked at a lot of program examples online but everyone that I looked at is using techniques that are beyond me as a beginner. Any advice would be greatly appreciated. Here is what I have so far.

#include <iostream>
#include <string>
using namespace std;

string digitName(int digit);
string teenName(int number);
string tensName(int number);
string intName(int number);


int main()
{
    int input;

    do
    {

    cout << "Please enter a positive integer: ";

    cin >> input;

    cout << "\n" << intName(input) << endl;

    cout << "\n\n" << endl;

    }while (input > 0);

    return 0;
}

string digitName(int digit)
{
    if (digit == 1) return "one";
    if (digit == 2) return "two";
    if (digit == 3) return "three";
    if (digit == 4) return "four";
    if (digit == 5) return "five";
    if (digit == 6) return "six";
    if (digit == 7) return "seven";
    if (digit == 8) return "eight";
    if (digit == 9) return "nine";

    return "";
}

string teenName(int number)
{
    if (number == 10) return "ten";
    if (number == 11) return "eleven";
    if (number == 12) return "twelve";
    if (number == 13) return "thirteen";
    if (number == 14) return "fourteen";
    if (number == 15) return "fifteen";
    if (number == 16) return "sixteen";
    if (number == 17) return "seventeen";
    if (number == 18) return "eighteen";
    if (number == 19) return "nineteen";

    return "";
}

string tensName(int number)
{
    if (number >= 90) return "ninety";
    if (number >= 80) return "eighty";
    if (number >= 70) return "seventy";
    if (number >= 60) return "sixty";
    if (number >= 50) return "fifty";
    if (number >= 40) return "fourty";
    if (number >= 30) return "thirty";
    if (number >= 20) return "twenty";

    return "";
}

string intName(int number)
{
    int part = number;
    string name;

    if (part >= 20000)
    {
        if (part % 10000 == 0)
        {
            name = tensName(part / 1000) + " thousand ";
            part = part % 1000;

        }else
        {
            name = tensName(part / 1000) + " ";
            part = part % 10000;
        }
    }

    if (part >= 10000)
    {
        name = teenName(part / 1000) + " thousand ";
        part = part % 1000;
    }

    if (part >= 1000)
    {
        name = digitName(part / 1000) + " thousand ";
        part = part % 1000;
    }

    if (part >= 100)
    {
        name = name + digitName(part / 100) + " hundred";
        part = part % 100;
    }

    if (part >= 20)
    {
        name = name + " " + tensName(part);
        part = part % 10;

    }else if (part >= 10)
    {
        name = name + " " + teenName(part);
        part = 0;
    }

    if (part > 0)
    {
        name = name + " " + digitName(part);
    }

    return name;
}
like image 370
RootFire Avatar asked Dec 19 '22 12:12

RootFire


1 Answers

Two things: You can save yourself a lot of if typing if you just index into an array. You can solve the problem more genrally by realizing it repeats after the hundreds case. thousands, millions, billions, trillions and quadrillions all follow exactly the same pattern, so it can be done recursively.

    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;

    string digitName(int digit);
    string teenName(int number);
    string tensName(int number);
    string intName(int number);

    vector<string> ones {"","one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    vector<string> teens {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen","sixteen", "seventeen", "eighteen", "nineteen"};
    vector<string> tens {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};

    string nameForNumber (long number) {
        if (number < 10) {
            return ones[number];
        } else if (number < 20) {
            return teens [number - 10];
        } else if (number < 100) {
            return tens[number / 10] + ((number % 10 != 0) ? " " + nameForNumber(number % 10) : "");
        } else if (number < 1000) {
            return nameForNumber(number / 100) + " hundred" + ((number % 100 != 0) ? " " + nameForNumber(number % 100) : "");
        } else if (number < 1000000) {
            return nameForNumber(number / 1000) + " thousand" + ((number % 1000 != 0) ? " " + nameForNumber(number % 1000) : "");
        } else if (number < 1000000000) {
            return nameForNumber(number / 1000000) + " million" + ((number % 1000000 != 0) ? " " + nameForNumber(number % 1000000) : "");
        } else if (number < 1000000000000) {
            return nameForNumber(number / 1000000000) + " billion" + ((number % 1000000000 != 0) ? " " + nameForNumber(number % 1000000000) : "");
        }
        return "error";
    }

    int main()
    {
        long input;
        do
        {
            cout << "Please enter a positive integer: ";    
            cin >> input;
            cout << "\n" << nameForNumber(input) << endl;
            cout << "\n\n" << endl;
        }while (input > 0);
        return 0;
    }
like image 158
Josh Homann Avatar answered Dec 21 '22 02:12

Josh Homann