GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Dec-2005, 08:33
thulaotzu thulaotzu is offline
New Member
 
Join Date: Oct 2005
Posts: 26
thulaotzu is on a distinguished road

Converting numbers to words


I have an assignment that asks for the following:
Write a C++ program that inputs a dollar amount to be printed on a check and:

1) Calls a function to print the amount in check-protected format with leading asterisks if necessary. [For example amount 1230.60 should be written as **1,230.63] Assume that eleven spaces are available for printing an amount.

2) Calls a function to write the word equvalent of the amount. For example, amount 1230.60 should be written as: ONE THOUSAND TWO HUNDRED THIRTY and 60/100

I have no idea how to approach this problem... Can anyone help me with this?

Below is my C++ code. (Sorry for the short code, but as you can see, I'm kinda stuck...)

CPP / C++ / C Code:
 #include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main ()
{

	int number;
	
	// read numbers 

	cout << "Enter amount of money:" << endl;
	cin >> number;


	cout << number << endl; 

	return 0;
}
  #2  
Old 02-Dec-2005, 10:53
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough
Arrow

Re: Converting numbers to words


Hi thu,

Another good assignment on the cards.

First, declare the number as a float, and not int.

Because this says so:
Quote:
Originally Posted by thu
1) Calls a function to print the amount in check-protected format with leading asterisks if necessary. [For example amount 1230.60 should be written as **1,230.63] Assume that eleven spaces are available for printing an amount.

You can do this program by this algorithm::

1. Find the number of digits in the number n. Let this be length.
(you can use % operator only to integer. So, make int number equal to float input...)
2. Use a string say s to store the number in check-protected format.
3. Add the digits of the number one by one in the string. Use a count variable to check whether three digits have been added to the string.
4. If three digits have been added to the string, then add a comma: ','
5. Go on till the number becomes zero.
6. Finally reverse the string.

So, here is an example:
Code:
Enter the number: 18624 Length of the number is 5. Inside a loop: Add 4 to string. s = 4 Add 2 to string. s = 42 Add 6 to string. s = 426 Add a comma. s = 426, Add 2 to string. s = 426,8 Add 1 to string. s = 426,81 End loop Reverse the string. s = 18,624 Gotcha!

For printing the remaining spaces by '*', you can use the cout.fill method.
So, for an example, consider that the string s is equal to 18,624.
When we do like this:
CPP / C++ / C Code:
cout.width(11); //there are 11 spaces available
cout.fill('*');     // fill blank spaces with *
cout<<s;

The output will be like:
Code:
*****18,624

Now, on to the second part of the program:
Quote:
Originally Posted by thu
2) Calls a function to write the word equvalent of the amount. For example, amount 1230.60 should be written as: ONE THOUSAND TWO HUNDRED THIRTY and 60/100
For this, you can create a string array with corresponding number in the text.
For example, like this:
CPP / C++ / C Code:

string text[] = { "Zero", "One", ......, "Nine", "Ten", "Eleven", "Twelve", ....,"Twenty", "Thirty", ...., "Ninety"};

So, text[0] will be "Zero".
and text[10] = "Ten"
and text[11] = "Eleven"..
and text[21] = "Thirty" and so on...
(Remember the place where the text is stored...)

Then, we can check whether each number in the string.
If we get a comma, we should check the length of the string.
If the length is less than 6, there is only one comma.
So, we can print THOUSAND.

If the length if less than 9, there are two commas.
So, on the encounter of first comma, print MILLION.
On the encounter of next comma, print THOUSAND, etc.

Here is a sample execution:
Code:
string s is 18,624 Inside the loop Encountered 1. Check next character. Encountered 8. Check next character. Encountered ,. Print 18 to string. Check length. Length < 6 and Length > 3. Add THOUSAND to string. string s = EIGHTEEN THOUSAND Encountered 6. Check next character. Encountered 2. Check next character. Encountered 4. Print SIX to string. Check length. Length < 6. So, Add HUNDRED AND to string. s = EIGHTEEN THOUSAND SIX HUNDRED AND Print 24 to string. Print TWENTY. Print FOUR s = EIGHTEEN THOUSAND SIX HUNDRED AND TWENTY FOUR End loop. End of the program.

