Thread: seperate and add pos and neg numbers

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    1

    seperate and add pos and neg numbers

    I'm just starting out with C++ and have a problem for my class which requires the user to input 5 integers positive or negative then it is suppose to break it down by adding all of them, adding just the neg. and adding just the pos. and print each to the screen. I have the initial addition of all numbers fine and i've tried several different ways to get the neg and pos numbers to add on there own if anyone could help it would be much appreciated

    the output should be something like after entering: 3 8 -4 -6 0

    the total sum is: 1
    neg sum is: -10
    and pos sum is: 11

    like i said i have the output statments and all done i just haven't the slightest clue how to get neg sum and pos sum

    thanks

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I would imagine something along this line:
    Code:
       for ( i = 0; i < 5; ++i )
       {
          // get value
    
          if ( value < 0 )
          {
             sumneg += value;
          }
          else
          {
             sumpos += value;
          }
          totalsum += value;
       }
       // print results
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> i've tried several different ways to get the neg and pos numbers to add on there own
    Show what you tried that did not work (in [code][/code] tags) and tell us what output you got that is wrong. If you tried a bunch of things, show what you think is your best shot. Somebody can probably steer you in the right direction.

    Edit: Or just read the answer above.

Popular pages Recent additions subscribe to a feed