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.