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
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.
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.
精彩评论