checking if a char is an integer

Hi, I am trying to create a function that can be called upon when the user wishes to receive an integer value only for a variable. This function will check that the user has entered an integer value and if not ask for the user to try again.

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
int main()
{
	while(true){
	
		int option = get_integer();
	}
	
	
	cin.get();
	return 0;
}

int get_integer(){
	char x[1] ={'a'};

	while(true)
	{
		cin>>x[0];
		if(isdigit(x[0]))
		{
			break;
		}else
		{
			printf("Please enter an integer value only:");
		}
	}
	return x[0];
}


When i type an incorect value that is longer than 1 such as "ffsdf" I am asked to enter an integer 5 times and then it allows me to type again it appears like so:

1
2
3
4
5
6
Please enter an integer value only:
Please enter an integer value only:
Please enter an integer value only:
Please enter an integer value only:
Please enter an integer value only:
_


How can i modify the code so that the "Please enter an integer value only:" appears only once no matter how many chars you enter?
Last edited on
Short answer is to check out ignore: http://www.cplusplus.com/reference/iostream/istream/ignore/

What has happened is that when you type "ffsdf" only the first character is extracted from the buffer. When the loop comes around again, it finds "fsdf" and extracts the first "f", checks if it is a digit, loops around again, finds "sdf" ...
I have changed it to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int get_integer(){
	char x[1] ={'a'};

	while(true)
	{
		cin>>x[0];
		cin.ignore(1,EOF);
		if(isdigit(x[0]))
		{
			break;
		}else
		{
			printf("Please enter an integer value only:\n");
		}
	}
	return x[0];
}


I still get the same problem but half of the time if that makes sense?
Sure does.

The first argument is the amount of characters to ignore, the second argument is the delimiter.

I generally use 80 as the first argument, but more proper would be numeric_limits<streamsize>::max(). Since I do basic console programs, ignoring a line of console input is probably enough. The other version will ignore the maximum amount possible to input in the first place.

The delimiter I generally set to '\n', as this will be the carriage return.
It works but I don't understand why. So the first paramater ignores the first 80 characters in the buffer and the second parameter tells it to stop ignoring characters when '\n' character is reached. If that is the case then I can't see how it solves my problem. Sorry for being so dense.
Line 6 reads one char, then line 7 throws the rest out of the buffer (until the next line.) Now in order to get more text from cin you need to type it in and hit enter again (because it's empty now.)
I would also suggest you read about the >> operator: http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/

cin.ignore(80, '\n'); will also extract the '\n'.

Basically, cin is going to wait for the carriage return, and >> is going to leave it ('\n') in the buffer. Most of the time this isn't noticed because the >> operator will also ignore the carraige return if it hasn't found the data type it was looking for.

The main problem is that >> isn't the only way to extract. Functions like get() and getline() will stop when they hit a '\n', so it is important that the buffer is clear for user input.

Try playing with some code, the goal to ask for an age as an integer and then get their full name as a string. To get you started you will need to use >>, ignore, and getline.


Ok. Thanks for help I think I understand it better now.
Topic archived. No new replies allowed.