I need help with this C++ problem. Design a class called Numbers that can be use
ID: 3918660 • Letter: I
Question
I need help with this C++ problem.
Design a class called Numbers that can be used to translate integers in the range 0 to 9999 into an English description of the number.
The class should have a single integer member variable:
int number;
and a static array of string objects that specify how to translate the number into the desired format. For example, you might use static string arrays such as:
string lessThan20[20] = {"zero", "one", ..., "eighteen", "nineteen"};
string hundred = "hundred";
string thousand = "thousand";
The class should have a constructor that accepts a nonnegative integer and uses it to initialize the Numbers object. It should have a member function, print(), that prints the English description of the Numbers object.
Demonstrate the class in a main program that prompts the user to input a number and then prints out its English description.
Explanation / Answer
class Numbers{
private :
int num;
const char * const ones[20] = {"zero", "one", "two", "three","four","five","six","seven",
"eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen",
"eighteen","nineteen"};
const char * const tens[10] = {"", "ten", "twenty", "thirty","forty","fifty","sixty","seventy",
"eighty","ninety"};
public :
Numbers(int n) : num(n);
void print()
{
if(num>=1000)
{
expand(num/1000);
cout<<" thousand";
if(num % 1000)
{
if(num % 1000 < 100)
{
cout << " and";
}
cout << " " ;
expand(num % 1000);
}
}
else if(num >= 100)
{
expand(num / 100);
cout<<" hundred";
if(num % 100)
{
cout << " and ";
expand (num % 100);
}
}
else if(num >= 20)
{
cout << tens[num / 10];
if(num % 10)
{
cout << " ";
expand(num % 10);
}
}
else
{
cout<<ones[num];
}
return;
}
}
//This is the class I hope i am able to answer u. All the best