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 =)
}
Comparing strings?
Lets say I have something like this
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
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.
Jesus christ i''m dumb, i always forget about that stupid strcmp function thanks so much saved me a lot of stupid trouble =)
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!...
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
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
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement