Numbers Class Design a class Numbers that can be used to translate whole dollar
ID: 3547869 • Letter: N
Question
Numbers Class
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 a single 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.
If the user enters invalid input such a negative number, print to the user "You entered an invalid number, please try again."
Explanation / Answer
Here is the program which is tested for a number of sample inputs.For the invalid numbers,it will show invalid and exit.Other numbers it will shows the words.Depending upon your paging file size of your commputer,it may show a runtime error.Please neglect it.
#include<iostream>
#include<string>
#include <stdio.h>
#include <conio.h>
#include <math.h>
using namespace std;
int main()
{
string s1[]={"zero","one","two","three","four","five","six",
"seven","eight","nine"};
string s2[]={"zero ","eleven","twelve","thirteen","fourteen","fifteen",
"sixteen","seventeen","eighteen","nineteen"};
string s3[]= {"zero","ten","twenty","thirty","fourty","fifty","sixty","seventy",
"eighty","ninety"};
int num=0;
int input;
int b=0;
std::cout<<" This program converts whole numbers to words ";
std::cout<<"enter numbers and press enter ";
cin >> input;
if (input >= 0 && input <= 9999){
int c = 0; /* digit position */
int n = input;
while (n != 0)
{
n /= 10;
c++;
}
int a[3];
c = 0;
n = input;
a[0]=0;
a[1]=0;
a[2]=0;
a[3]=0;
/* extract each digit */
while (n != 0)
{
a[3-c] = n % 10;
n /= 10;
c++;
}
std::cout<<s1[a[0]]<<" thousand";//write thousands value 1-9
if(a[1]==0 && a[2]==0 && a[3]==0)return 0;
if(a[1]==0 && a[2]>0 && a[3]>0)std::cout<<" and ";//iff no hundreds write "and"
if(a[1]>0)std::cout<<' '<<s1[a[1]]<<" hundred";//write hundreds value
if(a[1]>0)std::cout<<" and ";
if(a[2]==1)std::cout<<s2[a[3]];
if(a[2]>1)std::cout<<s3[a[2]]<<s1[a[3] ];//4735
if(a[1]==0 && a[2]==0)std::cout<<" and "<<s1[a[3] ];//4006
if(a[1]==0 && a[2]==1)std::cout<<s2[a[3]];//4013
b=1;
}
if (b==0){
printf("You entered an invalid number, please try again ");
}
getchar();
return 0;
}
*Tested in visual studio 2010
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.