C++ Design a class called Numbers that can be used to translate integers in the
ID: 3819879 • Letter: C
Question
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.
Explanation / Answer
Here is your code below. I have added all the required comments below for your understanding and output as well: -
Numbers.h
#ifndef NUMBERS_H
#define NUMBERS_H
#include <iostream>
#include <string>
using namespace std;
class Numbers
{
public:
int number;
static string lessThan20[ ];
static string bet20To100[ ];
static string hundred;
static string thousand;
Numbers(int);//Constructor
void print();//method to print the value
};
#endif
Numbers.cpp
#include <iostream>
#include <string>
#include "Numbers.h"
using namespace std;
//initializing the variables and arrays
string Numbers::lessThan20[ ] =
{"zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
"nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sisteen", "seventeen", "eighteen", "nineteen" };
string Numbers::bet20To100[ ] =
{" ", " ", "twenty", "thirty", "forty", "fifty", "sixty",
"seventy", "eightty", "ninety" };
string Numbers::hundred = "hundred";
string Numbers::thousand = "thousand";
//********************************************************************************
// Numbers
// This constructor accepts an integer and uses it to initialize the
// Numbers object.
//********************************************************************************
Numbers::Numbers(int num)
{
// cout << “Creatng an instance Numbers. ”;
if (num >= 0 || num <= 9999)
number = num;
else
cout << "Error! Number is outside valid range. ";
}
//********************************************************************************
// Print
// This member function prints the English descripton of the Numbers object.
//********************************************************************************
void Numbers::print()
{
int K = 0, // Holds thousand value
H = 0, // Holds hundread value
T = 0, // Holds values less than 100
N = 0;
string EngNum = " "; // Holds the English description of number
// If number has a value in the thousands convert the
// thousand value to English.
if (number > 999)
{
K = number / 1000;
EngNum = lessThan20[K] + " " + thousand + " ";
N = K * 1000;
}
// If number has a value in the hundreds convert
// hundreds value to English.
if (number > 99)
{
if (number > 999)
{
H = (number - N)/100;
}
else
H = number / 100;
EngNum += lessThan20[H] + " " + hundred + " ";
N += (H * 100);
}
// Get tens value.
if (N > 0)
{
T = number - N;
}
else
T = number;
// Convert tens value to English.
if (T > 19)
{
EngNum += bet20To100[(T/10)] + " " + lessThan20[(T%10)];
}
else
EngNum += lessThan20[T];
// Display English description
cout << "English descripton: " << EngNum << "." << endl;
}
main.cpp
#include <iostream>
#include <string>
#include "Numbers.h"
using namespace std;
int main()
{
int Num;
//Ask user for the input
cout << "This program displays the English description of a number. ";
cout << "Enter a number in the range 0 through 9999: ";
cin >> Num;
Numbers m(Num);
m.print();
return 0;
}
Output: -
This program displays the English description of a number.
Enter a number in the range 0 through 9999: 9999
English descripton: nine thousand nine hundred ninety nine.
--------------------------------
Process exited after 2.559 seconds with return value 0
Press any key to continue . . .
This program displays the English description of a number.
Enter a number in the range 0 through 9999: 0
English descripton: zero.
--------------------------------
Process exited after 1.911 seconds with return value 0
Press any key to continue . . .
This program displays the English description of a number.
Enter a number in the range 0 through 9999: 876
English descripton: eight hundred seventy six.
--------------------------------
Process exited after 2.995 seconds with return value 0
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.