User inputs incorrect number-error message

I'm almost finished with a program that asks the user to input a number from 0 to 100, then asks if they'd like to enter another umber (y/n), and the loop continues until the user enters 'n'. At the end of the program, the sum, average, largest and smallest of the inputted numbers is to be shown. Also, if the user enters a number that isn't in the range of 0 to 100, the message "Please enter a number from 0 to 100" should appear. I'm having difficulty having that message appear, as well as obtaining the correct sum, average, largest and smallest of the inputted numbers. I'm not quite sure where I'm going wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
	int number = 0;
	int count = 0;
	char key;
	int sum = 0; 
	float average = 0;
	int largest = 0;
	int smallest = 0;
		
		do
		{
			cout << "Enter a number between 0 and 100: ";
			cin >> number;
			count++;

			cout << "Would you like to enter another number (y or n)? ";
			cin >> key;

			if (number < 0 || number > 100)
				{
					cout << "Please enter a number between 0 and 100. ";
					cin >> number;
				}
			
		} while (key != 'n');
		
			sum = sum + count;
			average = float(sum)/number;
			if (number > largest)
				largest = number;
			if (!count || smallest > number)
				smallest = number;

		
		cout << "You've entered " << count << " numbers. " << endl;
		cout << "The sum is: " << sum << endl;
		cout << "The average is: " << average << endl;
		cout << "The largest is: " << largest << endl;
		cout << "The smallest is: " << smallest << endl;

	getch();
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
do
{
  cout << "Enter a number between 0 and 100: ";
  cin >> number;
  while (number < 0 || number > 100)
  {
    cout << "Please enter a number between 0 and 100. ";
    cin >> number;
  }
  count++;

  cout << "Would you like to enter another number (y or n)? ";
  cin >> key;

} while (key != 'n');
Sohguanh,

That didn't work...now I'm getting a slew of "Please enter a number between 0 and 100" and I can't get it to stop
Ok maybe I misunderstood what you want.

1. "Enter a number between 0 and 100: ";

If user enter something below 0 or > 100, what should happen ?

if they enter a number less than 0 or greater than 100, the program should prompt them to "Please enter a number between 0 and 100"
"Enter a number"

then continue with: "Would you like to enter another number (y or n)?"

Also, I'm still stuck on the sum, average, largest, smallest...help with that would be great, too :( I'm new to programming...
This is an example my teacher gave of how the program should run:

Enter a number: 80
Would you like to enter another number (y or n)? y
Enter a number: 100
Would you like to enter another number (y or n)? y
Enter a number: 200
The number must be in the range 0 to 100.
Enter a number: -5
The number must be in the range 0 to 100.
Enter a number: 50
Would you like to enter another number (y or n)? y
Enter a number: 70
Would you like to enter another number (y or n)? n
You entered 4 numbers
The sum is 300
The average is 75.0
The largest is 100
The smallest is 50
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int number = 0;
int count = 0;
char key;
do
{
  cout << "Enter a number: ";
  cin >> number;
  while (number < 0 || number > 100)
  {
    cout << "The number must be in the range 0 to 100.\n";
    cout << "Enter a number: ";
    cin >> number;
  }
  count++;

  cout << "Would you like to enter another number (y or n)? ";
  cin >> key;

} while (key != 'n');
cout << "You've entered " << count << " numbers\n";
Thanks! :) it works now.

Any help with the sum, average, largest and smallest?
Actually you really should put in some effort and post your code snippets and then we try to help else I will be accused of providing solutions in this forum and get banned thereafter.
I really have been trying, but I'm very stuck and am not sure where to go from here.

I think that the average is correct, but I'm not sure...right now I'm working on the sum and have tried:

 
sum = sum + number;


and

 
sum = sum + count;


I'm not sure what other way to do this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int number = 0;
int count = 0;
char key;
int sum = 0;
float average = 0;
int largest = -1;
int smallest = -1;

do
{
  cout << "Enter a number: ";
  cin >> number;
  while (number < 0 || number > 100)
  {
    cout << "The number must be in the range 0 to 100.\n";
    cout << "Enter a number: ";
    cin >> number;
  }
  sum += number;
  if (largest == -1) largest=number;
  else if (number > largest) largest=number;

  if (smallest == -1) smallest=number;
  else if (number < smallest) smallest=number;
  count++;

  cout << "Would you like to enter another number (y or n)? ";
  cin >> key;

} while (key != 'n');
cout << "You've entered " << count << " numbers\n";
cout << "The average is " << (sum/count) << "\n";
cout << "The largest is " << largest << "\n";
cout << "The smallest is " << smallest << "\n";
Take a look at this. I think that both input statements (cin >> x) should be embedded within a while loop that prints an error message and clears if there is invalid or out of range input. It is critical to check for errors otherwise fat fingering a key will cause the program to fail. It is very simple to fix this and the pattern is shown in an example within the following FAQ.
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.3
Topic archived. No new replies allowed.