object c: octal constant

Discussion in 'Mac Programming' started by danewithoutwax, Dec 28, 2010.

  1. danewithoutwax macrumors newbie

    Joined:
    Aug 19, 2010
    #1
    I have a little trouble with this part of my programming and i hope someone out there can help me. All the numbers needs to be as they are (0 can't be deleted)

    Code:

    #import <Foundation/Foundation.h>

    int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    int userNUM;
    NSLog(@"Write number here");
    scanf("%i", &userNUM);

    switch (userNUM) {
    case 0015 :
    NSLog(@"547048");
    break;
    case 0172 :
    NSLog(@"853846");
    break;
    case 0245 :
    NSLog(@"055201");
    break;
    case 0360 :
    NSLog(@"424839");
    break;
    case 0372 :
    NSLog(@"401574");
    break;
    case 0427 :
    NSLog(@"054376");
    break;
    case 0433 :
    NSLog(@"280993");
    break;
    case 0485 : invalid digit "8" in octal constant
    NSLog(@"856448");
    break;
    case 0554 :
    NSLog(@"523721");
    break;
    case 0654 :
    NSLog(@"298401");
    break;
    case 0673 :
    NSLog(@"913189");
    break;
    case 0678 : invalid digit "8" in octal constant
    NSLog(@"005165");
    break;
    case 0742 :
    NSLog(@"845915");
    break;
    case 0828 : invalid digit "8" in octal constant
    NSLog(@"785730");
    break;
    case 0950 : invalid digit "9" in octal constant
    NSLog(@"227521");
    break;
    default:
    break;
    }

    [pool drain];
    return 0;
     
  2. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #2
    Delete the leading 0s. For some bizarre reason literal numbers with leading zeros are interpreted as octal values. If you need the leading zeros, you can't use switch(). You'll have to use strcmp() and repeated if() + else
     
  3. danewithoutwax thread starter macrumors newbie

    Joined:
    Aug 19, 2010
    #3
    I don't get have the strcmp() will help in this case?
     
  4. MikhailT macrumors 601

    Joined:
    Nov 12, 2007
    #4
    Instead of the switch, why not use a hash table? If those numbers and their output is always the same, using a hash table would be faster than the switch case here.

    Look up NSDictionary or hash tables for ObjC.
     
  5. McGordon macrumors member

    Joined:
    Dec 28, 2010
    Location:
    Scotland
    #5
    Are you sure you really need the leading zeroes? If you use scanf("%i", &userNUM) it will interpret numbers with a leading 0 as octal and I don't think that's what you want as some of your cases have the digits 8 and 9.

    You'll probably have to read them as a string and then be unable to use a switch statement, as Catfish_Man said.
     
  6. danewithoutwax thread starter macrumors newbie

    Joined:
    Aug 19, 2010
    #6
    I tried to look that up, but google is not giving me what i want to know about it. Do you know any good sites or can you just give me a small example?
     
  7. MikhailT macrumors 601

    Joined:
    Nov 12, 2007
    #7
  8. danewithoutwax thread starter macrumors newbie

    Joined:
    Aug 19, 2010
    #8
    Okay, that would actually work really well. Thank you so much! :)
     

Share This Page