Results 1 to 32 of 32

Thread: Can someone check this code to see If I could make it smaller

  1. #1

    Thread Starter
    Addicted Member SandmanSamR's Avatar
    Join Date
    Dec 2003
    Location
    Florida
    Posts
    131

    Can someone check this code to see If I could make it smaller

    I was told to make a program that makes the user input a numerical value of 1-999 and the computer prints out that number in letters (i.e "123" = "One hundred and twenty three"). This is the solution I came up with:
    VB Code:
    1. Dim valu As Integer
    2. Option Explicit
    3.  
    4.  
    5. Private Sub Command1_Click()
    6. lblConverted.Caption = ""
    7. valu = val(Text1.Text)
    8.  
    9. If valu >= 900 Then
    10.     valu = valu - 900
    11.     lblConverted.Caption = "Nine Hundred "
    12. ElseIf valu >= 800 Then
    13.     valu = valu - 800
    14.     lblConverted.Caption = "Eight Hundred "
    15. ElseIf valu >= 700 Then
    16.     valu = valu - 700
    17.     lblConverted.Caption = "Seven Hundred "
    18. ElseIf valu >= 600 Then
    19.     valu = valu - 600
    20.     lblConverted.Caption = "Six Hundred "
    21. ElseIf valu >= 500 Then
    22.     valu = valu - 500
    23.     lblConverted.Caption = "Five Hundred "
    24. ElseIf valu >= 400 Then
    25.     valu = valu - 400
    26.     lblConverted.Caption = "Four Hundred "
    27. ElseIf valu >= 300 Then
    28.     valu = valu - 300
    29.     lblConverted.Caption = "Three Hundred "
    30. ElseIf valu >= 200 Then
    31.     valu = valu - 200
    32.     lblConverted.Caption = "Two Hundred "
    33. ElseIf valu >= 100 Then
    34.     valu = valu - 100
    35.     lblConverted.Caption = "One Hundred "
    36. End If
    37.  
    38. If valu >= 90 Then
    39.     valu = valu - 90
    40.     lblConverted.Caption = lblConverted.Caption & "and Ninty-"
    41. ElseIf valu >= 80 Then
    42.     valu = valu - 80
    43.     lblConverted.Caption = lblConverted.Caption & "and Eighty-"
    44. ElseIf valu >= 70 Then
    45.     valu = valu - 70
    46.     lblConverted.Caption = lblConverted.Caption & "and Seventy-"
    47. ElseIf valu >= 60 Then
    48.     valu = valu - 60
    49.     lblConverted.Caption = lblConverted.Caption & "and Sixty-"
    50. ElseIf valu >= 50 Then
    51.     valu = valu - 50
    52.     lblConverted.Caption = lblConverted.Caption & "and Fifty-"
    53. ElseIf valu >= 40 Then
    54.     valu = valu - 40
    55.     lblConverted.Caption = lblConverted.Caption & "and Fourty-"
    56. ElseIf valu >= 30 Then
    57.     valu = valu - 30
    58.     lblConverted.Caption = lblConverted.Caption & "and Thirty-"
    59. ElseIf valu >= 20 Then
    60.     valu = valu - 20
    61.     lblConverted.Caption = lblConverted.Caption & "and Twenty-"
    62. End If
    63.  
    64. If valu >= 19 Then
    65.     lblConverted.Caption = lblConverted.Caption & "and Ninteen"
    66.  
    67. ElseIf valu >= 18 Then
    68.     lblConverted.Caption = lblConverted.Caption & "and Eighteen"
    69.  
    70. ElseIf valu >= 17 Then
    71.     lblConverted.Caption = lblConverted.Caption & "and Seventeen"
    72.  
    73. ElseIf valu >= 16 Then
    74.     lblConverted.Caption = lblConverted.Caption & "and Sixteen"
    75.  
    76. ElseIf valu >= 15 Then
    77.     lblConverted.Caption = lblConverted.Caption & "and Fifteen"
    78.  
    79. ElseIf valu >= 14 Then
    80.     lblConverted.Caption = lblConverted.Caption & "and Fourteen"
    81.  
    82. ElseIf valu >= 13 Then
    83.     lblConverted.Caption = lblConverted.Caption & "and Thirteen"
    84.  
    85. ElseIf valu >= 12 Then
    86.     lblConverted.Caption = lblConverted.Caption & "and Twelve"
    87.  
    88. ElseIf valu >= 11 Then
    89.     lblConverted.Caption = lblConverted.Caption & "and Eleven"
    90.  
    91. ElseIf valu >= 10 Then
    92.     lblConverted.Caption = lblConverted.Caption & "Ten"
    93.  
    94. ElseIf valu >= 9 Then
    95.     lblConverted.Caption = lblConverted.Caption & "Nine"
    96.  
    97. ElseIf valu >= 8 Then
    98.     lblConverted.Caption = lblConverted.Caption & "Eight"
    99.  
    100. ElseIf valu >= 7 Then
    101.     lblConverted.Caption = lblConverted.Caption & "Seven"
    102.  
    103. ElseIf valu >= 6 Then
    104.     lblConverted.Caption = lblConverted.Caption & "Six"
    105.  
    106. ElseIf valu >= 5 Then
    107.     lblConverted.Caption = lblConverted.Caption & "Five"
    108.  
    109. ElseIf valu >= 4 Then
    110.     lblConverted.Caption = lblConverted.Caption & "Four"
    111.  
    112. ElseIf valu >= 3 Then
    113.     lblConverted.Caption = lblConverted.Caption & "Three"
    114.  
    115. ElseIf valu >= 2 Then
    116.     lblConverted.Caption = lblConverted.Caption & "Two"
    117.  
    118. ElseIf valu >= 1 Then
    119.     lblConverted.Caption = lblConverted.Caption & "One"
    120.    
    121. End If
    122.  
    123.  
    124.  
    125. End Sub
    ';..;'
    Monsterous

    SANDMANSAMR
    SANDMANSAMR
    SANDMANSAMR
    SANDMANSAMR
    SANDMANSAMR

  2. #2
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    SandmanSamR

    Yeah Man, Put those things in an array and only reference them once.I will just give you the concept.
    VB Code:
    1. Arr1 = Array("","One","Two","Three",...,"Ninteen")
    2. Arr10 = Array("","Ten","Twenty","Thirty",...,"Ninety")
    3.  
    4. x100 = Int(value / 100)
    5. x10 =  Int((value - (x100 * 100)))
    6. x1 =  (value - (x100 * 100) - (x10 * 10))
    7.  
    8. Arr1(x100) "Hundred and " & Arr10 (x10) & " Dollars"
    You get the idea?
    Last edited by randem; Jan 19th, 2004 at 12:51 PM.

  3. #3

    Thread Starter
    Addicted Member SandmanSamR's Avatar
    Join Date
    Dec 2003
    Location
    Florida
    Posts
    131
    I just started vb this year. We have yet to get to arrays. Looking at that code though I say I have some understanding of whats going on. Would you mind summerizing so I can assure myself of what exaclty is going on so that I can use this if and when I need it in the future
    ';..;'
    Monsterous

    SANDMANSAMR
    SANDMANSAMR
    SANDMANSAMR
    SANDMANSAMR
    SANDMANSAMR

  4. #4
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    SandmanSamR

    OK, backing up...An array is just a place to hold a series of like data types where you can reference them by an index.

    From the code I gave you:

    Arr1(1) = "One"
    Arr1(2) = "Two" ....

    Arr10(1) = "Ten"
    Ar10(2) = "Twenty" ...

    So we can reference any item in the array by giving it's index to it. Now when taking the statement:

    x100 = Int(value / 100)

    with value equal to 525

    x100 = Int(525 / 100) which equals 5

    Now going to the array we get:

    Arr1(5) = "Five"

    And we do that for each value and array. Getting clearer?
    Last edited by randem; Jan 19th, 2004 at 12:51 PM.

  5. #5
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Wrong forum

  6. #6
    Fanatic Member
    Join Date
    Jun 2003
    Location
    IL
    Posts
    739
    Randem has the right idea, but you have to do a little more checking than that. Or else you would be printing "hundred and" even though you just entered 20 in the text box. Here is some code that works.

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim sNum(1 To 9) As String
    3.     Dim sOnes(1 To 9) As String
    4.    
    5.     Dim Hundred As Integer
    6.     Dim Ten As Integer
    7.     Dim One As Integer
    8.    
    9.     sNum(1) = "Eleven"
    10.     sNum(2) = "Twelve"
    11.     sNum(3) = "Thir"
    12.     sNum(4) = "Four"
    13.     sNum(5) = "Fif"
    14.     sNum(6) = "Six"
    15.     sNum(7) = "Seven"
    16.     sNum(8) = "Eigh"
    17.     sNum(9) = "Nine"
    18.    
    19.     sOnes(1) = "One"
    20.     sOnes(2) = "Two"
    21.     sOnes(3) = "Three"
    22.     sOnes(4) = "Four"
    23.     sOnes(5) = "Five"
    24.     sOnes(6) = "Six"
    25.     sOnes(7) = "Seven"
    26.     sOnes(8) = "Eight"
    27.     sOnes(9) = "Nine"
    28.    
    29.    
    30.     lblConverted.Caption = ""
    31.    
    32.     If Len(Text1) = 3 Then
    33.         Hundred = CInt(Left$(Text1, 1))
    34.         Ten = CInt(Mid$(Text1, 2, 1))
    35.         One = CInt(Right$(Text1, 1))
    36.     ElseIf Len(Text1) = 2 Then
    37.         Hundred = 0
    38.         Ten = CInt(Left$(Text1, 1))
    39.         One = CInt(Right$(Text1, 1))
    40.     ElseIf Len(Text1) = 1 Then
    41.         Hundred = 0
    42.         Ten = 0
    43.         One = Text1
    44.     Else
    45.         Hundred = 0
    46.         Ten = 0
    47.         One = 0
    48.     End If
    49.    
    50.     If Hundred > 0 Then
    51.         lblConverted = sOnes(Hundred) & " Hundred "
    52.         If Ten > 0 Or One > 0 Then
    53.             lblConverted = lblConverted & " and "
    54.         End If
    55.     End If
    56.    
    57.     If Ten > 0 Then
    58.         If Ten = 1 Then
    59.             Select Case One
    60.                 Case 1, 2
    61.                     lblConverted = lblConverted & sNum(One)
    62.                 Case Else
    63.                     lblConverted = lblConverted & sNum(One) & "teen"
    64.             End Select
    65.         Else
    66.             Select Case Ten
    67.                 Case 2
    68.                     lblConverted = lblConverted & "Twenty"
    69.                 Case Else
    70.                     lblConverted = lblConverted & sNum(Ten) & "ty"
    71.             End Select
    72.            
    73.             If One > 0 Then
    74.                 lblConverted = lblConverted & "-"
    75.             End If
    76.         End If
    77.     End If
    78.    
    79.    'This was the missing code. I don't know how that didnlt copy
    80.     If Ten <> 1 Then
    81.         If One > 0 Then
    82.             lblConverted = lblConverted & sOnes(One)
    83.         End If
    84.     End If
    85. End Sub

    [edit]
    Oops somehow I lost some of the code. Hold on.
    Last edited by Maldrid; Jan 19th, 2004 at 12:55 PM.
    Motto: Anything for a laugh.

    Getting second place only means you are the first loser to cross the finish line.

  7. #7

    Thread Starter
    Addicted Member SandmanSamR's Avatar
    Join Date
    Dec 2003
    Location
    Florida
    Posts
    131
    cool i get it. Quick question though, instead of
    VB Code:
    1. x100 = Int(525 / 100) which equals 5
    could you have done
    VB Code:
    1. x100 = (525 \ 100) which equals 5
    ?
    ';..;'
    Monsterous

    SANDMANSAMR
    SANDMANSAMR
    SANDMANSAMR
    SANDMANSAMR
    SANDMANSAMR

  8. #8
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    Maldrid

    That is what a concept is correct? I did not plan to do what seems like his homework for him.

  9. #9

    Thread Starter
    Addicted Member SandmanSamR's Avatar
    Join Date
    Dec 2003
    Location
    Florida
    Posts
    131
    Wrong forum

    How you figure?
    ';..;'
    Monsterous

    SANDMANSAMR
    SANDMANSAMR
    SANDMANSAMR
    SANDMANSAMR
    SANDMANSAMR

  10. #10
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    SandmanSamR

    Well sort of. It depends on how the x100 was defined. I like to take that out of the equation and alway return an integer. That way it always works no matter what numeric data type was defined.

    525 / 100 = 5.25

    or as an integer

    525 / 100 = 5

  11. #11
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by SandmanSamR
    Wrong forum

    How you figure?
    Uh.... look at your title, then look at the forums... you can figure it out.

  12. #12

    Thread Starter
    Addicted Member SandmanSamR's Avatar
    Join Date
    Dec 2003
    Location
    Florida
    Posts
    131
    I did not plan to do what seems like his homework for him.

    The original program I wrote was complete. I didnt need someone to finish it for me. I wanted to know if I could cut down on the repetitivness of it. Thanks for originally not showing me hard code and concept instead though, I would rather that then just shown what to copy and paste.
    ';..;'
    Monsterous

    SANDMANSAMR
    SANDMANSAMR
    SANDMANSAMR
    SANDMANSAMR
    SANDMANSAMR

  13. #13
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    SandmanSamR

    No offense, but lots of people attempt to get their homework done by unsuspecting members here. I saw what you were doing and using all those IF's is something a beginner would do.

    So, I thought it might be a homework assignment.

  14. #14

    Thread Starter
    Addicted Member SandmanSamR's Avatar
    Join Date
    Dec 2003
    Location
    Florida
    Posts
    131
    525 / 100 = 5.25

    or as an integer

    525 / 100 = 5



    thats why I wrote 525\100 not 525/100. I wrote \ instead of /
    ';..;'
    Monsterous

    SANDMANSAMR
    SANDMANSAMR
    SANDMANSAMR
    SANDMANSAMR
    SANDMANSAMR

  15. #15
    Fanatic Member
    Join Date
    Jun 2003
    Location
    IL
    Posts
    739
    Randem-

    You are absolutely right. Sorry if it sounded like I was trying to say you were wrong. I knew you were just giving him the concept not the exact answer.



    My previous code did not consider Ten. Here is the revised code.
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim sNum(1 To 9) As String
    3.     Dim sOnes(1 To 9) As String
    4.    
    5.     Dim Hundred As Integer
    6.     Dim Ten As Integer
    7.     Dim One As Integer
    8.    
    9.     sNum(1) = "Eleven"
    10.     sNum(2) = "Twelve"
    11.     sNum(3) = "Thir"
    12.     sNum(4) = "Four"
    13.     sNum(5) = "Fif"
    14.     sNum(6) = "Six"
    15.     sNum(7) = "Seven"
    16.     sNum(8) = "Eigh"
    17.     sNum(9) = "Nine"
    18.    
    19.     sOnes(1) = "One"
    20.     sOnes(2) = "Two"
    21.     sOnes(3) = "Three"
    22.     sOnes(4) = "Four"
    23.     sOnes(5) = "Five"
    24.     sOnes(6) = "Six"
    25.     sOnes(7) = "Seven"
    26.     sOnes(8) = "Eight"
    27.     sOnes(9) = "Nine"
    28.    
    29.    
    30.     lblConverted.Caption = ""
    31.    
    32.     If Len(Text1) = 3 Then
    33.         Hundred = CInt(Left$(Text1, 1))
    34.         Ten = CInt(Mid$(Text1, 2, 1))
    35.         One = CInt(Right$(Text1, 1))
    36.     ElseIf Len(Text1) = 2 Then
    37.         Hundred = 0
    38.         Ten = CInt(Left$(Text1, 1))
    39.         One = CInt(Right$(Text1, 1))
    40.     ElseIf Len(Text1) = 1 Then
    41.         Hundred = 0
    42.         Ten = 0
    43.         One = Text1
    44.     Else
    45.         Hundred = 0
    46.         Ten = 0
    47.         One = 0
    48.     End If
    49.    
    50.     If Hundred > 0 Then
    51.         lblConverted = sOnes(Hundred) & " Hundred "
    52.         If Ten > 0 Or One > 0 Then
    53.             lblConverted = lblConverted & " and "
    54.         End If
    55.     End If
    56.    
    57.     If Ten > 0 Then
    58.         If Ten = 1 Then
    59.             If One = 0 Then
    60.                 lblConverted = lblConverted & "Ten"
    61.             Else
    62.                 Select Case One
    63.                     Case 1, 2
    64.                         lblConverted = lblConverted & sNum(One)
    65.                     Case Else
    66.                         lblConverted = lblConverted & sNum(One) & "teen"
    67.                 End Select
    68.             End If
    69.         Else
    70.             Select Case Ten
    71.                 Case 2
    72.                     lblConverted = lblConverted & "Twenty"
    73.                 Case Else
    74.                     lblConverted = lblConverted & sNum(Ten) & "ty"
    75.             End Select
    76.            
    77.             If One > 0 Then
    78.                 lblConverted = lblConverted & "-"
    79.             End If
    80.         End If
    81.     End If
    82.    
    83.     If Ten <> 1 Then
    84.         If One > 0 Then
    85.             lblConverted = lblConverted & sOnes(One)
    86.         End If
    87.     End If
    88. End Sub
    Motto: Anything for a laugh.

    Getting second place only means you are the first loser to cross the finish line.

  16. #16
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    SandmanSamR

    Wasn't even aware that you could do that. But it would just seem to me that the notation used could confuse someone who overlooked or did not know that was possible.

  17. #17

    Thread Starter
    Addicted Member SandmanSamR's Avatar
    Join Date
    Dec 2003
    Location
    Florida
    Posts
    131
    lol, Maldrids code is almost as long as mine. I would sacrifice having 20 = "and twenty" for a smaller code, should I just go with randems? (Plus I'm getting confused reading maldrids code lol)
    ';..;'
    Monsterous

    SANDMANSAMR
    SANDMANSAMR
    SANDMANSAMR
    SANDMANSAMR
    SANDMANSAMR

  18. #18
    Fanatic Member
    Join Date
    Jun 2003
    Location
    IL
    Posts
    739
    Yea you can go with randems suggestion since you are more comfortable with it.
    Motto: Anything for a laugh.

    Getting second place only means you are the first loser to cross the finish line.

  19. #19
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    SandmanSamR

    Let me see your code when you are done. What you want to accomplish should take the most twenty lines.

  20. #20

  21. #21
    Addicted Member
    Join Date
    Apr 2003
    Posts
    170
    if i were you i'd code it like this:
    [sorry my code is not neat as i wrote here,
    not on vb]
    VB Code:
    1. num=563
    2.  
    3. dim x(9) as string
    4. dim y(9) as string
    5. dim z(9) as string
    6.  
    7. x(0) = ""
    8. x(1) = "one"
    9. x(2) = "two"
    10. '....
    11. '....
    12. x(9) = "nine"
    13.  
    14. y(0) = ""
    15. y(1) = ""
    16. y(2) = "twen"
    17. y(3) = "thir"
    18. '....
    19. '....
    20. y(9) = "nine"
    21.  
    22. 'dont need them :D
    23. 'dim i as long
    24. 'for i = 1 to 9
    25. 'zz(i) = xx(i) & " hundred"
    26. 'next i
    27.  
    28. 'now a 563 should be segmented to 5 / 6 / 3
    29. dim xx as byte, yy as byte, zz as byte
    30. xx = num mod 10
    31. yy = (num \ 10) mod 10
    32. zz = num \ 100
    33.  
    34. dim str as string
    35.  
    36. str = y(yy) & "ty " & x(xx)
    37.  
    38. 'special case treatment for yy=1, eg. 11, 12, 215
    39. if yy=1 then
    40. select case xx
    41. case 0: str="ten"
    42. case 1: str="eleven"
    43. case 2: str="twelve"
    44. case else: str = y(xx) & "teen"      'lines saved here ;)
    45. end select
    46. end if
    47.  
    48. if str = "ty " then    'special cases like: 100 , 200 , 300 ...etc
    49. str = x(zz) & " hundred"
    50. else
    51. str= x(zz) & " hundred and " & str
    52. end if
    Last edited by ZaidGS; Jan 26th, 2004 at 08:12 AM.

  22. #22
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    ZaidGS


    Trade 10 lines of code for all of that???? Why????

  23. #23
    Addicted Member
    Join Date
    Apr 2003
    Posts
    170
    Originally posted by randem
    ZaidGS


    Trade 10 lines of code for all of that???? Why????
    so u got a 10 line code ?! ha ?!

    my code is cleanest so far (i guess)
    Last edited by ZaidGS; Jan 24th, 2004 at 10:19 PM.

  24. #24
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    ZaidGS


    You wish, You should really actually read the posts.

  25. #25
    Addicted Member
    Join Date
    Apr 2003
    Posts
    170
    sorry your posts are too
    brief so that they become un-understandable,
    will you please tell me whats wrong ??!

  26. #26
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    ZaidGS

    Wrong???, Nothing is wrong. It's just way too much code for a simple task (expecially when you claim it's the shortest). The post is looking for short code, not just code that works. You code will turn out longer than the original posted code.

    The reason my post are short is they get to the point quicker.

  27. #27
    Addicted Member
    Join Date
    Apr 2003
    Posts
    170
    ok, i'll take off some lines

    if you dont believe me count lines :P
    u'll know mine is shorter, even without the
    lines i removed!!
    Last edited by ZaidGS; Jan 26th, 2004 at 08:21 AM.

  28. #28
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    ZaidGS

    Take a look here: Convert Dollar Amount into Words

    A simple project that does it all.

  29. #29
    Addicted Member
    Join Date
    Apr 2003
    Posts
    170
    wow, y its not posted here, i dont see it

  30. #30
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    ZaidGS

    Click on the link.

  31. #31
    Addicted Member
    Join Date
    Apr 2003
    Posts
    170
    i already clicked on the link, i just meant that
    its not been posted so i didnt know of its
    existance.
    sorry, if that was because of ignorance from me !!!

  32. #32
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    ZaidGS

    Oh, Sorry, I thought you meant you could not download it. I don't want to post everything I did here, unless asked for I guess. I could put it in the codebank, but this was the first request for something like this from the forum.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width