Jun 21, 2013 at 7:38pm UTC
Here is my function. This is meant to keep the user from entering anything other than an integer. For some reason it always rejects the input, even if it is correct. Can someone help me?
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
int handler(int type, string prompt)
{
bool good = false ;
while (!good)
{
string input;
cin >> input;
if (input.find('!' ) || input.find('@' ) || input.find('#' ) || input.find('$' ) || input.find('%' ) || input.find('^' ) || input.find('&' ) ||
input.find('*' ) || input.find('(' ) || input.find(')' ) || input.find('_' ) || input.find('=' ) || input.find('~' ) || input.find('`' ) ||
input.find('q' ) || input.find('w' ) || input.find('e' ) || input.find('r' ) || input.find('t' ) || input.find('y' ) || input.find('u' ) ||
input.find('i' ) || input.find('o' ) || input.find('p' ) || input.find('[' ) || input.find(']' ) || input.find('\\' ) || input.find('a' ) ||
input.find('s' ) || input.find('d' ) || input.find('f' ) || input.find('g' ) || input.find('h' ) || input.find('j' ) || input.find('k' ) ||
input.find('l' ) || input.find(';' ) || input.find("'" ) || input.find('z' ) || input.find('x' ) || input.find('c' ) || input.find('v' ) ||
input.find('b' ) || input.find('n' ) || input.find('m' ) || input.find(',' ) || input.find('.' ) || input.find('/' ) || input.find('Q' ) ||
input.find('W' ) || input.find('E' ) || input.find('R' ) || input.find('T' ) || input.find('Y' ) || input.find('U' ) || input.find('I' ) ||
input.find('O' ) || input.find('P' ) || input.find('{' ) || input.find('}' ) || input.find('|' ) || input.find('A' ) || input.find('S' ) ||
input.find('D' ) || input.find('F' ) || input.find('G' ) || input.find('H' ) || input.find('J' ) || input.find('K' ) || input.find('L' ) ||
input.find(':' ) || input.find('"' ) || input.find('Z' ) || input.find('X' ) || input.find('C' ) || input.find('V' ) || input.find('B' ) ||
input.find('N' ) || input.find('M' ) || input.find('<' ) || input.find('>' ) || input.find('?' ))
{
cout << prompt;
}
else
{
int value = atoi(input.c_str());
return value;
}
}
}
Can someone tell me what I am doing wrong? (Besides atoi(), I will fix that later)
Last edited on Jun 21, 2013 at 7:39pm UTC
Jun 21, 2013 at 11:18pm UTC
This is what I tried:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int handler(int type, string prompt)
{
while (true )
{
cin >> type;
if (cin.fail())
{
cout << prompt;
continue ;
}
else
{
break ;
}
}
return type;
}
It goes into an infinite loop anyway.
Last edited on Jun 21, 2013 at 11:31pm UTC
Jun 25, 2013 at 6:45pm UTC
Ahh, thank you modoran, this solved my problem. Thanks for everyone's ideas!