Input validation for numbers 0-100 for 3 variables

Hello,
The first part of my assignment asks me to write a program that takes 3 integers from 0-100 input in any order and outputs them shown smallest to largest.
I have the general body of the code down but I am having trouble with input validation. I am want to utilize cin.fail(), cin.clear(), and cin.ignore() to only allow inputs that are integers between 0-100.

I have it such that it fails when letters are entered but when it fails (for instance if I enter letter a) it prints out "Please enter an integer between 0 and 100In ascending order, the numbers are:"
when it should just say "Please enter an integer between 0 and 100".

I am not entirely sure how all the parts of this cin.fail() bit work. Any help appreciated. I am sorry this question is somewhat vague but I dont know the language enough to get more specific. Thanks.

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  //Arrange integers in order of smallest to greatest

#include <iostream>

using namespace std;

int main()
{
	int n1;
	int n2;
	int n3;
	

	cout << "Enter three numbers between 0 and 100:\n\n";

	cin >>  n1;
	cin >>  n2;
	cin >>  n3;

	if (cin.fail() == true)
	{
		cout << "Please enter an integer between 0 and 100";
		cin.clear();
		cin.ignore(50,'\n');
	}
	else;
	
	cout << "In ascending order, the numbers are:\n";

	if ((n1 < n2) && (n1 < n3))
	{
		cout << n1 <<" ";

		if (n2 < n3)
		{
			cout << n2 <<" "<< n3;
		}
		else
			cout << n3 <<" "<< n2;
	}

	if ((n2 < n1) && (n2 < n3))
	{
		cout << n2 <<" ";

		if (n1 < n3)
		{
			cout << n1 <<" "<< n3;
		}
		else
			cout << n3 <<" "<< n2;
	}

	if ((n3 < n1) && (n3 < n2))
	{
		cout << n3 << " ";

		if (n1 < n2)
		{
			cout << n1 <<" "<< n2;
		}
		else
			cout << n2 <<" "<< n1 << "\n";
	}
	
	

	
        system("pause");

	return 0;
		


}
Topic archived. No new replies allowed.