question archive Using C++ 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
Using C++
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.
Answer:
Please find the code below:::
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
class Numbers
{
public:
static int numbers;
static void print();
};
void Numbers::print()
{
int b;
string lessThan20[] = { "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 tens[] = { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
if (numbers < 0)
cout << "it is a negative number";
numbers = abs(numbers);
b = numbers / 1000;
if (b > 0)
cout << " " << lessThan20[b] << thousand;
numbers %= 1000;
b = numbers / 100;
if (b > 0)
cout << lessThan20[b] << hundred;
numbers %= 100;
if (numbers>= 20)
{
b = numbers / 10;
if (b > 0)
cout << tens[b] << " ";
}
else if (numbers >= 10)
{
cout << lessThan20[numbers] << " ";
return;
}
numbers %= 10;
if (numbers > 0)
cout << lessThan20[numbers];
cout << " ";
}
int Numbers::numbers;
int main()
{
int b;
cout << "Please enter a number from 0-999 or enter 0 to Exit : "<<" ";
cin >> b;
while (b != 0)
{
cout << "The number " << b << " " << "would be translated into - "<<" ";
Numbers::numbers = b;
Numbers::print();
cout << "\nPlease enter a number from 0-999 or enter 0 to Exit : "<<" ";
cin >> b;
}
cin.ignore();
cin.get();
system("pause");
return 0;
}
output:
PFA