Design a class called Numbers that can be used to translate whole dollar amounts
ID: 649866 • Letter: D
Question
Design a class called Numbers that can be used to translate whole dollar amounts in the range 0 through 9999 into an English description of the number. For example, the number 713 would be translated into the string seven hundred thirteen, and 8203 would be translated into eight thousand two hundred three. The class should have a single integer member variable int number; and public member functions: const static string lessThan20[ ]; const static string tens[ ]; const static string hundred; const static string 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. void print(); Numbers(int x){ number = x; } Static string members will specify how to translate key dollar amounts into the desired format (Static member variables must be defined outside of the class). For example, you might use static strings such as const string Numbers::lessThan20[ ]= {"zero", "one", ..., "eighteen", "nineteen" }; const string Numbers::tens[ ] = { "zero", "ten", "twenty",
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string name ("");
string lessthan[20] = {"","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 twodigit[8] = {"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}
cout << "Enter the number to display";
int i,p;
cin >>i;
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];
cout <<"the number in words is " << name <<;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.