code not working

Discussion in 'C' started by Gura_Kid, Aug 28, 2009.

  1. Gura_Kid

    Gura_Kid New Member

    Joined:
    Feb 17, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I'm trying to solve TOO + TOO + TOO + TOO = GOOD using a c++ program.

    My solution to this, is to use a nested loop for each unique letter (in this case T, O, G, D). The loops would systematically assign the digits from 0-9 to each letter.

    However the following program gives me the wrong results :crazy:


    Code:
    [COLOR=#008200]#include <iostream>[/COLOR]
    [B][COLOR=#006699]using[/COLOR][/B] [B][COLOR=#006699]namespace[/COLOR][/B] std[COLOR=#999900];[/COLOR]
    
    [B][COLOR=#006699]int[/COLOR][/B] main[COLOR=#999900]()[/COLOR] [COLOR=#999900]{[/COLOR]
       [B][COLOR=#006699]int[/COLOR][/B] T[COLOR=#999900],[/COLOR] O[COLOR=#999900],[/COLOR] G[COLOR=#999900],[/COLOR] D[COLOR=#999900];[/COLOR]
       [B][COLOR=#006699]for[/COLOR][/B] [COLOR=#999900]([/COLOR]T[COLOR=#999900]=[/COLOR][COLOR=#006666]0[/COLOR][COLOR=#999900];[/COLOR]T[COLOR=#999900]<=[/COLOR][COLOR=#006666]9[/COLOR][COLOR=#999900];[/COLOR]T[COLOR=#999900]++)[/COLOR] [COLOR=#999900]{[/COLOR]
          [B][COLOR=#006699]for[/COLOR][/B] [COLOR=#999900]([/COLOR]O[COLOR=#999900]=[/COLOR][COLOR=#006666]0[/COLOR][COLOR=#999900];[/COLOR]O[COLOR=#999900]<=[/COLOR][COLOR=#006666]9[/COLOR][COLOR=#999900];[/COLOR]O[COLOR=#999900]++)[/COLOR] [COLOR=#999900]{[/COLOR]
             [B][COLOR=#006699]for[/COLOR][/B] [COLOR=#999900]([/COLOR]G[COLOR=#999900]=[/COLOR][COLOR=#006666]0[/COLOR][COLOR=#999900];[/COLOR]G[COLOR=#999900]<=[/COLOR][COLOR=#006666]9[/COLOR][COLOR=#999900];[/COLOR]G[COLOR=#999900]++)[/COLOR] [COLOR=#999900]{[/COLOR]
                [B][COLOR=#006699]for[/COLOR][/B] [COLOR=#999900]([/COLOR]D[COLOR=#999900]=[/COLOR][COLOR=#006666]0[/COLOR][COLOR=#999900];[/COLOR]D[COLOR=#999900]<=[/COLOR][COLOR=#006666]9[/COLOR][COLOR=#999900];[/COLOR]D[COLOR=#999900]++)[/COLOR] [COLOR=#999900]{[/COLOR]
                   [B][COLOR=#006699]if[/COLOR][/B] [COLOR=#999900]([/COLOR] [COLOR=#999900]([/COLOR]T[COLOR=#999900]*[/COLOR][COLOR=#006666]100[/COLOR] [COLOR=#999900]+[/COLOR] O[COLOR=#999900]*[/COLOR][COLOR=#006666]10[/COLOR] [COLOR=#999900]+[/COLOR] O[COLOR=#999900])[/COLOR] [COLOR=#999900]*[/COLOR] [COLOR=#006666]4[/COLOR] [COLOR=#999900]==[/COLOR] [COLOR=#999900]([/COLOR]G[COLOR=#999900]*[/COLOR][COLOR=#006666]1000[/COLOR][COLOR=#999900])+([/COLOR]O[COLOR=#999900]*[/COLOR][COLOR=#006666]100[/COLOR][COLOR=#999900])+([/COLOR]O[COLOR=#999900]*[/COLOR][COLOR=#006666]10[/COLOR][COLOR=#999900])+[/COLOR]D[COLOR=#999900])[/COLOR] [COLOR=#999900]{[/COLOR]
                      cout [COLOR=#999900]<<[/COLOR] [COLOR=#646464]"T="[/COLOR] [COLOR=#999900]<<[/COLOR] T [COLOR=#999900]<<[/COLOR] [COLOR=#646464]"\nO="[/COLOR] [COLOR=#999900]<<[/COLOR] O [COLOR=#999900]<<[/COLOR] [COLOR=#646464]"\nG="[/COLOR] [COLOR=#999900]<<[/COLOR] G [COLOR=#999900]<<[/COLOR] [COLOR=#646464]"\nD="[/COLOR] [COLOR=#999900]<<[/COLOR] D [COLOR=#999900]<<[/COLOR] endl[COLOR=#999900];[/COLOR]
                   [COLOR=#999900]}[/COLOR]
                [COLOR=#999900]}[/COLOR]
             [COLOR=#999900]}[/COLOR]
          [COLOR=#999900]}[/COLOR]
       [COLOR=#999900]}[/COLOR]
       cout [COLOR=#999900]<<[/COLOR] [COLOR=#646464]"Press <ENTER> to exit. "[/COLOR][COLOR=#999900];[/COLOR]
       cin[COLOR=#999900].[/COLOR][B][COLOR=#006699]get[/COLOR][/B][COLOR=#999900]();[/COLOR]
       [B][COLOR=#006699]return[/COLOR][/B] [COLOR=#006666]0[/COLOR][COLOR=#999900];[/COLOR]
    
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    How are the results wrong?
    What results did you get, and what results did you expect?
    Is a requirement that T,O,G,D are all unique? If so, then you haven't coded this into your program.
    Is a requirement that neither of TOO and GOOD begin with zero? Similarly you haven't coded that in.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice