Thread: Changing numbers to words

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    2

    Question Changing numbers to words

    In my programming class, we were recently given an assignment where a paycheck is printed out. The prof mentioned that if you wanted to, you could also print out not only the total of the check, but the words as well.(ie..$157.00 -one hundred fifty seven dollars).With this assignment, we were given 3 files with a list of employees,a list of hours worked for some employees(only those employees that worked are included in the final paychecks issued), and taxes deducted from the paychecks.For the employees that had a paycheck , the gross,deductions and net pay were printed.I made a check for the employees with the actual numerical value.I know that this may not make much sense to anyone, but if you have any suggestions on how to change the actual numbers to text it would be greatly appreciated.(btw-this program included pointers,arrays,files if that is of any help).

    Cheers!

  2. #2
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    You want to format a float into currency?

    printf("$%.2f", foo);
    pointer = NULL

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    >> Changing numbers to words <<
    What you want to do is loop through the numbers and make the string. A working (probably) example would be:
    Code:
    /*----------------struct for linked list----------------------*/
    struct num {
    int num;
    struct num *next_num;
    };
    /*--------------------------------------------------------------*/
    char numword[50];
    int i;
    
    printf("Enter your number(s): ");
    fgets(numword, sizeof(numword), stdin);
    numword[strlen(numword)-1] = '\0';
    
    struct num *temp, *head;
    temp->next_num = NULL;
    head->next_num = NULL;
    
    for (i = 1; i <= strlen(numword); i++)
    {
        if (numword[i] != '\0')
        {
            temp->num = numword[i];
            temp->next_num = NULL;
    
            head->next_num = temp;
            head = head->next_num;
        }
    
        else 
            break;
    }
    This code is not tested! But, logically, it is correct. Use a linked list. It is the easiest way.

    --Garfield the Programmer
    1978 Silver Anniversary Corvette

  4. #4
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    Nonono, you all have it wrong from what I understand...

    Let's say the check is for $150, well then they want "One hundred and fifty dollars" to print out along with that.

    I'm not quite sure what to do here...maybe you could put the number into an array and try something like...

    while(num[i] != '\0')
    {
    case 1:
    strcat(numstring, "One ");
    i++;
    break;
    case 2:
    strcat(numstring, "Two ");
    i++;
    break;

    //etc....
    }

    but the problem with that is it would print out "One Five Zero" instead of "One hundred and fifty"

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    float num;

    int flag = 0;

    char szNum[100];

    itoa(num, szNum, 10); // convert the whole numbers to string

    int len = strlen(szNum);

    if(len == 0) /// ERROR!

    else if(len == 1) flag = 1;/// must be less than $10

    else if(len == 2) flag = 2/// must be $10 - $99 dollars

    etc, etc.

    Then, since you know the range, you can set up routines for all the possibilities.

    switch(flag)
    {
    case 1:

    switch(atoi(szNum[0]))

    {
    case '1':

    strcpy(output, "One ");
    strcat(output, "Dollar");

    //etc, etc



    There are of course much more efficient ways to do it!
    But since you posted no code...
    see if you can make something work from that?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    I'm thinking the only way to do this is sloppy. You know, if (iNum[1] > 0 && iNum[1] <= 9), kind of programming. That seems to be the only solution.

    --Garfield
    1978 Silver Anniversary Corvette

  7. #7
    Registered User goran's Avatar
    Join Date
    Sep 2001
    Posts
    68
    there is a solution to the problem pointed out by KEN.
    if the digit to be converted is 0 then do not consider that 0 "for word conversion". For eg, $151 gets converted to one hundred fifty one but $150 would be one hundred and fifty, since we wont be considering the last 0. similarly $1500 would be converted to one thousand five hundred since the last 2 0's wouldn't be considered. and in a similar way $1501 would be one thousand five hundred one. since the 0 in the ten's place will not considered.

    cheers,
    I don't wait for the future; it comes soon enough.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    - lmov

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The night of my last reply, I went home and wrote a function which accomplished the task in about 300 lines of code. I am happy to report that, no longer being a newbie, it took me one hour to write it! Though it does contain some bugs, it otherwise works well.

    I'll explain a little of how I approached it:

    -First, if the input is a string, erase all whitespace.

    -Find the length of the string. This will tell you right away the range of the number.

    -Start backwards. That is, assemble the output string starting with the largest values.

    -Use strcat frequently!

    There will be several tricky manipulations. For instance, when you are processing, say, the "tens" place, you might need to do something like:

    Code:
    switch(input[i])
    {
    case '9': strcat(output, "Ninety ");
    break;
    ///continue down the line
    case '1': 
       
       ones_place_flag = 0;   /* <--- make sure we don't get a string   that ends up like : " Eighteen Eight Dollars" !!! */
        switch(input[i+1])
     {
        case '9': strcat(output, "Nineteen ");
        break;
    // continue on down

    ...So that when you get to the ones place, you can use the "ones_place flag":

    Code:
    if(ones_place_flag != 0)
    {
    switch(input[i])
    {
    case '9': strcat(ouput, "Nine ");
    break;
    /// And so on...

    You will probably use several of these types of flags....


    Anyway, I hope that helped some. Let me know how it goes...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Sayeh
    Guest

    creativity

    This is an excellent small exercise for everyone-- it displays one of the fundamental aspects of programming and experience, which go hand in hand-

    the more experience you have the more varied your approach can be...

    I would have approached this using a variable to tell me what place I'm at (1's, 10's, 100's, etc.), and a table of strings corresponding to the position.

    Then simply use the placeholder and value as an index into the table...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 06:28 AM
  2. Converting Numbers to Words
    By denizengt in forum C Programming
    Replies: 20
    Last Post: 11-05-2003, 09:19 PM
  3. converting numbers into words
    By Kozam in forum C Programming
    Replies: 2
    Last Post: 09-30-2003, 07:49 AM
  4. C++ program help(converting numbers to english words)
    By bama1 in forum C++ Programming
    Replies: 4
    Last Post: 10-08-2002, 01:17 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM