Design a class called Numbers that can be used to translate whole dollar amounts
ID: 3638730 • 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", …, "eighty", "ninety",
string hundred = "hundred";
string thousand = "thousand";
const string Numbers::hundred = "hundred";
const string Numbers::thousand = "thousand";
Demonstrate the class by writing a main program that asks the user to enter a number in the proper range and then prints out its English description
This is what I have so far and there are alot of errors, can you please help me.
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
class Numbers
{
private:
int number;
public:
Numbers(int x) //default constructor
{number = x;
}
void Display();
const static string lessThan20[];
const static string tens[];
const static string hundred;
const static string thousand;
void print();
};
void Numbers::Display()
{
const string Numbers::abovetwenty[20]=
{"zero","one","two","three","four","five","six","seven",
"eight","nine","ten","eleven","tweleve","thirteen","fourteen",
"fifteen","sixteen","seventeen","eighteen","nineteen"};
};
const string Numbers::lessThan20[10]=
{"zero","ten","twenty","thirty","forty","fifty","sixty",
"seventy","eighty","ninety"};
};
//number=abs(number);
int x=number/1000;
if(x>0)
cout<<" "<<abovetwenty[x]<<" thousand ";
number%=1000; //2nd number
x=number/100;
if(x>0)
cout<<abovetwenty[x]<<" hundred ";
number%=100;
if(number>=20)
{
x=number/10;
if(x>0)
cout<<lessThan20[x]<<" ";
}
else if(number>=10)
{
cout<< abovetwenty[number]<< " ";
return;
}
number%=10;
if(number>0)
cout<<abovetwenty[number];
cout<<" ";
}
int main(){
int num;
cout<< "Enter a number: "<<endl;
cin>>num;
while (num!=0){
cout<<"Number has to be positive."<<endl;
}
{
Numbers number(num);
number.Display(); //call display function
cout<<" Enter number:";
cin>>num;
}
system("pause");
return 0;
}
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
class Numbers
{public:
int number;
Numbers(int);
void print( );
void printword(string);
void getthree( long div,string upto20[],string tens[],string group);
};
int main()
{int n;
cout<<"enter a number: ";
cin>>n;
while(n<0||n>9999)
{cout<<"Please enter positive numbers which are no more than 4 digits in length ";
cout<<"enter a number: ";
cin>>n;
}
Numbers num(n);
num.print();
cout<<endl;
system("pause");
return 0;
}
Numbers::Numbers(int n)
{number=n;
}
void Numbers::printword(string w)
{long i;
cout<<w;
cout<<" ";
}
void Numbers:: print()
{long n,t;
string upto20[20]={"zero","one","two","three","four","five","six","seven",
"eight","nine","tenth","eleven","twelve","thirteen",
"fourteen","fifteenth","sixteen","seventeen",
"eighteen","nineteen"};
string tens[10]={"zero","ten","twenty","thirty","forty","fifty","sixty",
"seventy", "eighty", "ninety"};
getthree(100000,upto20,tens,"thousand ");
n=number/100;
if(n>0)
{printword(upto20[n]);
cout<<"hundred ";
}
number%=100;
if(number>=20)
{n=number/10;
if(n>0)
printword(tens[n]);
}
else if(number>=10)
{ printword(upto20[number]);
return;
}
number%=10;
if(number>0)
printword(upto20[number]);
}
void Numbers::getthree(long div,string upto20[],string tens[],string group)
{long n;
n=number/div;
if(n>0)
{printword(upto20[n]);
cout<<"hundred ";
}
number%=div;
n=number/(div/10);
if(n>0)
printword(tens[n]);
number%=(div/10);
n=number/(div/100);
if(n>0)
{printword(upto20[n]);
cout<<group;
}
number%=(div/100);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.