problem with validate integer

Aug 16, 2016 at 12:27pm

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
int num;
 do{
		
		cout << "How many values ?";
		cin >> num;
		cin.clear();
		cin.ignore();
		if (cin.fail())
		{
			cout << "value must be interger !" << endl;
			cin.clear();
			cin.ignore();
			
		}
		else if (num <= 0)
		{
			cout << "The number must be integer value greater than 0!" << endl;
			
			
		}

		else if (num == double(num))
		{

			cout << "Interger value only, no decimal place!" << endl;
			

		}
		
	} while (cin.fail()||num<=0||num==double(num));


i want to validate the input only allow integer, when user input 3.5 or abc or -5 will go to if else statement and show the error msg.
What's wrong with my code?
Aug 16, 2016 at 12:39pm
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/general/195921/
Aug 19, 2016 at 3:01pm
You will always get en error because your third if statement (line 22) is not actually checking for decimal places, you're only changing your number to double, but it is still the some number.
Aug 19, 2016 at 3:06pm
also you're inputting it as an int, so any decimal number entered by a user will be rounded down
Topic archived. No new replies allowed.