1

So I'm trying to write a program that will read in ten whole numbers and outputs the sum of all numbers greater than zero, the sum of all numbers less than zero, and the sum of all numbers, whether positive, negative, or zero. Currently only the total sum and negative sum is adding correctly. My positive sum is always 1 or 0. Any tips to get me in the right direction would help.

Current Code:

#include <iostream>
using namespace std;

int main()
{
int N = 0;
int sum = 0;
int positiveSum = 0;
int negativeSum = 0;

cout << "Number 1" << endl;
cin >> N;
cout << "Number 2" << endl;
cin >> N;
cout << "Number 3" << endl;
cin >> N;
cout << "Number 4" << endl;
cin >> N;
cout << "Number 5" << endl;
cin >> N;
cout << "Number 6" << endl;
cin >> N;
cout << "Number 7" << endl;
cin >> N;
cout << "Number 8" << endl;
cin >> N;
cout << "Number 9" << endl;
cin >> N;
cout << "Number 10" << endl;
cin >> N;

for(int i=0;i<10;i++)
 {
    if (N >= 0 )
    {
      positiveSum += N;
    }
    else
    {
        negativeSum += N;
    }
 }
    sum = positiveSum + negativeSum;
       cout << "The positive sum is= " << positiveSum << endl;
       cout << "the negative sum is= " << negativeSum << endl;
       cout << "The total sum is= " << sum << endl;

return 0;

}
user4581301
  • 33,082
  • 7
  • 33
  • 54
unit5016
  • 29
  • 5
  • 1
    You read ten numbers into the same variable. That's not all that useful. Think on what happened to the first nine numbers. Where are they now? – user4581301 Sep 15 '20 at 00:27
  • You can read into the same number each time, but you have to be smart about it and perform the sifting and summing as you read in that number. – user4581301 Sep 15 '20 at 00:30
  • Ah ok, I did not realize I can make Number increase by itself without adding more cout and cin values. So I made ten different ones. – unit5016 Sep 15 '20 at 00:50
  • Don't make ten different numbers. Any time you find yourself making more than one of the same variable, it's doing the same job, and it will be used the same way, that nature telling you you want an array ([or a container](https://en.cppreference.com/w/cpp/container)). But in this case, there's no need for the array. asmmo's done almost exactly what I would have. Do what they did and keep using the same number in a loop. – user4581301 Sep 15 '20 at 00:53
  • consider accepting the answer if it solves your question. – asmmo Sep 16 '20 at 00:07

1 Answers1

2

After entering all Ns and when entring to the loop, you have only the last N. because after assigning the first number to N you don't process the value but you assigning the next value to N again after asking the user to enter it and so on. Use something like the following

#include <iostream>

int main()
{
    int N = 0;
    int sum = 0;
    int positiveSum = 0;
    int negativeSum = 0;
    
    for(int i=0;i<10;i++)
    {
        std::cout << "Number "<<i + 1<< std::endl;
        std::cin >> N;
        
        if (N >= 0 )
        {
            positiveSum += N;
        }
        else
        {
            negativeSum += N;
        }
    }
    sum = positiveSum + negativeSum;
    std::cout << "The positive sum is= " << positiveSum << std::endl;
    std::cout << "the negative sum is= " << negativeSum << std::endl;
    std::cout << "The total sum is= " << sum << std::endl;

    return 0;

}

And see Why is "using namespace std;" considered bad practice?

asmmo
  • 6,922
  • 1
  • 11
  • 25
  • I see, so the way I structured it causes the previous N value to be erased rather than store them all like I thought it would right? cout n, cin n, over and over. – unit5016 Sep 15 '20 at 00:40
  • @unit5016 exactly. `N` is an integer, so it can store only one integral value (the last one assigned to it). If u want to store all of them, use a container like `std::vector` – asmmo Sep 15 '20 at 00:41