0

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;
        }

        `
5
  • The only thing you can enter into cin is a string. That string may then be parsed into something else though - like a int or double for example. Commented Feb 7, 2018 at 6:05
  • Oh I see so it may be much more work than it's worth if it's just for personal satisfaction then, because the only way I could see really doing it is through extensive switch statements Commented Feb 7, 2018 at 6:26
  • @MicahJohnson you can do it with function isalpha and a loop. isalpha tests if one character is a letter. You could also do it with a regex which is even simpler. Commented Feb 7, 2018 at 6:31
  • @john might just be out of my league, but when I come across those terms in my learning I'll keep that in mind. Thanks Commented Feb 7, 2018 at 7:20
  • What are '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 use isalpha like the answer below. Commented Feb 7, 2018 at 13:25

2 Answers 2

1

ASCII codes are integers, don't put them in quotes.

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;
}

You are still doing the hard way though, the function isalpha is designed for this task

#include <cctype>

bool checkString(const char *str)
{
    bool isLetters = true;
    while (*str != '\0')
    {
        if (!isalpha(*str)))
            isLetters = false;
        ++str;
    }
    return isLetters;
}

Then you could return immediately when you realise it's not a letter, no point carrying on checking

#include <cctype>

bool checkString(const char *str)
{
    while (*str != '\0')
    {
        if (!isalpha(*str)))
            return false;
        ++str;
    }
    return true;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Since this is processing untrusted input from outside the program, that needs to be isalpha((unsigned char)*str) or you get undefined behaviour if the input contains values outside the range [0,127] and char is signed. And strictly speaking if you include <cctype> you should use std::isalpha (or add a using-declaration, or include <ctype.h> instead).
Thanks, that worked. Feel silly since I didn't try taking them out of quotes. But isalpha is overall much more simple.
0

You will have to check if each char is an alphabet by using ascii values.

bool checkString(string stdunt)
    {
        bool isLetters = true;
        for (int i = 0; i < stdunt.length(); i++)
            {
                    if (stdunt[i] < 65 || stdunt[i] > 122)
                    { return false; }

                    if (stdunt[i] < 97 && stdunt[i] > 90)
                    { return false; }
            }
        return isLetters;
    }

int main()
{

int numStudents=5;
string students[numStudents];
    for (int i = 0; i < numStudents; i++)
    {
        bool valid = true;


    do{
            valid = true;
            cout << "Enter NAME for student #" << i + 1 << ":" << endl;
            cin >> students[i];
//            cout <<students[i];
            if (cin.fail() || !checkString(students[i]))
            {
                cin.clear();
                cin.ignore();
                cout << "Please enter a valid input." << endl;
                valid = false;
            }
      } while (!valid);
    }
    cout << "Complete";
    return 0;
}
`

2 Comments

Just edited code with attempt at doing this, no luck.
Just edited the answer, hopefully it is what you want. You can use pointers if the string size is unknown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.