Archived

This topic is now archived and is closed to further replies.

Strings in Functions

This topic is 5016 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know it is embarrassing ... I don''t know where to look. Thought I could ask here. I want to pass a string as a variable and compare it later. For example call this void with different variables: void ThisExample( somevariable ) { thevariable = somevariable; if ( thevariable == "hello" ) { cout << "hi\n"; } if ( thevariable == "idiot" ) { cout << "shuddup\n"; } return 0; } I don''t know what variables to use. I guess char arrays? I can''t get it to work. Can anybody help?

Share this post


Link to post
Share on other sites
If you''re using C++, you should probably use the std::string class (#include < string>). It allows for comparisons using the == operator, and overloads other operators as well for assignment, concatenation, etc... All the various cool little things that make them so much better than C-style character arrays.




Golem
Blender--The Gimp--Python--Lua--SDL
Nethack--Crawl--ADOM--Angband--Dungeondweller

Share this post


Link to post
Share on other sites
Hey Clueless,

Here is how I would use something like this:

#include <iostream.h>
#include <string.h>

void ThisExample( char *buffer ) {
// strcmp() is a good way for checking if a char* or char[] equals what you want. Also making sure if it equals 0 assures you that they are exactly the same, not sort of off and on

if( strcmp(buffer, "hello") == 0 ) {
cout << "hi\n\n" << endl;
}else if( strcmp(buffer, "idiot") == 0 ) {
cout << "shuddup\n\n" << endl;
}
}

int main() {
char str[] = {"hello"}; // set str to "hello"


ThisExample( str ); // send str to your function


return 0;
}



Edit: Here is some information about strcmp(). Simply what this function does is it compares a string to another, in this case buffer to "hello" or whatever.

Here is a quick example of how it would work:

int strcmp_example(const char *s1, const char *s2) {
while( *s1 == *s2 && *s1 && *s2 ) {
s1++;
s2++;
}

return *s1 - *s2;
}


Of course you wont need this, its just an example.


Hope this helps, and questions you have please feel free to ask,
- [BDS]StackOverflow

[edited by - BlueDev on March 18, 2004 11:30:54 PM]

Share this post


Link to post
Share on other sites
Thanks for the fast replies!

The std::string class works like a charm.

I read about strcmp ... but I wasn''t able to filter out the important stuff.
The example helps. I''ll have to experiment a little.

Thanks again.

Share this post


Link to post
Share on other sites
The important stuff about strcmp:

- it compares the strings (in the C sense) pointed to by the arguments (of type char *). Simply testing == would test whether the *pointers* are the same. You can have multiple copies of the same string in memory, so that''s (generally) not good enough.

- it returns 0 if the strings are equal, up until the null terminator of each. It will keep going until it finds one of those and has no concept of what space has been allocated. (So doing stuff with std::string is a lot safer and should be preferred any time you can get away with it.)

Share this post


Link to post
Share on other sites
When you pass a string as a variable, ordinarily you''re really passing a pointer to an array of char. In basic C/C++, when using char arrays, you can''t use ''=='' to test equality ... you have to use one of the string compare functions like strcmp() or stricmp() (look for these in the C library reference) ..

so ...

char mystring[] = "Hello";

if (0 == strcmp("Hello",mystring)) then match was made

hope this helps

Share this post


Link to post
Share on other sites