Hi,
I've got problem with method ignore from cin.
This is my code:
void _name()
{
string name;
char ch = 0;
bool name_accepted = false;
cout << "Enter your name: " << endl;
while (!name_accepted)
{
while (ch = cin.get())
{
//cin.
if (isalnum(ch) || isspace(ch) || ispunct(ch)) //construct name
{
name += ch;
if ((ch == '\n') && cin && (name == ""))
{
name = "UNNAMED";
}
if ((ch == '\n') && cin && (name != ""))
{
name_accepted = true;
}
}
else
{
cerr << "Bad input. Try again." << endl;
cin.clear(ios_base::failbit); //set to fail
}
if (!cin)
{
name = "";
ch = 0;
cin.ignore(256);
//cin.seekg(0);
//cin >> _dump;
cin.clear();
}
}
if (!cin)
{
//throw new _Exit();
cerr << "Bad input. Try again." << endl;
name = "";
ch = 0;
cin.ignore(256);
//cin.seekg(0);
//cin >> _dump;
cin.clear();
}
}
}
but when I enter :
a^A^Atom (letter a, ctrl+A, ctrl+A, t, o,m), it catches correctly error but ignore seems to ignore just one sign instead of ignoring all of them. Hope someone will help with this. Thank you.