Question

My function is part of a Roman Numeral to Arabic conversion function. It evaluates fine where there is a pair CM, a pair CD, where there is a single character C, and for all the combinations of CM, CD, M, D and C.

However, where the final character in a string is C (e.g. MCDCCC), the function crashes on the final 'C'. Ive put a stack of NSLog in there to trace the problem but I can't work out why its crashing on the final 'C'.

Any help welcome as Ive been trying to figure this out for a week. PS, Ive rewritten it as a switch function but Im getting the same kind of problem.

-(NSString *) decimalToRomanNumeral : (NSString*) romanNumeral
{
    NSLog(@"Roman Numeral       is %@", romanNumeral);

    int sumOfDecimals = 0;
    int stringLength = [romanNumeral length];
    NSLog(@"RomanNumeral Length is %i characters", stringLength);
    int arrayOfPrimitiveIntegers[stringLength];
    NSLog(@"Array Size          is %i spaces", stringLength);

    for (int c=0; c<[romanNumeral length]; c++)
    {
        NSLog(@"Character at       counter %i is %C", c, [romanNumeral characterAtIndex:c] );

        if ([romanNumeral characterAtIndex:c] == 'M')
        {
            NSLog(@"**********************************");
            NSLog(@"Within                 Evaluator M");
            NSLog(@"Counter                      is %i", c);
            arrayOfPrimitiveIntegers[c] = 1000;
            NSLog(@"Value in Array at position %i is %i", c, arrayOfPrimitiveIntegers[c] );
            sumOfDecimals = sumOfDecimals + arrayOfPrimitiveIntegers[c];
            NSLog(@"Sum of Decimals              is %i", sumOfDecimals);
            NSLog(@"**********************************");
        }
        else if (  ([romanNumeral length]>1 ) && (([romanNumeral characterAtIndex:c] == 'C') && ([romanNumeral characterAtIndex:c+1] == 'D'))  )
        {
            NSLog(@"**********************************");
            NSLog(@"Within                Evaluator CD");
            NSLog(@"Counter                      is %i", c);
            arrayOfPrimitiveIntegers[c] = -100;
            NSLog(@"Value in Array at position %i is %i", c, arrayOfPrimitiveIntegers[c] );
            sumOfDecimals = sumOfDecimals + arrayOfPrimitiveIntegers[c];
            NSLog(@"Sum of Decimals              is %i", sumOfDecimals);
            NSLog(@"**********************************");
        }
        else if (  ([romanNumeral length]>1 ) && (([romanNumeral characterAtIndex:c] == 'C') && ([romanNumeral characterAtIndex:c+1] == 'M'))  )
        {
            NSLog(@"**********************************");
            NSLog(@"Within                Evaluator CM");
            NSLog(@"Counter                      is %i", c);
            arrayOfPrimitiveIntegers[c] = -100;
            NSLog(@"Value in Array at position %i is %i", c, arrayOfPrimitiveIntegers[c] );
            sumOfDecimals = sumOfDecimals + arrayOfPrimitiveIntegers[c];
            NSLog(@"Sum of Decimals              is %i", sumOfDecimals);
            NSLog(@"**********************************");
        }
        else if ([romanNumeral characterAtIndex:c] == 'D')
        {
            NSLog(@"**********************************");
            NSLog(@"Within                 Evaluator D");
            NSLog(@"Counter                      is %i", c);
            arrayOfPrimitiveIntegers[c] = 500;
            NSLog(@"Value in Array at position %i is %i", c, arrayOfPrimitiveIntegers[c] );
            sumOfDecimals = sumOfDecimals + arrayOfPrimitiveIntegers[c];
            NSLog(@"Sum of Decimals              is %i", sumOfDecimals);
            NSLog(@"**********************************");
        }
        else if  (([romanNumeral length]==1 ) &&   ([romanNumeral characterAtIndex:c] == 'C'))
        {
            NSLog(@"**********************************");
            NSLog(@"Within                 Evaluator C");
            NSLog(@"Counter                      is %i", c);
            arrayOfPrimitiveIntegers[c] = 100;
            NSLog(@"Value in Array at position %i is %i", c, arrayOfPrimitiveIntegers[c] );
            sumOfDecimals = sumOfDecimals + arrayOfPrimitiveIntegers[c];
            NSLog(@"Sum of Decimals              is %i", sumOfDecimals);
            NSLog(@"**********************************");
        }        
        else if ([romanNumeral characterAtIndex:c] == 'C')
        {
            NSLog(@"**********************************");
            NSLog(@"Within               Evaluator C>1");
            NSLog(@"Counter                      is %i", c);
            arrayOfPrimitiveIntegers[c] = 100;
            NSLog(@"Value in Array at position %i is %i", c, arrayOfPrimitiveIntegers[c] );
            sumOfDecimals = sumOfDecimals + arrayOfPrimitiveIntegers[c];
            NSLog(@"Sum of Decimals              is %i", sumOfDecimals);
            NSLog(@"**********************************");
        }
    }
    NSString * numberString = [[NSString alloc]init];
    //numberString = [NSString stringWithFormat:@"%d", decimalNumber];
    return numberString;
}
@end
Was it helpful?

Solution

romanNumeral characterAtIndex:c+1 -- this would seem to to be out of bounds on the final loop iteration.

You getting an NSRangeException?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top