If you insist on a stop gap measure that might not work everytime you can leave sum -= n right where it is, but, at least IMO, it's not the best way to do things.

How do I ignore values outside of 0.0 and 100.0? Thanks for the help Lerner.

One way is to use a flag, again. They can be very helpful in controlling loops!

bool stillLooking = true;
do
{
   cin >> n;
   if(n <0 || n > 100)
      cout << "try again" << endl;
   else
     stillLooking = false;
}while(stillLooking);

Alternatively:

cout << "enter a number between 0-100 inclusive: ";
n = -9999;
while(n < 0 || n > 100)
{
  cin >> n;
  if(n < 0 || n > 100)
   cout << "enter a number between 0-100 inclusive: ";
}

should work, too.

I redefined sum in the last while loop as sum = sum - n (where n is the last value entered by the user). If i enter a number outside of 0-100 at the beginning of the inputs, it works fine, but if the last number entered is outside of 0-100, it does not work.

I redefined sum in the last while loop as sum = sum - n (where n is the last value entered by the user). If i enter a number outside of 0-100 at the beginning of the inputs, it works fine, but if the last number entered is outside of 0-100, it does not work.

Define "it works". One, you have brackets that don't need to be there. The compiler doesn't care, but it makes it hard for the human eye to follow. Delete the ones you don't need. Two, brackets should line up with the loop or if statement they are associated with. For example:

if (count == 0)
{
    maxval = n;
    minval = n;
}

not

if (count == 0)
{
	maxval = n;
    minval = n;
}

It makes it much easier to see what goes with what. So define your outer while loop:

do
{

}
while (!cin.fail ());

Everything inside this loop should be indented so it's clear what's inside the loop and what is not. Within this loop, once you read in the user data, you need to have one or more if statements that tests the data that is entered. You'll have at least three criteria.

One, user enters a number from 0 to 100. Two, user enters a number, but one that is not from 0 to 100. Three, user enters something that isn't a number. You should, at the least, decide which category the input falls into. You should actually probably split that last category into two categories:

Category 3a: User enters Ctrl-Z.
Category 3b: User enters something that isn't a number and isn't Ctrl-Z

but that will require you to redesign your program considerably. Each category involves executing certain code and not executing other code. So decide which of the three (or four) categories the input belongs to, then have it execute the appropriate code based on that. You'll need one or more if statements to decide which category it belongs to.

Subtracting n from sum after the loop is over is a band-aid that may work, but a better solution is to control what code is executed rather than clean up after code that shouldn't have executed in the first place. Proper indentation of brackets/code within brackets and deleting unnecessary/misleading brackets is key to understanding the program's flow and debugging it.

Using cin.fail () to test for Ctrl-Z is OK if you don't care that other things which aren't Ctrl-Z will be caught by it too, but if you do, you'll need a different solution.

I just decided to start over with all of your advice. The ctrl-z seems to have been "built-in." I am very happy to get it done. Thanks for all of your help.

#include <iostream>
using namespace std;

int main()
{
	double n;
	double minval = 0;
	double maxval = 0;
	double count = 0;
	double sum = 0;
	double range;

	while(cin >> n)
	{

		if(n < 0 || n > 100) 
		{
			cout << "out of range; value ignored" << endl;
			continue;
		}


		++count;
		sum += n;

		if (count == 1)
		{
			maxval = n;
			minval = n;
		}
		if (n > maxval)
		{
			maxval = n;
		}
		if (n < minval)
		{
			minval = n;
		}
		
		range = maxval - minval;
		
	}

	cout << "The average is " << sum / count << endl;
	cout << "The range is " << range << endl;


}

Except n isn't ignored if it's below zero or above 100. If everything outside the if statement inside the while loop is enclosed in an else statement, then n would be ignored if it is below zero or above 100.

It does ignore values entered outside of 0 to 100. Try executing it. Thanks Lerner.

continue;
Do you really need this?

have u read some reference textbooks? I believe there are many good reference textbooks for C++ study. For me, I use Weiss

Data Structures and Algorithm Analysis in C++, 3rd Edition

well organize and easy to follow.

If you search and buy from internet, you will find very good price for this textbooks. If you need where to buy it, email me at

<snipped>

commented: LOL! +12
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.