Another sample:
Code:
string s is 1,128,624 Inside the loop Encountered 1. Check next character. Encountered ,. Check length. length < 9 and length > 6. count = 1. Print ONE. Print MILLION. string s = ONE MILLION Encountered 1. Check next character. Encountered 2. Check next character. Encountered 8. Print ONE . Print HUNDRED AND. Print 24: Print TWENTY. Print FOUR. Check count. count = 1. Check length. length < 9 and length > 6. Print THOUSAND string s = ONE MILLION ONE HUNDRED AND TWENTY FOUR THOUSAND Encountered 6. Check next character. Encountered 2. Check next character. Encountered 4. Print SIX to string. Add HUNDRED AND to string. Print 24 to string. Print TWENTY. Print FOUR string s = ONE MILLION ONE HUNDRED AND TWENTY FOUR THOUSAND SIX HUNDRED AND TWENTY FOUR End loop. End of the program.


So, Do the first part correct.
Then Go to the Second part.
The second part is a little bit tougher.

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #3  
Old 03-Dec-2005, 07:10
thulaotzu thulaotzu is offline
New Member
 
Join Date: Oct 2005
Posts: 26
thulaotzu is on a distinguished road

Re: Converting numbers to words


Oh la la.. Looks like I've got my hands full with eh. That really helps Paramesh. I'll let you know if I come up with any coding.
  #4  
Old 08-Dec-2005, 09:42
vazagothic vazagothic is offline
New Member
 
Join Date: Dec 2005
Posts: 2
vazagothic is on a distinguished road

Re: Converting numbers to words


I know it's a C++ forum, but I have just created something in PHP which you may find useful

PHP Code:

<?php
// Create an array with values 0-100

$TextNumber = array(0 => "zero",
                    1 => "one",
                    2 => "two",
                    3 => "three",
                    4 => "four",
                    5 => "five",
                    6 => "six",
                    7 => "seven",
                    8 => "eight",
                    9 => "nine",
                    10 => "ten",
                    11 => "eleven",
                    12 => "twelve",
                    13 => "thirteen",
                    14 => "fourteen",
                    15 => "fifteen",
                    16 => "sixteen",
                    17 => "seventeen",
                    18 => "eighteen",
                    19 => "nineteen",
                    20 => "twenty",
                    30 => "thirty",
                    40 => "fourty",
                    50 => "fifty",
                    60 => "sixty",
                    70 => "seventy",
                    80 => "eighty",
                    90 => "ninety",
                    100 => "hundred");

// Create an array with additional values
$TextNumberHuge = array(      1000 => "thousand",
                           1000000 => "million",
                        1000000000 => "billion",
                     1000000000000 => "trillion",
                  1000000000000000 => "quadrillion");

// Output 0-999 on the screen
for ($i = 0; $i< 1000; $i++)
     echo "$i     :    ".NumberToText($i)."<BR>";

// This function divides the number into portions
// eg: 1'234'567'890 will be divided into:
//   1*1'000'000'000 + 234*1'000'000 + 567*1'000 + 890*1
function NumberToText($org_number) {

   global $TextNumber, $TextNumberHuge;

   // Value is less than 20 ? Pull the value from the array and quit
   if ($org_number < 20)
        return $TextNumber[$org_number];

   // get the value which is between 0-999 (use modulo)
   $string = NumberToTextSubPart(gmp_mod($org_number,1000));

   // Process the table with the thousand, million, billion, trillion, etc values
   foreach ($TextNumberHuge as $hnumber => $word) {
        // get the integer part of the number divided by 1'000, 1'000'000, etc
        // so we get first: 567 then 234 then 1 (looking on our example)
        $value = (int)($org_number/$hnumber);
        // Add the word to the string ONLY if it was bigger than 0
        // and add the string describing how big the number is:
        // eg: for 345*1'000 we would get "three hundred and fourty five"
        //     and we would add "thousands" to it
        // (the "s" part is added only if the value is greater than 1)
        if ($value > 0)
             $string = NumberToTextSubPart($value)." $word".(($value == 1) ? " " : "s ").$string;
   }
   return $string;
}


