开发者

How do I use scanf to call an item in an array?

My problem/question is basically: how do I use scanf to call an item in an array.

and in more detail:

I want to make a simple app which gives details of houses in a street. I want an array/dictionary or similar which contains;

house type: terraced, detached etc. (string)

house number: (int)

Rooms: number of rooms in the house (int)

floor space: in feet squared. (int)

residents: number of people living in the house. (int)

etc.

I want to ask the user to enter a house number (in console for now but later using the iPhone UI) and then receive details of that property. Sometimes I would like to give all the details, sometimes just one or two.

I’m new to programming but have been researching loads and have learnt about arrays, dictionaries and plists. It seems I could use any of those in different ways but I think the best way is to use a combination.

I’m thinking of organising it all in a plist, using NSDictionary to store the details of each house and then putting all the houses in an array. so in the array, houseNo1, houseNo2, houseNo3 etc then each house/item in the array would be a dictionary with the keys; house type, rooms, floor space etc

I think that if I made the house number match up with the item number in the array then when the user enters “3” it takes the number ‘3’ and pulls the information from item3 in the array, ie. houseNo3.

I’ve been trying to do it using scanf and assigning their choice to a int called userInput but I can’t work out how to get the program to then use userInput to choose which item in the array to choose.

*I haven’t forgotten arrays start at 0 by the way, I figu开发者_开发问答re I can just assign a string to item0 saying “there isn’t any houses with the door number 0” or something along those lines.

I hope I’ve made that clear, I can explain more if need be. Any help would be greatly appreciated. :)

cheers kris

ps. I understand how to put the array/dictionary/plist together, it's just the scanf to retrieve data from the array that I'm having trouble with. - just didn't want anyone writing out lines of code explaining that unnecessarily.


Hey and welcome to programming! As far as using scanf initially and switching to iPhone UI, it's really going to be quite different since scanf is a C method and in iOS you will be getting an NSString object from the user (from a UITextView or whatever you use to let them input). To get a number from scanf from the console use something like

int inputInteger;  
printf("Enter a number:");
scanf("%d",&inputInteger);
myHouse = [myArray objectAtIndex:inputInteger];

Note that the above doesn't do error checking or valid input checking (that it's actually a number that was input).

That being said to get a integer (primitive) from a NSString you can use something like int inputInteger = [myInputString intValue]; and then use inputInteger to go to that index in the NSArray like above.

As a general programming aside: you should get used to indexing an array starting at 0, if the user inputs "1" and expects that to be the first item, you can just subtract 1 from the integer you get by the above methods and then use that to index your array (so it would really be whatever is at index 0, the first value in the array)


That was exactly what I needed thank you.

I used it to make this little program, it works as I wanted but not sure if I've gone about it in the best way. If anyone has a few minutes spare and fancies giving me some feedback that would be cool!

#import <Foundation/Foundation.h>

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

    //create the array
    NSMutableArray *housesArray = [NSMutableArray array];

    //create dictionaries
    NSMutableDictionary *house1Dict = [NSMutableDictionary dictionary];
    NSMutableDictionary *house2Dict = [NSMutableDictionary dictionary];
    NSMutableDictionary *house3Dict = [NSMutableDictionary dictionary];


    //put dictionaries in the array
    [housesArray insertObject: house1Dict atIndex:0];
    [housesArray insertObject: house2Dict atIndex:1];
    [housesArray insertObject: house3Dict atIndex:2];

    //populate the dictionaries
    [house1Dict setObject:@"1" forKey:@"House number"];
    [house1Dict setObject:@"semi-detached" forKey:@"House type"];
    [house1Dict setObject:@"5" forKey:@"rooms"];
    [house1Dict setObject:@"1,525" forKey:@"floor space"];
    [house1Dict setObject:@"sea" forKey:@"view"];
    [house1Dict setObject:@"friendly" forKey:@"neighbours"];

    [house2Dict setObject:@"2" forKey:@"House number"];
    [house2Dict setObject:@"detached" forKey:@"House type"];
    [house2Dict setObject:@"8" forKey:@"rooms"];
    [house2Dict setObject:@"2,685" forKey:@"floor space"];
    [house2Dict setObject:@"car park" forKey:@"view"];
    [house2Dict setObject:@"nosy" forKey:@"neighbours"];

    [house3Dict setObject:@"3" forKey:@"House number"];
    [house3Dict setObject:@"detached" forKey:@"House type"];
    [house3Dict setObject:@"2" forKey:@"rooms"];
    [house3Dict setObject:@"585" forKey:@"floor space"];
    [house3Dict setObject:@"rear" forKey:@"view"];
    [house3Dict setObject:@"drunk" forKey:@"neighbours"];


    //check its all there
    /*NSLog(@"in the array is%@", housesArray);
    NSLog(@"at index 0 is %@", [housesArray objectAtIndex:0]);
    NSLog(@"at index 1 is %@", [housesArray objectAtIndex:1]);*/

    int inputInteger;
    id myHouse;
    printf("Which house are you interested in viewing?");
    scanf("%d", &inputInteger);

    myHouse = [housesArray objectAtIndex:(inputInteger-1)];

    NSLog(@"House number %d is %@, has %@ rooms, beautiful %@ views and %@ neighbours.",
          inputInteger, 
          [myHouse objectForKey:@"House type"],
          [myHouse objectForKey:@"rooms"],
          [myHouse objectForKey:@"view"],
          [myHouse objectForKey:@"neighbours"]);

    [pool drain];
    return 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