opposite bool compare output, why?
Feb 7, 2009 at 12:10am UTC
tihs is an extension of the dice game i am making, but i have two string arrays that look like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string rollone[] = {"one" , "two" , "two" , "three" , "three" };
string rolltwo[] = {"two" , "three" , "three" , "one" , "two" };
bool equal;
for ( int step = 0; step < 5; step++ ) {
equal = rollone[0].compare( rolltwo[step] );
cout << equal << " " ;
}
system ("pause" );
}
why does this return 1 1 1 0 1 instead of 0 0 0 1 0 i.e. a zero when they are not equal, and a 1 when the two entries are equal?
Feb 7, 2009 at 1:40am UTC
http://www.cplusplus.com/reference/string/string/compare.html
Return value
0 if the compared characters sequences are equal, otherwise a number different from 0 is returned, with its sign indicating whether the object is considered greater than the comparing string passed as parameter (positive sign), or smaller (negative sign).
Last edited on Feb 7, 2009 at 1:41am UTC
Feb 7, 2009 at 1:49am UTC
i see, i must have missed that part - is there a way without using the ! operator to get it to output 0 0 0 1 0 for the comparison?
Feb 7, 2009 at 2:27am UTC
What's wrong with NOT?
std::string has operator== overloaded, so you can just do rollone[0]==rolltwo[step]
.
Topic archived. No new replies allowed.