Design a class called Numbers that can be used to translate integers in the rang
ID: 3759073 • Letter: D
Question
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
#include<iostream>
#include<string.h>
using namespace std;
class Numbers {
int number;
string lessThan20[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";
public:
Numbers(int n) {
number = n;
}
void print() {
int temp = number/1000;
if(temp != 0) {
cout<<lessThan20[temp]<<" "<<thousand<<" and ";
int temp2 = (number - temp*1000)/100;
if(temp2 != 0) {
cout<<lessThan20[temp2]<<" "<<hundred<<" and ";
int temp3 = (number - temp*1000 - temp2*100);
if(temp3 < 20) {
cout<<lessThan20[temp3];
} else {
int b = temp3/10;
int a = temp3 - b*10;
cout<<lessThan20[a]<<" "<<lessThan20[b];
}
}
}
}
};
int main() {
Numbers n(9919);
n.print();
cout<<" ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.