Validating name so it has only characters

I'm trying to make sure that the user only enters alpha characters for their name. The for loop is my way of testing to make sure it's stepping through each index of the array to cout each letter(alpha char). If an !alpha character pops up it goes into the while loop. If user enters jake5 for example. The program couts j is a letter, a is a letter, so on until it gets to 5 then it says 5 is not a letter. Once it asks for new input, it says 5 is still no good even though i enter a name without numbers. I'm lost please help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <cstring>
#include <cctype>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
    string name;
    int nameLength = name.length();
    char name1[nameLength];
    cout << "What's your name?\n";
    getline(cin,name);
    strcpy(name1,name.c_str());
    cout << strlen(name1) << endl;
    for(int i = 0;i < strlen(name1);i++)
    {
        if(isalpha(name1[i]))
        {
            cout << name1[i] << " is a letter.\n";
        }
        while(!isalpha(name1[i]))
        {
            cout << name1[i] << " is not a letter.\n";
            cout << "Enter your name. Characters only no numbers this isn't a game!\n";
            cin.clear();
            cin.ignore(10000,'\n');
            getline(cin,name);
        }
    }


    return 0;
}



Last edited on
The problem is that your loop continues to check if name1 is an alpha character. When you input a new name, you store it in name [b][i] instead of name[i][/b]. So name1[i] never changes, so the while loop condition will always evaluate to true.

Also, the size of name on line 12 of your code is actually 0, so your program is probably checking junk values anyway. Your program will be hit or miss regardless. You only need one variable to store the name. You need not copy into a char array, you can access a string's chars the same way you would a char array using the index operator [].
You could also use the std::string.find_first_of() function to detect if a digit has been entered, or find_first_not_of() to determine if only specific characters have been entered.

thanks for the quick Thanksgiving responses jlb and Keene! Going to use both of your suggestions now to work on my code. Line12 ...can't believe I didn't notice that. Also the reason I tried using cstring is so I can check for alpha characters. I'm working on it now, confused on how this whole code works. Why is this style of writing code different from what I'm learning at school. I'm learning C++, what gives. I know found is important I just learned how find_first_not_of works, but not the found part of this code. This is important because I know I'm going to need it in my while loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// string::find_first_not_of
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>        // std::size_t

int main ()
{
  std::string str ("look for non-alphabetic characters...");

  std::size_t found = str.find_first_not_of("abcdefghijklmnopqrstuvwxyz ");

  if (found!=std::string::npos)
  {
    std::cout << "The first non-alphabetic character is " << str[found];
    std::cout << " at position " << found << '\n';
  }

  return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <cstring>
#include <cctype>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
    bool beta = false;
    string name;
    cout << "What's your name?\n";
    getline(cin,name);
    int nameLength = name.length();
    for(int i = 0;i < nameLength;i++)
    {
        if(isalpha(name[i]))
        {
            cout << name[i] << " is a letter.\n";
        }
        else
        {
            cout << "First instance of a non char is at index "
                 << name.find_first_not_of("abcdefghijklmnopqrstuvwxyz",0) << ".\n";
            beta = true;
            if (beta == true)
            {
                cout << "Enter a name with characters only.\n";
                getline(cin,name);
            }
        }
    }




    return 0;
}


getting a weird output, but I'm on my way. Need to get it to disregard the for loop completely after it gets a bad input and ask for new input, but don't know how. Right now it's outputting the index and then asking for a new name then after I enter a new name it outputs the index again, but with some huge number. nvm fixed it. Writing out my problem here actually helped me figure it out tho. Made 2 second fix and it works now. Just had to put the 2nd if statement outside the for loop. Thnx to the people who helped.
Last edited on
Why are you discriminating against people with non-letter characters in their names?

http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/
Topic archived. No new replies allowed.