C++ The name of the file must be convertermain.cpp As a software programmer in a
ID: 3705127 • Letter: C
Question
C++
The name of the file must be convertermain.cpp
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:
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.
* Your code should be structured into the following files:
- Converter.cpp
- Converter.h
- convertermain.cpp
- makefile
Explanation / Answer
/*Content of Converter.h*/
#include<iostream>
#include<string.h>
using namespace std;
/*Content of Converter.cpp*/
int temp=0;
class Converter
{
public:
int value;
char *below20[20]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
char *multiple10[8]={"Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninty"};
char *hundreds="Hundred";
char *thousands="Thousand";
Converter()//constructor to get a value from user and store it in variable "value"
{
value=-1;
while(value<0||value>9999)//to check if the value is in range (0 to 9999)
{
cout<<" Enter the value:";
cin>>value;
if(value<0||value>9999)//print error message if the value is not in range (0 to 9999)
{
cout<<" Invalid Value(0 to 9999) ";
}
}
}
int pow(int power)//to calculate power of ten and return it to caller
{
if(power==0)
{
return 1;
}
else
{
return 10*pow(power-1);
}
}
void display()// to print the figure in words
{
if(value==0)//if value is zero
{
cout<<"zero";
}
temp=value;
cout<<" ";
if(temp<=9999&&temp>999)//if value is in thousands
{
cout<<below20[get_last()]<<" "<<thousands<<" ";
}
if(temp<=999&&temp>99)//if remaining value is in hundreds
{
cout<<below20[get_last()]<<" "<<hundreds<<" ";
}
if(temp<=99&&temp>19)//if remaining value is in range (99 to 20)
{
cout<<multiple10[get_last()-2]<<" ";
}
if(temp<=19&&temp>0)//if value is in range (19 to 1)
{
cout<<below20[temp];
}
}
int get_last()//to get the last digit of value
{
int q=temp,count=0;
while(q>=10)//divide the value till we get leftmost digit of value
{
q=q/10;
count++;
}
temp=temp-(pow(count)*q);//calculate remaining rightmost digits and store it to temp global variable
return q;
}
};
/*Content of convertermain.cpp*/
#include"converter.h"
#include"converter.cpp"
int main()
{
Converter obj1;
obj1.display();
cout<<" ----------------------------------------------------------- ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.