I'm trying to compare user text input from an iphone app with the text in a static array I have declared. It is always returning "true", even when the text is different. After doing the strncmp, I display both text fields. To the human eye, they are what I expect the fields to be. The debugmsg I return to the screen shows what I expect the values to be, but the compare is always coming up true. Any suggestions would be appreciated. Thanks.
if (strncmp(SymbolEntered.text,
[NSString stringWithCString:elements_table2[idx].element_symbol],2)==0)
{
DebugMsg.text = [NSString stringWithCString:"Correct answer"];
}
else
{
DebugMsg.text = [NSString stringWithCString:"Incorrect!"];
}
DebugMsg2.text = SymbolEntered.text;
DebugMsg3.text = [NSString stringWithCString:elements_table2[idx].element_symbol];
You really should do this with NSString
, which has tons of comparison methods implemented, instead of CString
(why are you using CString
?). strcmp
doesn't work with NSString
.
if([SymbolEntered.text isEqualToString:[NSString stringWithCString:elements_table2[idx].element_symbol]]) {
DebugMsg.text = @"Correct answer";
} else {
DebugMsg.text = @"Incorrect answer";
}
Also instead of:
DebugMsg.text = [NSString stringWithCString:"Correct answer"];
you can do this:
DebugMsg.text = @"Correct answer";