Need some help with C++ (spec. strncmp())

Gooblah

Heh...
Joined
Jun 5, 2007
Messages
4,282
So, when one includes the string.h library in C++, one can use the strncmp function, which takes two strings, a length, and compares each character of one string to the corresponding one of the other, until the characters don't match, a null (/n) chara has been reached, or the length has been reached.

Something like this:

strncmp (const char* string 1, const char* string2, int length)

The function returns zero if all the characters up to the length specified match.

In a program I've written to add new tags to XML files (a hopeful boon for Civ4 modders!), I need to use the stncmp function to determine where to add the new tags. However, when I try to compile, I cannot get it to work. I'm inputting two char* arrays and an int value, but instead get an error like this:
Code:
invalid conversion from `char' to `const char*' 
initializing argument 1 of `int strncmp(const char*, const char*, size_t)'

Any ideas?
 
The error says that you are passing a character instead of a string as the first parameter.

This.

And a null character is \0, \n is the newline control character.

But if you're using C++ you shouldn't use C-style strings. It does work, but it is asking for all sorts of trouble. Better use std::string instead (#include <string>)
 
The error says that you are passing a character instead of a string as the first parameter.

This.

And a null character is \0, \n is the newline control character.

But if you're using C++ you shouldn't use C-style strings. It does work, but it is asking for all sorts of trouble. Better use std::string instead (#include <string>)

Hmmm. The program uses character arrays to handle the tags; it's the only way I could think of including a unique tagname and all the necessary characters......hold on.

Can one add strings?

For example, if I did String1 (Gooblah) + String2 (Rocks), would I get 'GooblahRocks'? If so, that would make it all work.....(muses).
hmmm. Time to write a quick test program. Thanks for the help guys! :D

Edit: HOLY CRAP IT WORKS! YES! YES! YESSSSSSS! I CAN MAKE THIS PROGRAM WORK! YESSSSS! WOOOOOOO! :goodjob:

Edit2: Compilation complete. Now comes testing and (hopefully) release.
 
Back
Top Bottom