Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

danewithoutwax

macrumors newbie
Original poster
Aug 19, 2010
15
0
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;
 
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
 
I don't get have the strcmp() will help in this case?

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.
 
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.
 
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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.