I have a signed integer variable
when I do this in main, it is giving me “Error” for integer values as well.
int main(){
unsigned int a;
while(cin>>a){
if(!isdigit(a)){
cout<<"Error"<<endl;
}
}
}
[EDIT]:
Thanks to all the responses, I understood the issue. Now, how do I check if cin is reading integer only and not alphabets or any other character. Is there any function for that in c++. Thanks
The problem is that
isdigit()takes a character, not an integer.It returns true when the character is
'0','1', etc… Which has ascii values of48,49, etc…Try it this way instead: