Checking if user input is alphabetic

1.8k Views Asked by At

I don't know how to check if a user's input is alphabetic. I want the program to:

  1. read the user input
  2. check if it's alphabetic
  3. output the name again

I tried to use isdigit and isalpha but I couldn't get it to work.

//checking if name is valid and not a number
int nameCheck(char enteredName)
{
    //if the name is alphabetic
        /* return and output it */

    //else
        /* let the user enter it again */
}

//user enters the name
int nameEntering()
{
    cout << "please enter your Name.\n";
    char enteredName;
    cin >> enteredName;
    nameCheck(enteredName);
    return 0;
}

int main()
{
    char enteredName;
    enteredName = nameEntering();
    cout << "Have a nice day " << enteredName << "!\n";
    return 0;
}

This is my old solution which doesn't work at all. I found that on another site and used it as a "template".

int nameCheck(char enteredName)
{
    int i = 0;
    char str[] = {enteredName};
    while (str[i])
        {
            if (isalpha(str[i])) printf("character %c is alphabetic\n",     str[i]);

            else
            cout << "Enter your name again without using numbers or other     special characters, please!\n";
            nameEntering();
            i++;
        }
    return enteredName;
}
1

There are 1 best solutions below

0
On

I found the solution.

//user enters the name
void nameEntering()
{
    cout << "please enter your Name.\n";
    string enteredName;
    cin >> enteredName;

//checking if name is valid and not a number
    if (find_if(enteredName.begin(), enteredName.end(), isdigit) !=enteredName.end())
    {
        system("cls");
        cout << "please don't enter numbers or other special characters.\n";
        cout << "\n";
        nameEntering();
    }
    //if valid it outputs name
    else
    {
        coutput(enteredName);
    }
}