how can I check if the input is integer or lettters? The program asks to enter an integer. IF the input is letter , the program should prompt the user that it is an "invalid input"
In a loop:
a. Read the input into a std::string
b. Try to convert the string to an integer (a std::istringstream is handy for this)
c. If the attempt is successful and entire input was consumed, exit the loop; use the number
d. Else emit an error message and repeat the loop.
One way to do it is what JLBorges suggested above.
Another way would be to check if cin is in an error state after the input.
You can do that with something like:
1 2 3 4
int input;
cin >> input;
if (!cin.good()) // if (!cin) should work too
// Bad input, do something about it
(of course, normally you would want to make that into a loop and until the user enters a valid value, but this is just to demonstrate)
Note that you'll have to use cin.clear(); to clear the error flags before you can use cin again.