# Help, positive and negative number sum...

Page 1 of 1

## 7 Replies - 22724 Views - Last Post: 20 June 2009 - 11:45 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=90863&amp;s=0dfa87e140d6f83364a7abce2c855709&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 xspockx

Reputation: 0
• Posts: 6
• Joined: 05-March 09

# Help, positive and negative number sum...

Posted 05 March 2009 - 04:47 PM

i need help with a part of my program that would allow me to sum up the amount of positives and negatives inputted into the program.
oh, btw, I know I'm missing the variables totalPos and totalNeg, there what im trying to find..

Here's what I have:

```#include <iostream>
using namespace std;

int main()
{
int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, sum_num;

cout << "Please Enter a Whole Number, positive or negative: ";
cin >>num1;
cout << "Please Enter a Whole Number, positive or negative: ";
cin >>num2;
cout << "Please Enter a Whole Number, positive or negative: ";
cin >>num3;
cout << "Please Enter a Whole Number, positive or negative: ";
cin >>num4;
cout << "Please Enter a Whole Number, positive or negative: ";
cin >>num5;
cout << "Please Enter a Whole Number, positive or negative: ";
cin >>num6;
cout << "Please Enter a Whole Number, positive or negative: ";
cin >>num7;
cout << "Please Enter a Whole Number, positive or negative: ";
cin >>num8;
cout << "Please Enter a Whole Number, positive or negative: ";
cin >>num9;
cout << "Please Enter a Whole Number, positive or negative: ";
cin >>num10;

sum_num = num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10;

cout << "You entered "<< totalPos <<" positive numbers";
cout << "and " << totalNeg <<" negative numbers";
cout <<"The sum of all numbers is " << sum_num;
cout <<"\n";
system("pause");
return 0;
}

```

This post has been edited by xspockx: 05 March 2009 - 04:56 PM

Is This A Good Question/Topic? 0

## Replies To: Help, positive and negative number sum...

### #2 OrganizedChaos

Reputation: 39
• Posts: 153
• Joined: 29-November 08

## Re: Help, positive and negative number sum...

Posted 05 March 2009 - 05:06 PM

totalPos and totalNeg will be counters. They should both be initialized to zero at the beginning of the program. Here's an example of how to use them:
```cout << "Please Enter a Whole Number, positive or negative: ";
cin >>num1;
if (num1 >= 0)
totalPos++;
else
totalNeg++;
...

```

Here's another way to do this program. You know you want to enter in 10 numbers, so make it a for loop.
```#include <iostream>
using namespace std;
int main()
{
int num, sum_num = 0, totalPos = 0, totalNeg = 0;
for (int x = 0; x < 10; x++){
cout << "Please Enter a Whole Number, positive or negative: ";
cin >> num;
sum_num += num;
if (num >= 0)
totalPos++;
else
totalNeg++;
cout << "\n";
}
cout << "You entered "<< totalPos <<" positive numbers";
cout << "and " << totalNeg <<" negative numbers";
cout <<"The sum of all numbers is " << sum_num;
cout <<"\n";
system("pause");
return 0;
}

```

As you can see, it's much shorter

This post has been edited by OrganizedChaos: 05 March 2009 - 05:07 PM

### #3 ericr2427

• D.I.C Regular

Reputation: 39
• Posts: 378
• Joined: 01-December 08

## Re: Help, positive and negative number sum...

Posted 05 March 2009 - 05:07 PM

Never mind.

This post has been edited by ericr2427: 05 March 2009 - 05:07 PM

### #4 xspockx

Reputation: 0
• Posts: 6
• Joined: 05-March 09

## Re: Help, positive and negative number sum...

Posted 05 March 2009 - 05:12 PM

Does that mean I have to add the if-else to each num?(ex num1, num2...to num10?)

OrganizedChaos, on 5 Mar, 2009 - 04:06 PM, said:

totalPos and totalNeg will be counters. They should both be initialized to zero at the beginning of the program. Here's an example of how to use them:
```cout << "Please Enter a Whole Number, positive or negative: ";
cin >>num1;
if (num1 >= 0)
totalPos++;
else
totalNeg++;
...

```

Here's another way to do this program. You know you want to enter in 10 numbers, so make it a for loop.
```#include <iostream>
using namespace std;
int main()
{
int num, sum_num = 0, totalPos = 0, totalNeg = 0;
for (int x = 0; x < 10; x++){
cout << "Please Enter a Whole Number, positive or negative: ";
cin >> num;
sum_num += num;
if (num >= 0)
totalPos++;
else
totalNeg++;
cout << "\n";
}
cout << "You entered "<< totalPos <<" positive numbers";
cout << "and " << totalNeg <<" negative numbers";
cout <<"The sum of all numbers is " << sum_num;
cout <<"\n";
system("pause");
return 0;
}

```

As you can see, it's much shorter

### #5 ericr2427

• D.I.C Regular

Reputation: 39
• Posts: 378
• Joined: 01-December 08

## Re: Help, positive and negative number sum...

Posted 05 March 2009 - 05:20 PM

No, because in the for loop, instead of using a separate variable for each number inputted, it uses the same one, and adds the number to sum_num and increments totalPos or totalNeg.

### #6 xspockx

Reputation: 0
• Posts: 6
• Joined: 05-March 09

## Re: Help, positive and negative number sum...

Posted 05 March 2009 - 05:33 PM

I think i understand most of what was going on..but I've been trying to figure out what the operator "++" does or what it means? I see that its in the variables as well as the if-else's, am I right in thinking that it "adds"?

### #7 ericr2427

• D.I.C Regular

Reputation: 39
• Posts: 378
• Joined: 01-December 08

## Re: Help, positive and negative number sum...

Posted 05 March 2009 - 05:37 PM

Yes, ++ adds one to the variable. It can be used as ++variable, which increments the variable and then does the operation, or variable++, which does the operation and then increments the variable.

### #8 sayha

Reputation: 0
• Posts: 1
• Joined: 20-June 09

## Re: Help, positive and negative number sum...

Posted 20 June 2009 - 11:45 PM

Dose this work on Dev C++?