Strcmp not working properly

Oct 15, 2013 at 8:36am
Strcmp has been bugging me for a whole day now.
All of my strcmp are basically like that but it's not working. And I don't think it has to do with the char being inside a struct. It gives me a warning and when I run it it gives me a status access violation error.

1
2
3
4
 if(strcmp(variable.players[variable.p].tiles[j], word[j])!=0){
   printf("You don't have the letter %c\n",variable.players[variable.p].tiles[j]);
   break;
 }	
Oct 15, 2013 at 8:41am
strcmp compares C-style strings, which are a contiguous sequence of characters terminated by a '\0'.

From the printf, we can see that the tiles array holds single characters, not C-style strings. Don't use strcmp if you don't have C-style strings.
Oct 15, 2013 at 8:43am
You don't say which warning, nor you tell enough about the variables.
Oct 15, 2013 at 8:44am
strcmp() does not compare single characters, as its name suggests it compares strings. More specifically, arrays of characters.

http://www.cplusplus.com/reference/cstring/strcmp/

If you want to search for a char inside of an array, maybe you should use strchr() instead.

http://www.cplusplus.com/reference/cstring/strchr/
Oct 15, 2013 at 8:46am
please post the whole warning and error messages so we can have a clue on where to look for the error
Oct 15, 2013 at 8:51am
OH, ok. Cause I was trying to compare single chars.

So,

 
if(variable.players[variable.p].tiles[j] == word[j])


works just fine?

The warning btw, is that the passing arg 1 and 2 of strcmp makes pointer from integer without a cast and the error is a status access violation.

EDIT: I just realized that I have to check if all the chars in a string exists in a array of chars.

In my code, the word to be checked is word[j] and the array of chars is the variable.players[variable.p].tiles[j].

Any ideas on how to do that?
Last edited on Oct 15, 2013 at 8:54am
Oct 15, 2013 at 9:04am
Oct 15, 2013 at 9:27am
Any ideas on how to do that?

As keskiverto suggested above, use string::find_first_of().

This will also serve as a gentle introduction to how a C++ programmer should take advantage of the C++ library.

Case in point, char arrays are remnants of good old C, and together with them, the entire string.h library (with strcmp(), strcpy() etc). As a C++ programmer you are encouraged to move up to std::string.

http://www.cplusplus.com/reference/string/string/
Topic archived. No new replies allowed.