Using C++ language As a software programmer in a financial institution, you have
ID: 3707823 • Letter: U
Question
Using C++ language
As a software programmer in a financial institution, you have been asked to develop a program that converts dollar amounts between $0 and $9,999 from numbers to words. The program will only accept whole numbers. For instance, it takes $815 and converts it into eight hundred fifteen.
Design a class called Converter to be used in this program. The class should have one integer member variable; int value, and a static array of string objects that specifies how to convert dollar amounts into the desired format. For example:
string below20[20] =
{"zero", "one", ..., "eighteen", "nineteen"};
string hundreds = "hundred”;
string thousands = "thousand";
The Converter class will include a constructor that accepts a nonnegative integer to initialize the Converter object. The class will also include a member function display() that displays the word description of the Converter object. The class will be used in a main function that asks the user to enter a number within the specified range and then displays the word description.
Explanation / Answer
CODE:
#include <iostream>
#include<string>
using namespace std;
class Converter{
public:
int value;
string onesStrings[20] = {"zero", "one", "two", "three","four","five","six","seven",
"eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen",
"eighteen","nineteen"};
string tensStrings[10] = {"", "ten", "twenty", "thirty","forty","fifty","sixty","seventy",
"eighty","ninety"};
Converter(int);
void display();
private:
void extract(int);
};
Converter.cpp
#include "Converter.h"
Converter::Converter(int n){
value=n;
}
void Converter::display(){
extract(value);
}
void Converter:: extract(int value){
if(value>=1000)
{
extract(value/1000);
cout<<" thousand";
if(value % 1000)
{
if(value % 1000 < 100)
{
cout << " and";
}
cout << " " ;
extract(value % 1000);
}
}
else if(value >= 100)
{
extract(value / 100);
cout<<" hundred";
if(value % 100)
{
cout << " and ";
extract (value % 100);
}
}
else if(value >= 20)
{
cout << tensStrings[value / 10];
if(value % 10)
{
cout << " ";
extract(value % 10);
}
}
else
{
cout<<onesStrings[value];
}
}
ConverterMain.cpp
#include <iostream>
#include "Converter.cpp"
using namespace std;
int main(){
int value;
while(1){
cout<<endl<<"Enter value from 0 to 9999"<<endl;
cin>>value;
if(value<0 || value>9999)
break;
Converter c(value);
c.display();
}
cout<<"bye bye";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.