0

Hey im trying to validate a string. Basically what i want it to do is prevent the user from entering anything other than a string. Here is my code:

**getString**

string getString(string str)
{
    string input;
    do
    {
        cout << str.c_str() << endl;
        cin >> input;
    }
    while(!isalpha(input));
    return input;
}

Errors

Error   2   error LNK2019: unresolved external symbol "public: bool __thiscall Validator::getString(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?getString@Validator@@QAE_NV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: void __thiscall Player::information(void)" (?information@Player@@QAEXXZ)    C:\Users\Conor\Documents\College\DKIT - Year 2 - Repeat\DKIT - Year 2 - Semester 1 - Repeat\Games Programming\MaroonedCA2\MaroonedCA2\Player.obj    MaroonedCA2
Error   3   error LNK1120: 1 unresolved externals   C:\Users\Conor\Documents\College\DKIT - Year 2 - Repeat\DKIT - Year 2 - Semester 1 - Repeat\Games Programming\MaroonedCA2\Debug\MaroonedCA2.exe MaroonedCA2
    4   IntelliSense: no suitable conversion function from "std::string" to "int" exists    c:\Users\Conor\Documents\College\DKIT - Year 2 - Repeat\DKIT - Year 2 - Semester 1 - Repeat\Games Programming\MaroonedCA2\MaroonedCA2\Validator.cpp 72  17  MaroonedCA2

Main

cout << "What is your name ?\n";
    name = validator.getString();<------This skips.

    cout << "\nWhat is your age? ";
    age = validator.getNum();

    string character = "What is your sex M/F?";
    sex = validator.getChar(character);


    cout <<"Name:\n"<< name<<" Age:\n" << age<< " Sex:\n"<< sex <<"\n";

New getString function.

string Validator :: getString()
{
   string input;
    do 
    {
    } 
    while (
    std::find_if_not( 
        std::begin(input), //from beginning
        std::end(input), //to end
        isalpha //check for non-alpha characters
    ) != std::end(input) //continue if non-alpha character is found
);
    return input;
}
10
  • You can output a string; you don't have to convert it to a C string first. Anyway, I'm guessing you forgot to prefix it with Validator::. Commented Nov 28, 2012 at 19:01
  • An integer,double,float etc..... Commented Nov 28, 2012 at 19:04
  • 1
    isalpha takes in a char, not a string Commented Nov 28, 2012 at 19:05
  • @BeyondSora, Technically an int. Commented Nov 28, 2012 at 19:06
  • @chris Yeah i forgot the prefix. But i still have an error for no suitable conversion. The variable input on the line while(!isalpha(input)); shows this. Commented Nov 28, 2012 at 19:06

3 Answers 3

2

The first problem described is that this function belongs to a class, but you forgot to specify that:

string Validator::getString(string str)
       ^^^^^^^^^^^

Next, isalpha takes an int (due to C reasons), and, as far as I know, there is no version for std::string. You can, however, use standard algorithms to do this:

do {
    ...
} while (
    std::find_if_not( 
        std::begin(input), //from beginning
        std::end(input), //to end
        isalpha //check for non-alpha characters
    ) != std::end(input) //continue if non-alpha character is found
);

This find_if_not call will search through the string and check if any non-alpha characters are found by comparing the return value to the ending iterator of the string. If they're equal, the string is clean. You might also have to cast isalpha because it expects a predicate taking a char, not an int.

For some samples using this algorithm, see here. Note that due to the version of GCC on there, std::begin() and std::end() were replaced, and the != was changed to == due to the reversed logic of the function (you'd use it like do {} while (!ok(...));).

Sign up to request clarification or add additional context in comments.

5 Comments

Is there an import for the find_if_not ?
@Pendo826, find_if_not also part of C++11, so you'll need the appropriate version of your compiler and argument to use it. Using C++03, I'm pretty sure using std::find_if with std::not1 wrapping isalpha would work.
@chris im getting a problem. When i run the code using this cout << "What is your name ?\n"; name = validator.getString(); It skips the input altogether.
@Pendo826, Could you post a small, complete sample of code that causes that to happen?
I have also added the new function you have helped me with.
1

isalpha takes in an int not a string.

One way to make sure every character in your string is a letter would be to do something like this.

bool isAlpha = false;
while (!isAlpha) {
// take in input blah blah


    isAlpha = true;
    for (unsigned i = 0; i < input.length(); ++i) {
      if (!isalpha(input[i]))
          isAlpha = false;
    }
}

3 Comments

yeah sorry, should be fixed now.
Just ran the code with the update. It skips the string altogether. This is the way i done it in the main cout << "What is your name ?\n"; name = validator.getString();
Yeah i dont quite understand it.
0

A more efficient method would be to search your string for numeric digits, using the std::string functions:

static const char digits[] = "0123456789";
isAlpha = text.find_first_of(digits, 0); // start from position 0.

3 Comments

This would return true for anything except finding it at position 0. isAlpha = text.find_first_of(digits, 0) == std::string::npos;. I don't know exactly which characters the OP doesn't want, but it would work well if all of those characters are included (or doing a whitelist instead).
@chris: my understanding is that the 2nd parameter of find_first_of is the starting position to search from, inclusive. Also, the first position in the string is zero.
Yes, I meant the return value. The only false return possible is if it's found at index 0. Otherwise, the return value will be the index (not 0), or npos (also not 0), meaning isAlpha will mostly always be true.

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.