strcmp(std::string, const char*) error

Hello all!

Read a little about using regex in C++ and everything pointed to Boost, read about using Boost and it seemed beyond me to actually get it running.. Not a big deal since my project is rather simple (way simple for what you wise ones must've dealt with).

at this point I am trying to match one character from a string to a period ("."), so I have this function so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int * Board::understandMove(std::string sMove)
{
    // this "trick" was taken from http://www.cplusplus.com/forum/beginner/6644/
    int *aOut = new int[5];
    // we can now use aOut[1] = 23;

    bool passedNumber = false;

    /* loop through this string */
    unsigned int iPos = 0;
    for (iPos = 0; iPos < sMove.length(); iPos++)
    {
        cout << "Analyzing: " << sMove[iPos] << endl;
        /* does this move starts with a number? */
        if ( (std::isdigit(sMove[iPos]) || strcmp(sMove[iPos], ".") == 0) && passedNumber == false)
        {
            /* we just skip the numbers and the period */
            continue;
        }
    }
    cout << endl;

    return aOut;
}


Fairly straightforward I guess, but sure enough strcmp complains that the first argument is not const char*
Here is the output:

In member function ‘int* Board::understandMove(std::string)’:
error: invalid conversion from ‘char’ to ‘const char*’
error:   initializing argument 1 of ‘int strcmp(const char*, const char*)’


Any help or guidance will be much appreciated :)

Have a nice day
strcmp is a function that accepts two arguments; both arguments must be of type "pointer to const char". You have tried to pass sMove[iPos] which is a char. The compiler quite rightly does not know how to turn this char into a pointer to a constant char, so fails.

Do not pass a char; pass a pointer to a constant char. The compiler will probably also be happy with a pointer to a char.

http://www.cplusplus.com/reference/string/string/operator[]/
If you just want to test if the character at iPos is a '.', you need

... || sMove[iPos] == '.')

Note the single quotes.

Andy

P.S. strcmp should not be be used with std::string. Instead use its operator==

... || sMove == ".")

You can also use the compare method, but I wouldn't use this just test for eqality.

... || sMove.compare(".") == 0)
Last edited on
Thanks all!

In the end this did the trick:
 
if ( (std::isdigit(sMove[iPos]) || (sMove[iPos] == '.') ) && passedNumber == false)


Much obliged!
Topic archived. No new replies allowed.