Write a program that contains a function that displays' Welcome to CS127-8L!" Then, it asks for a double value such as 25.36, 100.02 etc. The maximum is 1000. Getting the function displays the value in words. Please see the sample runs below.
Run 1
Welcome to CS127-8L!
Enter a double value: 255.36
That is two hundred twenty-five and thirty-six centavos!
Run 2
Welcome to CS127-8L!
Enter a double value: 800.20
That is eight hundred and twenty centavos!
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
void convert(int value){
string first[20] = {"zero", "one", "two", "three","four","five","six","seven","eight","nine","ten",
"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eightteen","nineteen"};
string second[10] = {"", "ten", "twenty", "thirty","forty","fifty","sixty","seventy","eighty","ninety"};
if(value<0)
{
cout<<" ";
convert(-value);
}
else if(value>=1000)
{
convert(value/1000);
cout<<" thousand";
if(value % 1000)
{
if(value % 1000 < 100)
{
cout << " ";
}
cout << " " ;
convert(value % 1000);
}
}
else if(value >= 100)
{
convert(value / 100);
cout<<" hundred";
if(value % 100)
{
cout << " ";
convert (value % 100);
}
}
else if(value >= 20)
{
cout << second[value / 10];
if(value % 10)
{
cout << "-";
convert(value % 10);
}
}
else
{
cout<<first[value];
}
}
int main(){
double value=-1;
cout<<"Welcome to CS127-8L!\n";
while(value<0 ||value>1000){
cout<<"Enter a double value: ";
cin>>value;
}
int y = int(value * 1000);
int integralpart=y / 1000;
int fractionalPart=(value-integralpart)*100;
cout<<"That is ";
convert(integralpart);
if(fractionalPart>0){
cout<<" and ";
convert(fractionalPart);
cout<<" centavos!";
}
cin>>value;
return 0;
}
Comments
Leave a comment