• Announcements

Archived

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

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 on other sites
strcmp() might help. search for it.

Share on other sites
or break down and use std::string''s
#include <string>
using std::string;

Share on other sites

  string name2,name1="";getline(cin,name2);if(name1==name2)    ///...

If you''re talking about c-style strings,

  char *a,*b;strcmp(a,b); //returns 0 if equal

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 on other sites
Jesus christ i''m dumb, i always forget about that stupid strcmp function thanks so much saved me a lot of stupid trouble =)

Share on other sites
As SabreMan would say, "If you''re using C++, use C++!"

Share on other sites
quote:
Original post by Oluseyi
As SabreMan would say, "If you''re using C++, use C++!"

// Oh no... you have increase the C variable by 1 twice!...

Share on other sites
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 on other sites
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 on other sites
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.

• Forum Statistics

• Total Topics
628277
• Total Posts
2981779

• 10
• 11
• 17
• 11
• 9