Answer to Question #268180 in C++ for Immanuel

Question #268180

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!

1
Expert's answer
2021-11-18T06:55:57-0500
#include <iostream>
using namespace std;


string first[] = { "", "one ", "two ", "three ", "four ",
                 "five ", "six ", "seven ", "eight ",
                 "nine ", "ten ", "eleven ", "twelve ",
                 "thirteen ", "fourteen ", "fifteen ",
                 "sixteen ", "seventeen ", "eighteen ",
                 "nineteen " };
string second[] = { "", "", "twenty ", "thirty ", "forty ",
                 "fifty ", "sixty ", "seventy ", "eighty ",
                 "ninety " };
 string figuresToWords(int  number, string temp)
{
    string results = "";
  
    if (number > 19)
        results += second[number / 10] + first[number % 10];
    else
        results += first[number];
 
   
    if (number)
        results += temp;
 
    return results;
}


string final_convert(long number)
{


   
    string results;
 
    
    results += figuresToWords((number / 10000000), "crore ");
 
    
    results += figuresToWords(((number / 100000) % 100), "lakh ");
 
   
    results += figuresToWords(((number / 1000) % 100), "thousand ");
 
    
    results += figuresToWords(((number / 100) % 10), "hundred ");
 
    if (number > 100 && number % 100)
        results += "and ";
 
    
    results += figuresToWords((number % 100), "");
 
    return results;
}
 


int main()
{
		cout<<"Welcome to CS127-8L!\n";
   cout<<" Enter a double value:\n";
    long number;
    cin>>number;
 
    
    cout <<"That is "<< final_convert(number) << endl;
 
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS