-1

I'm doing the challenges for BNR ObjC guide if anyone is wondering what this is from. I made a custom class that is called WCDStockHolding that has attributes 'name', 'currentPrice', 'purchasePrice', and 'numberOfShares'. I am asking the user to tell the program how many holdings they have then iterate over that many times to allocate the holdings as instances of the class in an NSMutableArray. The problem is, the name of all the WCDStockHoldings in my array are changed to the one added last. After debugging a lot I think this is due to my the setName instance method. What did I do wrong? I can't seem to figure this out. All other aspects of the objects in the array are fine.

setName implementation:

- (void)setName: (char*)n
{
    _name = n;
}

Reduced main function:

    NSMutableArray* stockArray = [[NSMutableArray alloc] init];

    char Stocks[100];
    printf("Enter Number of Stocks: ");
    scanf("%s", Stocks);
    int stocks = strtod(Stocks, NULL);

    for (int i=0; i<stocks; i++)
    {
        WCDStockHolding* holding = [[WCDStockHolding alloc] init];

        printf("[%d] Enter Stock [Name], [Purchase Price], [Current Price], and [Number of Shares]", i);
        printf("\nSeparated by commas (w/ no spaces) like above ");
        printf("\n>> ");

        char string[100];
        /* Declared name,PP,CP,and NS */


        char* items[100];

        scanf("%s", string);

        /* used strtok to split string into separate elements in array of strings 'item */

        name = items[0];  //name
        PP = strtof(items[1], NULL);  //purchasePrice
        CP = strtof(items[2], NULL);  //currentPrice
        NS = strtod(items[3], NULL);  //numberOfStocks

        [holding setName:name];
        [holding setPurchsePrice:PP];
        [holding setCurrentPrice:CP];
        [holding setNumberOfShares:NS];

        [stockArray insertObject:holding atIndex:i];
    }
abligh
  • 24,573
  • 4
  • 47
  • 84
cdipaolo
  • 229
  • 1
  • 2
  • 10

1 Answers1

0

Ok so I figured it out. Instead of using _name in WCDStockHolding as a char* I changed it to an NSString as well as all of the methods in main. I also used a separate for loop to allocate the holdings and referenced them in the 'setting' for loop as a call of the array they were already added to.

Use Allocation Loop:

for (int i=0; i<stocks; i++)
{
    WCDStockHolding* holding = [[WCDStockHolding alloc] init];
    [stockArray addObject:holding];
}

Deal with _name as an NSString and set ivars through array reference:

    for (int i=0; i<stocks; i++)
    {
        // request input form user

        char string[100];
        NSString* name = [[NSString alloc] init];
        // declare other variables

        char* items[100];

        scanf("%s", string);

        // use strtok to split string into items

        name = [NSString stringWithUTF8String:items[0]];
        // ...set others seen in initial question

        [[stockArray objectAtIndex:i] setName:name];
        // same as question in besides reference via array like above line
    }
cdipaolo
  • 229
  • 1
  • 2
  • 10