Cin being Ignored in loop.

Basically this is a small part in a big program I've developed, I've managed to create an entire game of monopoly, now I've only got one error left... and it's really silly one too.

When I input the number of players as anything else but a number, the loop is being ignored and continually outputs the same line.

1
2
3
4
5
6
7
8
9
10
11
12
13
int players = 0;

cout << "Welcome to Monopoly" << endl;
	cout << "How many players do you want? (2 - 6)" << endl;
	cin >> players;

	//checking if players are between 2 and 6
	while(players > 6 || players < 2)
	{
		cout << "players must be between 2 n 6" << endl;
		cin >> players;
	}


I've tried cin.ignore, I've tried cout << flush, I've tried converting it to a string and float, then back too and int, I've tried resetting the int within the loop...

Nothing works.

So any help would be much appreciated. Thanks Olly
Last edited on
Ok I found a solution, I have to use

cin.clear();
cin.ignore(1000, '\n');

Got too use both of them because if you only use ignore it ignores the symbols but if you use clear it also ignores the characters.
u can get input in form of a string. get it checked whether it contains only digit.
then stream it into players


#include<sstream>
#include<string>
main()
{
string play;
int players;
getline(cin,play);
{
check if string contains all digit
}

stringstream(play) >> players;
Topic archived. No new replies allowed.