Thursday, July 30, 2009

How do I test for equality for chars in C++?

lets say I have the chars a and b and both of them are the following line "Hello there"





how do I set up an equality testing device to compare whether their values are equal such as





if (a==b)





I am trying to run a search for names and need a way to see if the line of text entered is equal to stored names.

How do I test for equality for chars in C++?
In C++, the easy way is to put them stl::string and compare those with the == operator.





In C, you can use the strcmp function.
Reply:First you need to write these lines at the top:





#include %26lt;cstring%26gt;





using namespace std;








Now to compare the two strings, simply use "strcmp" like this








if(strcmp(a,b)==0)


{...}








Note: the function strcmp returns 0 if the strings "a" and "b" are equivalent.


No comments:

Post a Comment