function NumberToTextSubPart ($tmpnumber) {

   global $TextNumber;

   $tmpstring = "";
   // Get number of ones in the number (eg for 345, $ones will be 5)
   $ones = gmp_mod($tmpnumber, 10);
   // Get the value for "tens" in the number (eg, for 345, $tens will be 40)
   $tens = gmp_mod($tmpnumber-$ones, 100);
   // Get the value for "hundreds" (eg, for 345, $hundreds will be 300)
   $hundreds = gmp_mod($tmpnumber-$tens-$ones,1000);

   // Values lower than 20 require special behaviour
   // since they are taken from the array
   if (($tens+$ones < 20) and ($tens+ones>0)) {
           $tmpstring =  $TextNumber[$tens+$ones];
   }else{
       // Add the ones into the string, eg, for 345 it would be "five"
       if ($ones > 0)
           $tmpstring =  $TextNumber[$ones];
       // Add tens to the string, eg, for 345 it would be "fourty"
       // so the string will be now "fourty five"
       if ($tens > 0)
           $tmpstring =  $TextNumber[$tens]." ".$tmpstring;
   }
   // Add hundreds to the string and add "and" part if we have any previous
   // value in the string, eg, for 345 it will be "three hundred"
   // so the string will be now "three hundred and fourty five"
   if ($hundreds > 0)
        $tmpstring = $TextNumber[$hundreds/100]." ".$TextNumber[100].(empty($tmpstring)? "":" and ".$tmpstring);

    // return the string to calling procedure
    return $tmpstring;
}

?>

Last edited by LuciWiz : 12-Dec-2005 at 06:50. Reason: Please insert your Php code between [php] & [/php] tags
  #5  
Old 12-Dec-2005, 00:03
balusss balusss is offline
Junior Member
 
Join Date: Dec 2005
Posts: 64
balusss is on a distinguished road

Re: Converting numbers to words


hi all,
i tried this program and this is particularly difficult when we try to convert the number to words in 'asian' method(!!!!) i.e., 211123- is pronounced as two lakh eleven thousand one hundred 'and' twenty three but not as 'two hundred and eleven thousand one hundred and twenty three.
there is a lot of difference you know. the 'and' in my program is very difficult to arrive at, particularly when either the given number is less than 100 or the hundred's place of the number is a zero. i stumbled there and quit.

do u have any suggestion? or u want me to be a little clearer.
  #6  
Old 12-Dec-2005, 00:40
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Converting numbers to words


Quote:
Originally Posted by balusss
hi all,
i tried this program and this is particularly difficult when we try to convert the number to words in 'asian' method(!!!!) i.e., 211123- is pronounced as two lakh eleven thousand one hundred 'and' twenty three but not as 'two hundred and eleven thousand one hundred and twenty three.
there is a lot of difference you know. the 'and' in my program is very difficult to arrive at, particularly when either the given number is less than 100 or the hundred's place of the number is a zero. i stumbled there and quit.

do u have any suggestion? or u want me to be a little clearer.
Officially, the "and" should not be used -- at least in English. 123 is not "one hundred and twenty three" but "one hundred twenty three". The "and" is used in numbers to designate the decimal point:
123.4 = "one hundred twenty three and four tenths".

So my suggestion is forget the "and".
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #7  
Old 12-Dec-2005, 06:44
balusss balusss is offline
Junior Member
 
Join Date: Dec 2005
Posts: 64
balusss is on a distinguished road

Re: Converting numbers to words


you are promptly right mr.walt.
but i think thousands of fin.instt.in asia still needs to convert to this type of conversion as yet.
thankq.
  #8  
Old 12-Dec-2005, 09:17
vazagothic vazagothic is offline
New Member
 
Join Date: Dec 2005
Posts: 2
vazagothic is on a distinguished road

Re: Converting numbers to words


I'm not a native english speaker myself - please excuse me for the improper use of the "and" part in the code

It shouldn't be too hard to modify the PHP code (if we're talking about it) to suit your needs. Just remove the "and" part and it should be fine then.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary number systems: BCD, twos complement, ones complement, etc. machinated Miscellaneous Programming Forum 6 08-Feb-2006 11:51
subscript error in coding warborules C Programming Language 6 27-Nov-2005 18:16
sorting numbers through array zidanefreak01 C++ Forum 3 26-Jun-2005 03:41
Linear Search eccoflame C Programming Language 3 19-Apr-2005 09:36
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:52.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.