devxlogo

Detecting Alphabetic Data

Detecting Alphabetic Data

Question:
How do you detect alphabetic data being entered into an integer field from a cin statement?

Answer:
The easiest way to do that is by having cin read a string rather than an int. Then you can examine the string to detect its validity. For example:

#include #include using std::string;using std::cout;using std::cin;int main(){  bool error = false;  string data;  cout>data;  for (unsigned int i = 0; i

devx-admin

Share the Post: