Reference no: EM13215236
Design a class 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 asingle integer member variable:
int number;
and a static array of string objects that specify how to translate key dollar amounts into the desired format. For example, you might use static strings 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 by writing a main program that asks the user to enter a number in the proper range and then prints out its English description.
Fill the blank starting at line 64 of the lab6_ex1_starter.cpp and complete the following functions:
// Take care of hundreds, if any.
// Take care numbers less than a 100.
// Take care of anything less than 20
This starter can work out of the box, however, the denomination other than "thousand" are not working.
the Lab6_ex1_starter.cpp is
// Chapter 14, Programming Challenge 1: Number Class
#include <iostream>
#include <string>
using namespace std;
// Declaration of Numbers class
class Numbers
{
private:
int number; // To hold a number
// Static arrays to hold words
static string lessThan20[20];
static string tens[10];
static string hundred;
static string thousand;
public:
// Constructor
Numbers(int x){ number = x;}
// Function to print the words for the number
void print();
};
// Static member variables must be defined
// outside of the class
string Numbers::lessThan20[20] =
{ "zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen",
"nineteen",
};
string Numbers::tens[10] =
{ "zero", "ten", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety",
};
string Numbers::hundred = "hundred";
string Numbers::thousand = "thousand";
// *********************************************
// The print fucntion prints the English words *
// for the number *
// *********************************************
void Numbers::print()
{
// Residue holds what remains to be printed.
int residue = number;
// Take care of thousands, if any.
int n_thousands = residue/1000;
residue = residue % 1000;
if (n_thousands > 0)
{
cout << " " << lessThan20[n_thousands];
cout << " thousand ";
}
// Fill the blank
// Take care of hundreds, if any.
// Take care numbers less than a 100.
// Take care of anything less than 20
}
// Demo program
int main()
{
int number;
// Tell user what the program does.
cout << "Translates whole dollar amounts into words for"
<< "the purpose of writing checks.n"
<< "Entering a negative number terminates the program.n"
<< "Enter an amount (less than 20000)for be translated into words: ";
cin >> number;
while (number >= 0)
{
// Create a Numbers object.
Numbers n(number);
// Print the English description.
n.print();
// Get another number.
cout << "nEnter another number: ";
cin >> number;
}
return 0;
}