I\'m trying to write a program that ask the user for a number and translate it i
ID: 3641224 • Letter: I
Question
I'm trying to write a program that ask the user for a number and translate it into its English description.Here is the guidelines : Design a class Numbers that can be used to translate whole dollar amounts in the
range 0 through 9999 into a English description of the number
Also, demonstrate the class by writing a main program that loops asking the user to
enter a number in the proper range and then prints its English description
this class should have a single integer member variable
int num;
and a collection of static string members that specify how to translate key dollar
amounts into the desired format. per example :
static string lessThan20[] = {"zero", "one", ..., "nineteen" };
static string hundred = "hundred";
static string thousand = "thousand";
the class should have a constructor that accepts a non-negative integer and uses it
to initialize the Numbers object. It should have a a member function print() that
prints the English description of the Numbers object.
And that is what i have so far and there something wrong :
\Numbers.h
#include <iostream>
#include <string>
using namespace std;
#ifndef NUMBERS_H
#define NUMBERS_H
class Numbers
{
private:
int num;
static const string decimals[20];
static const string tens[8];
static const string HUNDRED;
static const string THOUSAND;
public:
Numbers(int = 0);
~Numbers();
void print(int res);
};
#endif
\Numbers.cpp
#include "Numbers.h"
const string Numbers::decimals[20] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
const string Numbers::tens[8] = {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
const string Numbers::HUNDRED = "hundred";
const string Numbers::THOUSAND = "thousand";
Numbers::Numbers(int num):num(num)
{
if (num >= 0 && num <= 9999)
this->num = 0;
}
Numbers::~Numbers()
{
}
void Numbers::print(int res)
{
if(num >=0 && num <= 9999)
{
int thousand = num/1000;
int hundreds = (num%1000)/100;
int ten = ((num%1000)%100)/10;
int digits = ((num%1000)%100)%10;
string res = "";
if(thousand > 0)
{
res += decimals[thousand];
res += " thousand ";
}
if(hundreds > 0)
{
res += decimals[hundreds];
res += " hundred ";
}
if(ten > 0 && ten < 2)
{
res+= decimals[ten*10 + digits] + " ";
}
else{
if(ten > 0)
{
res += tens[ten-2] + " ";
}
if(digits > 0 )
{
res+= decimals[digits];
}
}
cout << res;
}
else
{
cout << " Wrong number" ;
}
cout <<endl;
}
\Source.cpp
#include <iostream>
#include "Numbers.h"
using namespace std;
int main()
{
int res = 0;
Numbers* n = new Numbers();
cout << " Enter number 0 - 9999 Enter -1 will end the program " << endl;
while(res != -1)
{
cout << " Enter number 0 - 9999 " << endl;
cin >> res;
n->print(res);
}
delete n;
return 0;
}
Explanation / Answer
Hi, I am not being able to understand your code, can you comment it??? I have done this code differently, if you want I can send it to you as a private message, or just comment your code so I would know how everything is related to each others.