Infinite loop when validating input

In my program, I am trying to validate the users input an make sure that the only thing they can put in is a number between 1998-2019. The problem is, whenever I input a number first(and only a number), the program runs fine but if I put an invalid number then a character or string, the program goes into an infinite loop. The program also runs whenever I type in a character or string first then a number, but if I type in a character after I have typed in a number, it enters an infinite loop. Does anybody know why this is occurring or have a possible solution? This is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  void Champs::validate()
{
	while (!cin)
	{
		cin.sync();
		cin.clear();
		while (cin.get() != '\n') continue;
		cout << "\nPlease enter an integer between 1998 and 2019: ";
		cin >> userInputYear;
	}
	while (userInputYear < 1998 || userInputYear > 2019)
	{
		cout << "Please enter an integer between 1998 and 2019: ";
		cin >> userInputYear;
		cout << endl;
	}
}


This is my entire program, if it helps
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

class Champs
{
private:
	ifstream inChamps;		//input file

	int ayear[22];			//arrays
	string aname[22];

	int userInputYear = 0;				//holds value for user input


	void openFile();
	void testFile();
	void readFile();
	void validate();
	void yearName();
	void closeFile();

public:
	void driver()
	{
		openFile();
		readFile();
		yearName();
		closeFile();
	}
};

void Champs::openFile()							//opens input file
{
	inChamps.open("NationalChampionship.txt");		
}

void Champs::readFile()
{
	if (inChamps.is_open())
	{
		cout << "National Championship Inquiry" << endl << endl;
		cout << "Reading the input file..." << endl << endl;
	}
	else
		testFile();
}

void Champs::testFile()
{
	cout << "Unable to open input file";
	exit(1);
}

void Champs::yearName()
{
	for (int i = 0; i < 22; i++)
	{
		int year;
		inChamps >> year;
		ayear[i] = year;

		string name;
		getline(inChamps >> ws, name);
		aname[i] = name;
	}

	

	//validate input
	
	bool on = true;
	while (on == true)
	{
		cout << "Enter the year: ";
		cin >> userInputYear;
		validate();
	
		if (userInputYear == -99)
			{
				on = false;
				break;
			}
		for (int i = 0; i < 22; i++)
		{
			
			if (userInputYear == ayear[i])
			{
				cout << "\nIn " << userInputYear << " the " << aname[i] << " won the national championship.\n\n";
			}
			
		}
	}
}

void Champs::validate()
{
	while (!cin)
	{
		cin.sync();
		cin.clear();
		while (cin.get() != '\n') continue;
		cout << "\nPlease enter an integer between 1998 and 2019: ";
		cin >> userInputYear;
	}
	while (userInputYear < 1998 || userInputYear > 2019)
	{
		cout << "Please enter an integer between 1998 and 2019: ";
		cin >> userInputYear;
		cout << endl;
	}
}

void Champs::closeFile()
{
	inChamps.close();
}

int main()
{
	Champs obj;
	obj.driver();
	system("pause");
	return 0;
}
1
2
3
4
5
6
7
8
	while (!cin)
	{
		cin.sync();
		cin.clear();
		while (cin.get() != '\n') continue;
		cout << "\nPlease enter an integer between 1998 and 2019: ";
		cin >> userInputYear;
	}
¿what's the purpose of that code?
Last edited on
I use it for when cin fails due to somebody inputting characters or if they try to say "two thousand". Without it, if words are entered, the program immediately enters an infinite loop.
Last edited on
Topic archived. No new replies allowed.