question archive 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
Subject:Computer SciencePrice:2.87 Bought7
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. Please Answer in C++
//Headers file section
#include <iostream>
#include <string>
using namespace std;
//main function
int main ()
{
//Declare variables
string name ("");
string lessthan[20] = {"zero ","one ","two ","three ","four ","five ","six ","seven ","eight ","nine ","ten ","eleven ","twelve ","thirteen ","fourteen ","fifteen ","sixteen ","seventeen ","eighteen ","nineteen "} ;
string hundred("hundred ");
string thousand("thousand ");
string million(" million");
string twodigit[8] = {"twenty ","thirty ","forty ","fifty ","sixty ","seventy ","eighty ","ninety "};
//Read input
cout << "Enter the number to display: ";
int i;
int p,q;
cin >>i;
//Translate negative numbers
if(i<0)
{
name="negative ";
i=i*-1;
}
if (i>1000000)
{
p=i/1000000;
i=i%1000000;
if(p > 100)
{
q = p/100;
name += lessthan[q];
name += hundred;
p = p %100;
}
if(p > 20)
{
q = p / 10;
name += twodigit[q-2];
p = p%10;
}
name += lessthan[i];
name +=million;
}
if(i > 1000)
{
p = i /1000;
name += lessthan[p];
name += thousand;
i = i%1000;
}
if(i > 100)
{
p = i/100;
name += lessthan[p];
name += hundred;
i = i %100;
}
if(i > 20)
{
p = i / 10;
name += twodigit[p-2];
i = i%10;
}
name += lessthan[i];
//Display the result
cout <<"The number in words is " << name << endl;
//Pause the system for a while
system("PAUSE");
return 0;
}
Output:
PFA