I'm new to C++ and I'm having so much trouble with this problem. I honestly don't know what I'm doing wrong, I've tried everything but I still can't seem to make the program work. This is the problem:

Write a program that reads in ten whole numbers and that outputsthe sum of all the numbers greater than zero, the sum of all thenumbers less than zero (which will be a negative or zero), and thesum of all the numbers, whether positive, negative, or zero. Theuser enters the ten numbers just once each and the user can enterthem in any order. Your program should not ask the user to enterthe positive numbers and the negative numbers separately. Now, modify this program so that it outputs the sum of all positivenumbers, the average of all positive numbers, the sum of allnon-positive numbers, the average of all non-positive numbers, thesum of all positive and non-positive numbers, and the average ofall numbers entered.

This is what I have:

#include <iostream>
    using namespace std;


    int main ()
    { 

    // Set four counters to zero that is,
    // sum_greater_than_zero, num_greater_than_zero, sum_less_than_zero, num_less_than_zero.
    int numb =0 ;
    int sum_greater_than_zero = 0, num_greater_than_zero = 0, sum_less_than_zero = 0, num_less_than_zero = 0;
    int positiveSum= 0, nonpositiveSum=0;
    int positiveAverage=0, nonpositiveAverage=0;
    int counter= 10;


    // Use a for loop done 10 times:
    for (int i = 0; i < 10; i++)  
    {
    cout << "Please enter any number that you'd like. \n ";
    cin >> numb;
    }



    // Use if else statement:

    if (numb > 0)
    {

    sum_greater_than_zero += num_greater_than_zero ;

    }

    else if (numb < 0 )
    {

    sum_less_than_zero += num_less_than_zero; 

    }

    counter ++;


    // Average of numbers: 
    //(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) =(1+2+3+4+5+6+7+8+9+10)/10=5.5
    //

    positiveSum = (sum_greater_than_zero + sum_less_than_zero);
    nonpositiveSum = (num_greater_than_zero + num_less_than_zero);
    positiveAverage = (sum_greater_than_zero/num_greater_than_zero);
    nonpositiveAverage = (sum_less_than_zero/num_less_than_zero);

    {

    cout << " The sum of positive numbers: " << positiveSum << endl;

    cout << " The average of positive numbers: " << positiveAverage << endl ;

    cout << " The sum of nonpositive numbers: " << nonpositiveSum << endl ;

    cout << " The average of nonpositive numbers: " << nonpositiveAverage << endl ;

    }
    cout << endl;

    return 0;
    }

All help is truly appreciated!

All your processing of the input value needs to be inside that first for loop.

When categorizing the numbers, all you need do is test for > 0, then use an else to handle everything else that must be 0 or less.

Where you add the positives and the negatives, you must also increment the respective counters.

What you add to the sum variables is the recently input number.

Once the loop has finished, and you've added up and counted the positives and negatives, you can then use those sums and counts to find the averages.

Work on it some more, post your corrected code.

Ok, I tried this.. however, I can't seem to make the counters work:
Anyway, I'll try to work it out on my own. Thank you so much for your help!

#include <iostream>
using namespace std;


int main ()
{ 

// Set four counters to zero that is,
// sum_greater_than_zero, num_greater_than_zero, sum_less_than_zero, num_less_than_zero.
int numb;
int sum_greater_than_zero = 0, num_greater_than_zero = 0, sum_less_than_zero = 0, num_less_than_zero = 0, sum;
int positiveSum= 0, nonpositiveSum=0;
int positiveAverage=0, nonpositiveAverage=0, average;
int counter= 0; int positiveCounter = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10), negativeCounter = ((-1,-2, -3, -4, -5, -6, -7, -8, -9, -10));


// Use a for loop done 10 times:
for (int i = 0; i < 10; i++) 
{
cout << "Please enter any number that you'd like. \n ";
cin >> numb;
// Average of numbers: 
//(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) =(1+2+3+4+5+6+7+8+9+10)/10=5.5
//

sum = (positiveSum + nonpositiveSum); 
positiveAverage = (positiveSum/ positiveCounter);
nonpositiveAverage = (nonpositiveSum/ negativeCounter);
average= sum/counter;


cout << " The sum of positive numbers: " << positiveSum << endl;

cout << " The average of positive numbers: " << positiveAverage << endl ;

cout << " The sum of nonpositive numbers: " << nonpositiveSum << endl ;

cout << " The average of nonpositive numbers: " << nonpositiveAverage << endl ;


}


// Ask the user to enter values:
// that is, whole numbers.


// Use if else statement:

if (numb > 0)
{

nonpositiveSum+= numb;
negativeCounter ++;

}

else if (numb < 0 )
{

positiveSum += numb; 
positiveCounter++;

}



cout << endl;

return 0;
}

Is this for homework?

Is this for homework?

I would suspect so. At least they are making a reasonable effort to solve the problem, so helping them (at least as far as pointing out their errors) is not unreasonable, IMHO.

Personally, I find the wording of the question somewhat ambiguous. Hopefully this will help you.

#include <iostream>

using namespace std;

int main()
{
    int posCount = 0;
    int negCount = 0;
    int posSum = 0;
    int negSum = 0;
    int numb;

    cout << "Please enter any combination of ten positive\nor negative integers separated by whitespace, then press Enter:\n";
    for (int i = 0; i < 10; i++)
    {
        cin >> numb;
        if ( cin.fail() )
        {
            cin.clear();
            cin.ignore(90, '\n');
            --i;
            continue;
        }

        if ( numb > 0)
        {
            ++posCount;
            posSum += numb;
        }
        else
        {
            ++ negCount;
            negSum += numb;
        }
    }
    // simple input stream flush
    cin.ignore(90, '\n');

    cout << "\nThe sum of all positive integers = " << posSum << endl;
    cout << "The sum of all negative integers = " << negSum << endl;
    cout << "The sum of all integers = " << posSum + negSum << endl;
    // calculate the rest

    cout << "\n\npress Enter to exit...";
    cin.get();

    return 0;
}

Thank you so much everyone for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.