Numbers Class

Hey guys,

I'm working on this program that I have to design a class Numbers that can be used to translate whole numbers to the English description of the number.

Now this is what I got so far:

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

class Numbers {
private:
int number;
static string ones[];
static string tens[];
static string teens[];
static string hundreds[];
static string thousands[];

public:
Numbers();
Numbers(int num)
{
setNum(num);
};
void setNum(int num)
{
number = num;
};
void print();
};

string Numbers::ones[] = { "one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ", "nine " };

string Numbers::teens[] = { "ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen " };

string Numbers::tens[] = { "twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety " };

string Numbers::hundreds[] = { "one hundred ", "two hundred ", "three hundred ", "four hundred ", "five hundred ", "six hundred ", "seven hundred ", "eight hundred ", "nine hundred " };

string Numbers::thousands[] = { "one thousand ", "two thousand ", "three thousand ", "four thousand ", "five thousand ", "six thousand ", "seven thousand ", "eight thousand ", "nine thousand " };

void Numbers::print()
{
if (number < 0)
{
number = -number;
}

if (number == 0)
{
cout << "The number is zero ";
}

if (number >= 1000)
{
cout << thousands[number / 1000];
number %= 1000;
}

if (number >= 100)
{
cout << hundreds[number / 100];
number %= 100;
}

if (number >= 20)
{
cout << tens[(number / 10) - 1];
number %= 10;
}

if (number >= 10 && number <= 19)
{
cout << teens[(number % 10) + 1];
}

if (number > 0)
{
cout << ones[number];
}

cout << "dollars";
}

int main()
{
cout << " Please enter the amount you would like translated into words: ";

int number;

cin >> number;

while (number >= 0) {
Numbers n(number);
n.print();
cout << "\n please enter another number: ";
cin >> number;
}

cout << " the progrm is done!" << endl;

system("pause");
return 0;
}


The program seems to work. However its not giving me the right number description,

Example:

Please enter the amount you would like translated into words: 5
six dollars
please enter another number: 10
eleven dollars
please enter another number: 20
thirty dollars
please enter another number: 30
forty dollars
please enter another number: 100
two hundred dollars
please enter another number: 150
two hundred sixty dollars
please enter another number: 500
six hundred dollars
please enter another number: 1000
two thousand dollars
please enter another number:

Can someone help me out I would appreciate it!
Array's items are started from zero.
I.e. int x[3] contains x[0], x[1] and x[2] only.
sweet it works!, Thanks a lot man
Nice job.

Here are some pointers to help make the code even more useful.

- Recognize that you don't need a class for this at all. The whole thing can be done inside a function. The only trick is making the string arrays static and putting them inside the func. I understand that this may be a homework project and you have to make a class, but I wanted to point this out anyway.

What if you wanted to print some number of Yen? or the number of cartons of milk? What if you wanted to print the number to a different file? To solve these problems, you should have a function that converts the number to a string. Let the caller decide what to do with that string. This highlights a very useful design philosophy: your classes and functions don't need to do all the work. Rather, they need to make doing the work easy.

'Hmm. I think every even number should print as "xyz dollars" and every odd number should print as "the number is abc"....' Sounds ridiculous right? And yet you have something similar: every number prints "xyz dollars" except zero. Make it consistent.

Finally, recognize that if you can convert 0-999 to text then doing thousands, millions, billions and even higher is easy.

The following function is based on your code and will convert any non-negative 32-bit integer to text:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
string numToString(int n)
{
    static string ones[] =
	{"one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ", "nine "};
    static string teens[] =
	{"ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen "};

    static string tens[] =
	{"twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety "};
 
    string result;

    if (n < 0) {
	n = -n;
    }

    if (n == 0) {
	result = "zero ";
    }
    if (n >= 1000000000) {
	result += numToString(n / 1000000000);
	result += " billion ";
	n %= 1000000000;
    }
    if (n >= 1000000) {
	result += numToString(n / 1000000);
	result += " million ";
	n %= 1000000;
    }
    if (n >= 1000) {
	result += numToString(n / 1000);
	result += " thousand ";
	n %= 1000;
    }

    if (n >= 100) {
	result += numToString(n / 100);
	result += " hundred ";
	n %= 100;
    }
    if (n >= 20) {
	result += tens[(n / 10) - 2];
	n %= 10;
    }
    if (n >= 10) {
	result += teens[n-10];
	n = 0;
    }
    if (n > 0) {
	result += ones[n-1];
    }

    // The value ends with a space, trim it off.
    result.resize(result.size()-1);
    return result;
}

I used your trick of always having the string end with a space while I build it up, but I remove the space at the very end.

Notice that the code for billions, millions, thousands and hundreds is very similar. We can make that code smaller by putting the parts that change in a table and walking through the table. Here's a version that uses such a table and works with long longs for really huge numbers (up to about 9.223 quintillion):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
string numToString(long long n)
{
    static string ones[] =
	{"one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ", "nine "};
    static string teens[] =
	{"ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen "};

    static string tens[] =
	{"twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety "};
 
    struct Limit {
	long long limit;
	string name;
    };
    static Limit limits[] = {
	{1000000000000000000LL, " quintillion " },
	{   1000000000000000LL, " qadrillion " },
	{      1000000000000LL, " trillion " },
	{           1000000000, " billion " },
	{              1000000, " million " },
	{                 1000, " thousand " },
	{                  100, " hundred " },
	{ 0, "" }
    };

    string result;

    if (n < 0) {
	n = -n;
    }

    if (n == 0) {
	result = "zero ";
    }
    for (Limit *p = limits; p->limit; ++p) {
	if (n >= p->limit) {
	    result += numToString(n / p->limit);
	    result += p->name;
	    n %= p->limit;
	}
    }
    if (n >= 20) {
	result += tens[(n / 10) - 2];
	n %= 10;
    }
    if (n >= 10) {
	result += teens[n-10];
	n = 0;
    }
    if (n > 0) {
	result += ones[n-1];
    }

    // The value ends with a space, trim it off.
    result.resize(result.size()-1);
    return result;
}

please enter another number: 9123456789123456789
nine quintillion one hundred twenty three qadrillion four hundred fifty six trillion
seven hundred eighty nine billion one hundred twenty three million
four hundred fifty six thousand seven hundred eighty nine dollars

wow that's crazy haha

Ill have to keep this idea program in mind
Topic archived. No new replies allowed.