I want the user to enter a key and I want to check whether the key is a number or not, throw a message if it is not and exit if it is 0.
I read an answer that suggested the below method here : isdigit() c++, probably simple question, but stuck
int key;
while (true){
cout << "Enter Key (Press 0 to Exit) : ";
if (cin>>key){
if (key == 0){ break; }
//Code goes here
}
else{cout<<"Key should be a digit "<<endl;}
}
but my code goes into an infinite loop as soon as I enter an alphabet and I am not able to figure out why.
Any help would be appreciated or if there is a better alternate method for the same then do suggest.