Compare two strings in C++

Feb 4, 2017 at 10:45pm
I need to compare two strings in C++. Suppose the first string is sol and the other one incomplete_ans. They both have the same length. I am using the compare function like this:

if ((sol.compare(incomplete_ans)) == 0)
{
cout<<"***W I N N E R***\n";
}

But, the program prints "Winner" even when the strings are not same.I also tried printing the two strings if somehow they are getting equal in the program; but they are completely different.

Thank you in advance
Last edited on Feb 25, 2017 at 11:43pm
Feb 4, 2017 at 11:08pm
If you are just testing if the strings are the same why not just use something like:

1
2
3
4
5
6
7
8
	if (string1 == string2)
	{
		cout << "Win" << endl;
	}
	else
	{
		cout << "Loser" << endl;
	}
Last edited on Feb 4, 2017 at 11:09pm
Feb 4, 2017 at 11:41pm
if ((sol.compare(incomplete_ans)) == 0);

Remove the semi-colon at the end of the if line.
Topic archived. No new replies allowed.