What is wrong with my code?

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
Wow that is one way to doing sanity checks :O If you only want them to enter an integer, then just use an integer data type, and check to see if the failbit flag is set (it will be if someone tries to stick some characters into an int).

http://www.cplusplus.com/reference/ios/ios/fail/
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
try
1
2
3
 
while (!(cin >> type))
    cout << prompt;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int handler(int type, string prompt)
{
	while (true)
	{
		cin >> type;
		if(cin.fail())
		{
			cout << prompt;
                                          cin.clear (); // clear error flag
			continue;
		}
		else
		{
			break;
		}
	}
	return type;
	
} 
Ahh, thank you modoran, this solved my problem. Thanks for everyone's ideas!
Topic archived. No new replies allowed.