### Can't figure out what's wrong in my code?

The problem is:
Write a program that reads in ten whole numbers and that outputs the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative or zero), and the sum of all the numbers, whether positive, negative, or zero. The user enters the ten numbers just once each and the user can enter them in any order. Your program should not ask the user to enter the positive numbers and the negative numbers separately. Now modify this program so that it outputs the sum of all positive numbers, the average of all positive numbers, the sum of all non-positive numbers, the average of all non-positive numbers, the sum of all positive and non-positive numbers, and the average of all numbers entered.

So far, I have this:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273`` ``````#include 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; int positiveSum = 0, nonpositiveSum = 0, sum = 0; int positiveAverage = 0, nonpositiveAverage = 0, average = 0; // 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; cout << " The sum of all 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 << " The sum of all positive and nonpositive numbers: " << sum << endl; cout << " The average of all numbers entered: " << average << endl; // 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 = (sum_greater_than_zero + sum_less_than_zero); positiveAverage = (sum_greater_than_zero/ num_greater_than_zero); nonpositiveAverage = (sum_less_than_zero/ num_less_than_zero); average = ( sum/ 10 ); } // Ask the user to enter values: // that is, whole numbers. // Use if else statement: if (numb > 0) { sum_greater_than_zero += numb; num_greater_than_zero ++; } else if (numb < 0) { sum_less_than_zero += numb; num_less_than_zero ++; } cout << endl; return 0; } ``````

When the code executes the calculations come out as 0's. I don't know what I'm doing wrong. All answers are truly appreciated!
Last edited on
You initialize the results to 0 and never modify them before they are printed.
If I don't initialize the counters to zero I get an error message saying, "uninitialized local variable 'positiveCounter' used."
I don't mean you have to not initialize them. I mean that you have to do the calculations BEFORE you print them.
Oh, alright! Thank you so much!
Topic archived. No new replies allowed.