multiplication coding fault

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CyBerFirEZ
    New Member
    • Aug 2007
    • 48

    multiplication coding fault

    This code is meant to keep prompting for user input (integers), and when 0 is pressed it is meant to view the product of all the entered numbers apart from 0.
    It works fine, but when I hit 0 to view the product, it shows 0.
    Here's the code:



    Code:
    if (a == 10)
    {
    int l;
    int m;
    cout<< "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \n \n";
    cout << "Activated multiplying function... \n \n";
    while (1){
    cout << "Enter one number to multiply ( 0 to find total ): \n";
    cin >> l;
    m = m*l;
    if (l == 0)
    {
    cout << "PRODUCT:" << endl;
    cout << m << endl;
    cout << endl;
    cout<< "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \n \n";
    break;
    }
    }
    }
    Can anyone see the problem here, and how I can fix it?

    Thanks

    :P
  • Hunderpanzer
    New Member
    • Jul 2007
    • 60

    #2
    Originally posted by CyBerFirEZ
    This code is meant to keep prompting for user input (integers), and when 0 is pressed it is meant to view the product of all the entered numbers apart from 0.
    It works fine, but when I hit 0 to view the product, it shows 0.
    Here's the code:



    Code:
    if (a == 10)
    {
    int l;
    int m;
    cout<< "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \n \n";
    cout << "Activated multiplying function... \n \n";
    while (1){
    cout << "Enter one number to multiply ( 0 to find total ): \n";
    cin >> l;
    m = m*l;
    if (l == 0)
    {
    cout << "PRODUCT:" << endl;
    cout << m << endl;
    cout << endl;
    cout<< "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \n \n";
    break;
    }
    }
    }
    Can anyone see the problem here, and how I can fix it?

    Thanks

    :P

    I see your problem here. You are multiplying the breaking 0 with the total. Anything multiplied by 0 is 0. But anything multiplied by 1 , is the same thing. so try doing a bit of manipulation to the code : )

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by CyBerFirEZ
      Code:
      cin >> l;
      m = m*l;
      if (l == 0)
      {
      // display m ...
      }
      Can anyone see the problem here, and how I can fix it?
      Can't you see the problem? Suppose I enter 0, so zero is assigned to l ('ell')
      and m is multiplied by ell (i.e. zero (0)) ... and then ell is tested for zero.

      Somone already warned you for this logic mistake w.r.t. sentinel values but I
      bet you didn't even read it.

      kind regards,

      Jos

      Comment

      • CyBerFirEZ
        New Member
        • Aug 2007
        • 48

        #4
        so what i need to do is make it ignore the value 0 in the multiplication formula.
        How could I do that?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by CyBerFirEZ
          so what i need to do is make it ignore the value 0 in the multiplication formula.
          How could I do that?
          Don't multiply by ell when ell equals zero.

          kind regards,

          Jos

          Comment

          • CyBerFirEZ
            New Member
            • Aug 2007
            • 48

            #6
            and it is because I am using a previous code from a program that does the same thing but with addition.
            Now ive realized in addition, adding value 0 would give the same output
            anyway back to how to make it ignore value 0, see post above

            Comment

            • Hunderpanzer
              New Member
              • Jul 2007
              • 60

              #7
              Yeah I was just about to point out

              Killer42's Avatar
              Killer42
              Moderator
              5,108 Posts

              Yesterday
              10:09 PM
              #41

              Re: The addition problem
              Quote:
              Originally Posted by Hunderpanzer
              ...
              Code: ( cpp )

              1.
              while (1){
              2.
              cout << "Enter numbers to add ( 0 to total )";
              3.
              cin >> n;
              4.
              sum = sum + n;
              5.
              if (n == 0){
              6.
              cout << sum ;
              7.
              break;
              8.
              }

              Just as a general point, CyBerFirEZ. In this sort of situation it will usually make more sense to do your "if blah then break" processing before you add to your total. I'm aware that it doesn't make any practical difference in this case, since you're adding zero. It's just something I would recommend keep in mind for future loops. It's generally preferable to avoid any unnecessary processing, for reasons of efficiency among others.

              Comment

              • CyBerFirEZ
                New Member
                • Aug 2007
                • 48

                #8
                so then how do i make the forumla ignore the value 0?????

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by CyBerFirEZ
                  so then how do i make the forumla ignore the value 0?????
                  C and C++ do have an if statement and an optional else clause you know ...

                  kind regards,

                  Jos

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Expanbd your if statement.


                    If ell is 0, display the total else do the calculation.

                    BTW, you realize, I hope that in the Courier font, the number one and the letter ell are the same symbol making your code imposssible to read.

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by Hunderpanzer
                      Yeah I was just about to point out
                      Yep that was the tip; credits to Killer42 for the useful tip and you, Hunderpanzer
                      for retrieving it. thanks.

                      kind regards,

                      Jos

                      edit: after re-reading that tip text I'm not sure anymore *who* actually supplied
                      the tip ;-)

                      Comment

                      • Hunderpanzer
                        New Member
                        • Jul 2007
                        • 60

                        #12
                        Originally posted by JosAH
                        Jos

                        edit: after re-reading that tip text I'm not sure anymore *who* actually supplied
                        the tip ;-)

                        Haha?




                        Crazyfire the answer you need is already been said here. Just read slower, and think harder : )

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by Hunderpanzer
                          Haha?
                          No, no 'haha'; your post #7 doesn't clearly show who actually supplied the tip
                          and that's why I added my 'edit' paragraph, that's all.

                          kind regards,

                          Jos

                          Comment

                          • Hunderpanzer
                            New Member
                            • Jul 2007
                            • 60

                            #14
                            Originally posted by JosAH
                            No, no 'haha'; your post #7 doesn't clearly show who actually supplied the tip
                            and that's why I added my 'edit' paragraph, that's all.

                            kind regards,

                            Jos


                            I may be a bit confused. It said killer42 at the top ?

                            I'll just let it go :P

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by Hunderpanzer
                              I may be a bit confused. It said killer42 at the top ?

                              I'll just let it go :P
                              Yep, it said Killer42 at the top but it doesn't clearly tell where the (nested)
                              quote ends; and yep, I'll let go of it as well; thanks both of you anyway also
                              on behalf of the OP (I guess?)

                              kind regards,

                              Jos

                              ps. man this is a chaotic thread ...

                              Comment

                              Working...