Reason for infinite loop:
cin goes into a failed state, and this causes it to ignore further calls until the error flag and buffer are reset.
cin.clear(); cin.ignore(100, '\n');
Check if the input is numeric:
In your code, even the non-int type is still added to the int. It is not possible to check whether the input is numeric without entering input into the char array and calling the isdigit() function for each digit.
The isdigit () function can be used to mark numbers and alphabets. This function is present in the <cctype> header.
The is_int () function will look like this.
for(int i=0; char[i]!='\0';i++){ if(!isdigit(str[i])) return false; } return true;
source share