+ 1

How to make a condition in string data type that a user is not allowed to input numbers? Also how to set a limit on it?

example: sample run 1: enter your first name: khaizer sample run 2: enter your first name: 851565415684188515 Invalid input! please try again. enter your first name: sample run 3: enter your first name: dssjxsbfwjdbkndkjdbdjd Invalid input! 15 character only! please try again. enter your first name:

16th Dec 2016, 3:22 AM
Anonymous Hacker
Anonymous Hacker - avatar
1 Réponse
+ 2
bool CheckLength(char * Input, int MaxLength) { if (strlen(Input) > MaxLength) { return false; } else { return true; } } bool IsAllLetters(char * Input) { int len = strlen(Input); for (int i = 0; i < len; i++) { if (((Input[i] >= 'a') && (Input[i] <= 'z')) || ((Input[i] >= 'A') && (Input[i] <= 'Z'))) { return false; } } return true; } void main() { string Name; while (true) { cin >> Name; if (CheckLength(Name, 15)) cout << "Length error ...."; else if (IsAllLetters(Name)) cout << "Not a name.."; else break; } }
16th Dec 2016, 2:23 PM
Sara Majidzadeh
Sara Majidzadeh - avatar