-2

I am trying to make an app that takes away or adds 1 to an integer if the text entered in a text field matches an object in an array.

The Code in my .m file

NSString *inputtwo =EnterNameText.text;
BOOL isItright = NO;
for(NSString *possible in scoreArray1)
{
    if([inputtwo isEqual:possible] )
    {
        isItright = YES;
        break;
    }
}

NSString *wronginput = EnterNameText.text;
BOOL isWrong = NO;
for(NSString *wrong in scoreArray1)
{
    if(![wronginput isEqual:wrong ] )
    {
        isWrong = YES;
        break;
    }
}

static int myInt;

if(isItright)
{
    myInt++;

    NSString *score = [NSString stringWithFormat:@"%d", myInt];
    [scorelabel setText:score];
}

if (isWrong)
{
    myInt--;

    NSString *score = [NSString stringWithFormat:@"%d", myInt];
    [scorelabel setText:score];
}

So the program checks if there is a match in the array called scoreArray1, if there is it will add 1 to myInt, if not it will take one away.

The problem is it is only taking one away regardless whether it is right or wrong.

Thanks for your time.

deleted_user
  • 3,817
  • 1
  • 18
  • 27
Jeevan Thandi
  • 158
  • 2
  • 11

3 Answers3

2

You should be using isEqualToString if you are comparing string values. The isEqual method typically compares the pointer values, so something you get from a text field, and something entered in an array are going to always return different.

Dan F
  • 17,654
  • 5
  • 72
  • 110
  • Not to mention that `isWrong` will ALWAYS be true, unless your entered text somehow matches every single entry in `scoreArray1` – Dan F Oct 22 '12 at 20:52
0

You have a logical error in your program. First you check if the content of your text field matches any element in your scorearray1 and if there is a match isItright is true. Until this point everything is correct (except the equality check is better done with isEqualToString). But now you check if scorearray1 does not contain the content of your textfield and if only one elemnt in scorearray1 does not match the textfield isWrong will be true.

You should only use the first loop with a following if else. If the content of the textfield is equal to any string in scorearray1 add 1 to myInt, else (there is no match in the array) subtract 1.

Use the following code:

NSString *inputtwo =EnterNameText.text;
BOOL isItright = NO;
for(NSString *possible in scoreArray1)
{
    if([inputtwo isEqualToString:possible] )
    {
        isItright = YES;
        break;
    }
}

static int myInt;

if(isItright)
{
    myInt++;
}
else
{
    myInt--;
}
NSString *score = [NSString stringWithFormat:@"%d", myInt];
[scorelabel setText:score];
halex
  • 16,253
  • 5
  • 58
  • 67
  • Thank you so much! Can you please tell me how I can stop a user from entering in the same value for the array? For example an object called X in the array, can be entered in 10 times which can add 10 to the score. How can I say that an object in the array can only be used once? – Jeevan Thandi Oct 22 '12 at 21:31
  • @JeevanThandi There are two possibilities to achieve this: 1. After the inputted word is found in `scoreArray1` you remove it from the array (with remove I mean for example that you overwrite the respective element with a special string that probably will never be an input) 2. You make a second array `alreadyInput` where you add each input that is found in `scoreArray1` and add a loop over this array when you would set `isItright` to YES. You only assign YES if `inputtwo` is not in `alreadyInput`. – halex Oct 22 '12 at 21:49
  • I understand you would use the RemoveObjectatIndex command, but how does the program know what to remove, how do you tell the code to remove what the user entered? It would be really helpful if you could leave a comment on this question. http://stackoverflow.com/questions/12902079/array-issue-in-objective-c-xcode – Jeevan Thandi Oct 23 '12 at 00:00
  • @JeevanThandi I think the second version, that creates a new Array and appends already guessed words is better (as the answers in your posted question suggest). Removing elements from an array is handled in http://stackoverflow.com/questions/111866/best-way-to-remove-from-nsmutablearray-while-iterating – halex Oct 23 '12 at 07:19
0
NSString *input = EnterNameText.text;
BOOL matchFound = NO;
static in myInt;

for (NSString *score in scoreArray1)
    if ([input isEqualToString:score])
    {
        matchFound = YES;
        break;
    }

if (matchFound)
    myInt++;
else
    myInt--;
Tim Vermeulen
  • 12,352
  • 9
  • 44
  • 63