c++ - How to prevent integers being assigned to char strings? -
i new @ programming in c++, , have come across problem can't solve. trying create error message whenever user inputs integers instead of characters. problem is, integers being accepted , assigned char strings reason.. thought point of defining strings int or char etc. let compiler know values accepted. let me know i'm doing wrong?
int x, input = false; char check[size], str[size]; cout << "enter 1 word: "; cin >> str; while (!cin) { cout << "\nerror: must enter 1 word characters. \n\nre-enter: "; cin.clear(); cin.ignore(256, '\n'); cin >> str; }
a char 1-byte interpretation of integer according ascii value (i.e. integer 68 ascii value character 'd'). other ascii values can found here.
for code, need create function loops through inputted string , checks see if of characters not letters.
for example:
for (int = 0; < strlen(str); i++) { if ((int)str[i] < 65 || ((int)str[i] > 90 && (int)str[i] < 97) || (int)str[i] > 122) { // handle error } }
Comments
Post a Comment