Strings in Functions

Started by
4 comments, last by Clueless 20 years, 1 month ago
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?
Writing errors since 10/25/2003 2:25:56 AM
Advertisement
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
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]
[/quote]
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.

Writing errors since 10/25/2003 2:25:56 AM
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.)
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
Too old for this ...

This topic is closed to new replies.

Advertisement