check if input is integer



1
2
3
4
5
6
7
8
9
10
11
	
while((cout<<"Enter Year: ")&&(!(cin>>getYear)||getYear<0)){
		cout<<"Invalid Input!"<<endl;
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(),'\n');
	}
while((cout<<"Enter Month: ")&&(!(cin>>getMonth)||getMonth<1 || getMonth>12)){
		cout<<"Invalid Input!"<<endl;
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(),'\n');
	}


-------------
Is there a function or simplier way to do this? thanks
In addition you can check the reading that the read value whether number or not:
http://www.cplusplus.com/reference/iostream/istream/

look fail, good, bad methodes.
you mean I'll add these methods to the code above?
Hmmm... what I want is to make the lines shorter...

well, thanks for the help. =)
I think this code is okay.
It is still not okay.
When the input is a number preceded by any character like 123asds, it will assign 123 to getYear instead of outputting Invalid Input.
What will I add to this code?
cin.ignore(numeric_limits<streamsize>::max(),'\n');
or should I use sstream?

1
2
3
4
5
6
7
   int year=-1;
   string mystr;
   cout << "Enter year: ";
   getline (cin,mystr);
   stringstream(mystr) >> year;
   if(year == -1)
      cout<<"Invalid Input"<<endl;

Is there a simplier way to do this?
when the input is not an integer, print invalid input then input again.
Alright, cin reads integer values and stops when it encounters the string.

If you want to read the complete string that the user enters use this:

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
#include <cstdio>
#include <ctype.h>
#include <string.h> // or is it <cstring> ?

int main()
{
   char year[80]; /* Could be 4+1 but from what I'm guessing you want to get the complete "123asds" as it is, so, 80 should be enough? */
   size_t len;
   bool strIsNumber(char *);

   cout << "Enter year: ";
   fgets(year, 80, stdin);
   len = strlen(year);
   if(strIsNumber(year) && len <= 4) {
       /* Do what you will with a valid input here
        ... */
   } else {
   cout << "Invalid input!\n"; // print error message
   }

   return 0;
}

bool strIsNumber(char *buff)
{
   int mybool;

   while(mybool=isdigit(*buff++)) ;

   return mybool ? 1 : 0;
}


That is the only way of getting all what the user enters.
If you want a pure integer then after performing the check use the library function int atoi(char *). The return value is the converted integer - use that.
Don't forget to #include<cstdlib> if you use it.

EDIT: Sorry about the includes. I'm more used to the C header files.
-unoriginal
Last edited on
I see. I must use the isdigit() function.
Thanks unoriginal for the idea.
No problem, I'm glad that helped.

Also, it only checks for a single char, that's why there was that loop and function.
Don't forget to include <ctype.h>
Use this reference to determine the C++ header names instead of the deprecated C headers.
http://cplusplus.com/reference/
Topic archived. No new replies allowed.