# what am i doing wrong?

• 09-12-2003
jlmac2001
what am i doing wrong?
This program is suppose to :

Prompt the user to enter any number of integers. Keep prompting the user to input integers until the input is 0, which is the signal to stop entering numbers. After the inputs are finished, print out the following information: how many positive numbers were entered, how many negative numbers were entered, the sum of the numbers entered, and the decimal average of the numbers entered.

Sample run: (user input underlined)

Input integer (0 to stop) 12
Input integer (0 to stop) 4
Input integer (0 to stop) -1
Input integer (0 to stop) -5
Input integer (0 to stop) 18
Input integer (0 to stop) 0
# of positives = 3
# of negatives = 2
Total = 28
Average = 5.6

[code]

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main ()

{

int integer;
int positives = 1;
int negatives = 1;
int total = 0;
double average = 0;

cout << "Input integer (0 to stop): " ;
cin >> integer;

while ( integer != 0)
{
total = total + integer;
average = ;// how would i compute the average?
cout << "Input integer (0 to stop): " ;
cin >> integer;

}

if ( integer >= 1)
positives += 1;
cout << " # of positives = " << positives << endl;

if (integer < 1)
negatives += 1
cout << " # of negatives = "<< negatives << endl;

cout << "total = " << total << endl;
cout << "average = " << average << endl;

return 0;

}
• 09-12-2003
Sebastiani
Well since the comparison of positive/negative occurs OUTSIDE of the loop, how do you expect to get an accurate count?

An average is the total divided by the amount of numbers gathered. So count how many times you've looped.
• 09-12-2003
jlmac2001
one more question
If I don't know how many numbers that I will use, how will I know what number to divide the average by? The loop is infinite!
• 09-12-2003
Mithoric
Quote:

// how would i compute the average?
Average is just the total sum divided by number of entries.

Example series:
1,2,3,4,5

total: 15
average: total/5 = 3
• 09-12-2003
Mithoric
Code:

```int count; while ( integer != 0) { total = total + integer; average = ;// how would i compute the average? cout << "Input integer (0 to stop): " ; cin >> integer; count++; }```
• 09-12-2003
Sebastiani
Quote:

Originally posted by Mithoric
Code:

```int count; while ( integer != 0) { total = total + integer; average = ;// how would i compute the average? cout << "Input integer (0 to stop): " ; cin >> integer; count++; }```

Don't forget to initialize count to zero.

Finally, while counting activities should occur INSIDE the loop, averaging should obviously be done OUTSIDE of it.
• 09-12-2003
Mithoric
:| didn't even see that the average (or where it would be calculated) was in the loop.
• 09-13-2003
jlmac2001
i did it myself
I figured out how to get the average and my program is working great. Thanks.