0

I am adding values held within an array but the sum is +1 what it actually should be.

//update totalscore
    uint newTotalScore;

    for (uint i=0; i< [bestscoresArray count] ; i++) {      
        newTotalScore += [[bestscoresArray objectAtIndex:i] intValue];  

    }


    totalscore = newTotalScore;

//output l1bestscore=15900, l2bestscore=7800, l3bestscore=81000, l4bestscore=81000, l5bestscore=0, l6bestscore=0, l7bestscore=0, l8bestscore=0, l9bestscore=0, l10bestscore=0, totalscore=185701

As you can see the totalscore output is 185701 but the sum of all values is 185700.

Would anyone have any ideas why this is occurring?

Thanks,

Mark

Phil Ross
  • 25,590
  • 9
  • 67
  • 77
crooksy88
  • 3,849
  • 1
  • 24
  • 30

2 Answers2

9

You must define newTotalScore's initial value:

uint newTotalScore = 0;

Otherwise it will be undefined. In your case it was 1 but it could have been any other value.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
2

Not sure about this, but did you try initializing newTotalScore to zero? (See this question about variable initialization.) If that does not help, give us more code.

Community
  • 1
  • 1
zoul
  • 102,279
  • 44
  • 260
  • 354