Help with Simple Looping Program

Ask the user how many data numbers to input, (c&d on input failure). For each input of a data number, prompt the with a 1, 2, 3, ... depending on which value we're inputting. Assume that the data numbers may have fractional parts (so use double to hold the data value). C&d if the input of a data number fails. We're interested in the average of the positive data numbers only. If there are no positive data numbers, just output something like
no positive values
but if there are any positive data numbers, output their average.
An example run of your program (with user input in bold) might go as
how many #'s? 5
#1: 1.1
#2: -100
#3: 2.2
#4: -200
#5: 3.3
average positive is 2.2
(Notice how the prompts remind the user which number he's entering.)

The problem wants the program to output the average of the entered positive values only, ignoring the negative numbers inputted. But i can't seem to come up with a possible if statement to do so. Help please!

Here is my code so far:
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;

bool die(const string & msg);

int main(){
int limit; // store number of data points
double number; // variable to store the number
double sum; // variable to store the sum
int counter; // loop control variable
int entry;
cout << "How many numbers? ";
cin >> limit || die(" input failure ");
if (limit < 0) die(" limit cannot be negative ");
sum = 0;
counter = 0;
number = 0;
entry = 0;

while (counter < limit && entry < limit ){
entry++;
cout << "#" << entry << ": "; cin >> number || die(" input failure ");
if (number >= 0)
sum = sum + number;
counter++;
}
cout << "The sum of the " << limit << " numbers = " << sum << endl;

if (counter != 0)
cout << "The average positive is " << sum / counter << endl;

}

bool die(const string & msg){
cout << "Fatal error: " << msg << endl;
exit(EXIT_FAILURE);
}
Last edited on
You should increment the counter only when the number is greater than zero. (Hint: you've got the right if statement already, and you've got the right code. All you need to do is add some braces in the right places!).
So there is nothing to add into my code besides proper placement of more braces?
I was thinking of a way for the program to exclude the negative entries in the counter (as the counter in the program is including every value entered, both positive and negative) when calculating the average (thus somehow implementing another if statement?) but where exactly would i place more braces if I do not need to do that?
Last edited on
You're right about excluding negative entries from the counter. This is done easily by only incrementing the counter when the value entered is greater than zero. You already have this if statement in your code, but it only adds to sum and leaves counter high and dry to be incremented every time your loop executes. So your current code:

1
2
3
4
5
6
7
8
9
while (counter < limit && entry < limit ) {
    entry++;
    cout << "#" << entry << ": "; cin >> number || die(" input failure ");

    if (number >= 0)
        sum = sum + number;

    counter++;
}


Needs to be adapted to this:
1
2
3
4
5
6
7
8
9
10
while (counter < limit && entry < limit ) {
    entry++;
    cout << "#" << entry << ": "; cin >> number || die(" input failure ");

    if (number >= 0)     //the following code is only for super, special positive values...
    { //added brace
        sum = sum + number;
        counter++;         //...meaning counter excludes negatives
    } //added brace
}


That fixes your problem right up.

I would also suggest:
(1) removing the condition counter < limit. The while loop is only supposed to execute for so many entries. counter keeps track of positive entries, not total entries, so it doesn't belong in the condition.

(2) renaming your counter variable to something more descriptive like "postiveCounter". That makes it easier to discern the purpose of your current variables entry and counter.

[Edits for formatting and a bit of language change]
Last edited on
Ohhhh OK got it! Thank you so much for the help and explanation!!!
Topic archived. No new replies allowed.