students[#] is an array of structs and students[#].name is a string in the struct, and I want to make sure that when the user inputs the name that it's verified to be a string of letters.
Can someone tell me a (simple?) way to get that job done?
I have tried the following to no avail.
bool checkString(const char *str)
{
bool isLetters = true;
while (*str != '\0')
{
if (*str < '65' || *str > '122')
{
//checking to see if it's over or below ASCII alpha characters
isLetters = false;
}
if (*str < '97' && *str > '90')
{
// checking to see if it's between capital and
//lowercase alpha char's
isLetters = false;
}
++str;
}
return isLetters;
}
main
{
//...
for (int i = 0; i < numStudents; ++i)
{
valid = true;
do
{
valid = true;
cout << "Enter NAME for student #" << i + 1 << ":" << endl;
cin >> students[i].name;
if (cin.fail() || !checkString(students[i].name.c_str()))
// tried to see if i could convert it to a c-style
//string to check char by char
{
cin.clear();
cin.ignore();
cout << "Please enter a valid input." << endl;
valid = false;
}
} while (!valid);
//...
return 0;
}
`
cinis astring. That string may then be parsed into something else though - like aintordoublefor example.switchstatements'65'and'122'meant to be? Either test if it's less than the character,*str < 'A', or the integer value,*str < 65. Not'65'(which is a multi-character constant, and not what you want). Better still, don't assume ASCII and useisalphalike the answer below.