Sentinel controlled loop

My program is supposed to accept a list of positive numbers and compute the average once the sentinel, -1, is entered. The problem I'm having is that it is adding the sentinel, or subtracting -1, from the list of values.

Here's what I have:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
double arg = 0, counter = 0, sum = 0;
double average;

cout<< "Enter positive numbers or -1 to quit: \n\n";


do
{
cin >> arg;


while (arg < -1)
{
cout << "Enter only 0 and positive numbers!\n";
cin >> arg;
}


sum += arg;
counter++;

}

while (arg != -1);


average = sum/counter;


cout<< sum << "/"<<counter <<endl;
cout<< fixed<< showpoint<< setprecision(2);
cout<< "The average of your "<<counter<<" numbers is "<<average<<endl;


}
It's easier to use a for loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for(;;)
{
  cout << "Enter positive numbers or -1 to quit: \n\n";
  cin >> arg;
  if (arg == -1)
    break;

  while (arg < -1)
  {
    cout << "Enter only 0 and positive numbers!\n";
    cin >> arg;
  }
    
  sum += arg;
  counter++;
}
Topic archived. No new replies allowed.