Controlling Data Input

Hi,

I am currently trying to ensure that the user enters an int value when prompted and it keeps looping until they do. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void get_factoral (int &factoral)
{
    while (true)//while loop will execute until user inputs integer value
	{
		cout << "\nEnter a positive whole number greater than 0 you want to calculate (x!): ";
                cin >> factoral;

		if (cin.fail())//executes if cin was not an int value
		{
			cout << "\n\nThe value you entered was invalid" << endl;//tells user they entered an invalid number
			cin.clear ();//clears error state of stream
			cin.ignore(100, '\n');//clearing buffer for cin
		}
		else
		    break;//terminates loop when inputted value is an integer
	}
}


It seems to work when the input is an int, char or string but if the value is a double it does not work. Any ideas how I can fix this?

Thanks
It seems to work when the input is an int, char or string but if the value is a double it does not work. Any ideas how I can fix this?


use a template
Topic archived. No new replies allowed.