Archived

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

AcidJazz

Comparing strings?

Recommended Posts

Lets say I have something like this void CompareNames(char *Name); void main() { char *Name1 = new char [50]; cin.getline(Name,50); CompareNames(Name); } void CompareNames(char *Name) { char *AnotherName = new char[50]; cin.getline(AnotherName,50); // How would I compare the names to see if they''re identicle? // If I do this... if(*AnotherName == *Name) // Only first letter gets checked, however if i do this if(AnotherName == Name) // It just doesn''t work ;( why and what dahh help plz =) }

Share this post


Link to post
Share on other sites
quote:
Original post by AcidJazz



if(AnotherName == Name)
// It just doesn''t work ;( why and what dahh help plz =)




The reason the above doesn''t work is because it only checks to see if the pointers are pointed towards the same address.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
ha ha good one.

yeah AcidJazz why aren''t you using string? Is it that you don''t know about it? string is a type that comes with your compiler. instead of strcmp, you use less than, or equals to as need be, instead of strcpy you use = just as you would with ints. Instead of strcat you use + or +=. You should study up on it is wonderful, dare I say even dreamy.

Just include string (no .h!) and as always use namespace std

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
yeah AcidJazz why aren''t you using string? Is it that you don''t know about it? string is a type that comes with your compiler. instead of strcmp, you use less than, or equals to as need be, instead of strcpy you use = just as you would with ints. Instead of strcat you use + or +=. You should study up on it is wonderful, dare I say even dreamy.


...Only that all those operators will probably perform (slow) memory operations that beginners are not aware of and which are often not neccessary.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Anonymous Poster
...Only that all those operators will probably perform (slow) memory operations that beginners are not aware of and which are often not neccessary.

And what do you think strcmp, strcat, et al do? They perform the same kind of operations, but std::string provides an easier, more intuitive interface for them.

Share this post


Link to post
Share on other sites