Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have given two strings the same values but still it's saying the condition is false.

What I have tried:

char s[] = "hello", t[] = "hello";

	std::cout << "s: " << s << std::endl;
	std::cout << "t: " << t << std::endl;

	std::cout << (s == t ? "true" : "false") << std::endl;
Posted
Updated 31-Jul-18 7:04am

You're comparing the address of the two character arrays, not their contents. Either switch to std::string or use strcmp, see discussion here:

Comparing the values of char arrays in C++ - Stack Overflow[^]
 
Share this answer
 
This is because is not how string are compared.
in s == t, you are comparing the addresses of both string, and they are always different.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900