Number to Word (Billions)

This is a discussion on Number to Word (Billions) within the C Programming forums, part of the General Programming Boards category; Well, now we're even - because I don't understand the above program. I would throw this "fish" back, and go ...

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well, now we're even - because I don't understand the above program.

    I would throw this "fish" back, and go study the last program before this. That one was the way to go.

    This one is not a "keeper".

    You need an array of strings that will associate the word "two" (for instance), with the word[][] array row 2. Like the last program had.

    Then, when you are scanning through a number, and you come to the digit 2, you know where to find the right word - in word[2], which equals, LOOKY THERE, IT'S A MIRACLE!!, "two".

    There are other ways to do this, but they pretty much follow this same scheme or organization. Word[][] would be a 2 dimension char array with entries like: "zero", "one", "two", "three", "four", etc., which would correspond with word[0], word[1], word[2], word[3], word[4], in rows. (So word[0] is a whole row, not just one char like word[0][0], would be.)

    Let's look at word[0]:

    It has 6 subscripts (spaces), because "seven" has 5 letters, and then we need one char for the end of string char: '\0', so it's elevated to string status, not just a few char's.

    So word[0][0] == 'z';
    word[0][1] == 'e';
    word[0][2] == 'r';
    word[0][3] == 'o';
    word[0][4] == '\0';

    But - very sweetly, word[0] == "zero", (yes, the whole string!).

    That's what you need - that's what you need to study - not this last misguided effort.

    And if I were asking for help on an English help forum, I'd have the program in English, or some sub-titles in English. Obviously, you speak it well enough to do that.

    P.S.
    Ignore comma's. You can't rely on numbers always having them (2,000 does, but 2000 doesn't), and you don't need to do any special processing for them. The word to remember for comma's is "continue".
    Last edited by Adak; 01-25-2009 at 09:17 AM.

  2. #17
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    If you don't understand the version that uses tables instead of switch statements then you should give yourself more time to understand it. It will be far more productive for you to learn how to do it with tables than to use switch statements. Feel free to ask tons of questions about it.

    You will definitely spend at least as long trying to get it to work any other way. But if you spend the time learning to do it with tables you'll have gained so much more knowledge along the way. Trust me it will take less time to learn it than you think. Also your teacher will be that much happier grading a shorter program that you'll probably get a better mark.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #18
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Quote Originally Posted by Perspective View Post
    by the way, you should only have "and" after a decimal place. 3,485 should be "Three thousand four hundred eighty five" not "three thousand four hundred and eighty five".
    Only in US English.

  4. #19
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,170
    And Canadian English (no "and").

  5. #20
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Wow, I had no idea any cultures omitted the 'and'.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #21
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by Brian View Post
    Only in US English.
    "and" marks the decimal place. In what country would you say "four hundred and eighty two dollars and eighty six cents" ? That's just bad english.

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Perspective View Post
    "and" marks the decimal place. In what country would you say "four hundred and eighty two dollars and eighty six cents" ? That's just bad english.
    That's what you do in England, except it would be "four hundred and eighty two pounds and eighty six pence". At least if you speak "proper English".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    I think that myphilosofi should follow whatever were the given requirements.

    Quote Originally Posted by Perspective
    "and" marks the decimal place. In what country would you say "four hundred and eighty two dollars and eighty six cents" ? That's just bad english.
    The problem is that you first propose a rule ("and" marks the decimal place), but the reasoning does not support the rule, except in the case where there is a decimal place. In that sense I see no wrong in rendering the amount as "three thousand, four hundred and eighty five dollars". It also seems correct to render $482.86 as "four hundred and eighty two dollars, and eighty six cents".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by laserlight View Post
    I think that myphilosofi should follow whatever were the given requirements.


    The problem is that you first propose a rule ("and" marks the decimal place), but the reasoning does not support the rule, except in the case where there is a decimal place. In that sense I see no wrong in rendering the amount as "three thousand, four hundred and eighty five dollars". It also seems correct to render $482.86 as "four hundred and eighty two dollars, and eighty six cents".

    I don't see how the reasoning doesn't support the rule, but anyway, saying "A and B, and C" is in general bad grammar. It should be "A, B, and C".

  10. #25
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Perspective View Post
    I don't see how the reasoning doesn't support the rule, but anyway, saying "A and B, and C" is in general bad grammar. It should be "A, B, and C".
    That may be if we simply talk about grammar in generic terms. However, if we talk about "reading numbers out loud", then the proper British English way to say 123 is "One hundred and twenty three". If you want to say "Between 123 and 125", then, grammar or not, you should say "Between one hundred and twenty three and one hundred and twenty five".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Quote Originally Posted by Perspective
    I don't see how the reasoning doesn't support the rule, but anyway, saying "A and B, and C" is in general bad grammar. It should be "A, B, and C".
    I may be something of an amateur C and C++ language lawyer, but I am definitely not an English language lawyer, so I cannot substantiate why I think that "A and B, and C" is valid

    Fortunately, I have a way to substantiate my first claim. We can agree that "A, B, and C" is valid. So, we can agree that "four hundred, eighty two dollars, and eighty six cents" is valid. Likewise, we can agree that "one thousand, four hundred, and eighty two dollars" is valid. That implies that the reasoning behind the '"and" marks the decimal place' rule is flawed, unless you propose that "A, B, and C" is invalid in this case.
    Last edited by laserlight; 01-26-2009 at 09:31 AM. Reason: agree -> propose
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #27
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >Likewise, we can agree that "one thousand, four hundred, and eighty two dollars" is valid.

    But I don't agree with this point. I think it should be "one thousand four hundred eighty two dollars". So my rule isn't violated by what I've claimed (in this case it's not "A, B, and C", it's only "A"). I guess it's different in british english.

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Quote Originally Posted by Perspective
    I guess it's different in british english.
    Yes, it seems that that is the case.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #29
    Registered User
    Join Date
    Jan 2009
    Posts
    22

    Question Hey, it's me again

    one of my classmates said to use "atol"

    does anyone have any idea how and why?

    thanks!

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Quote Originally Posted by myphilosofi
    one of my classmates said to use "atol"

    does anyone have any idea how and why?
    Your classmate probably thought that using atol() to convert the numeric string input to a long int would allow the method you originally wanted to use to work. The problem of removing the commas remain, and you still have the account for the decimal place. Oh, and as before, the range that you are required to handle exceeds the guaranteed range of a long, so the approach is not guaranteed to work in the first place.

    Personally, I feel that just handling it as a numeric string is best.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  4. Assembly Tutorials
    By JoshR in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 06-11-2005, 09:56 AM
  5. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 05:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21