problem on the output--newbie

-hello, im just new in using c++...please help me know whats wrong with these program i've done...it allows a user to enter two words and will display if the 1st word entered is equal to the second word entered...my problem is if enter two equal words it displays NOT EQUAL...please help me


char first[15];
char second[15];
clrscr();
cout<<"Enter the first word: ";
cin>>first;
cout<<"\nEnter the second word: ";
cin>>second;

if(first==second)
cout<<"\n\nEQUAL";
else
cout<<"\n\nNOT EQUAL";

The logic is correct, but you're comparison isn't doing what you think.

replace
1
2
char first[15];
char second[15];


with std::string first, second;
Hello!

I suggest that you use string instead of array of char. It is more confortable and you can use the equality "==" operator.

http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/STRING.html

If you want to use arrays of char, you should use the strcmp to compare two "array of char" strings.

http://www.java2s.com/Code/C/String/Comparestringsstrcmp.htm

1
2
3
4
char first[15];
char second[15];
...
if(first==second)


This code fragment compares the two address of char arrays! I think that you didn't want to do it.

Bye!
thanks for the immediate reply kbw and screw...i will try those



thanks thanks a lot:)
Last edited on
You have to include <string> for that by the way. If you wanted to keep the char arrays though you can use strcmp(first, second) which is in <cstring>
thanks mcleano...that works!...cheers for that
:)
Thats fine. The reason why what you was doing before wasn't working, and why people were telling you to use std::string instead, is because std::string has comparison operator functions written for it: http://www.cplusplus.com/reference/string/operators/
Topic archived. No new replies allowed.