1

I am following the instructions for a tutorial but I cannot figure out what is wrong. I have double checked everything. I put the the compiler errors in the code's comments below. Sorry, this will probably show how much of a noob I am.

//  main.m


#import <Foundation/Foundation.h>
#import "LotteryEntry.h"

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

   // Creates the date object

    NSCalendarDate *now = [[NSCalendarDate alloc]init];

    //Seed the random number generator

    srandom(time(NULL));
    NSMutableArray * array;
    array = [[NSMutableArray alloc]init];

    int i;
    for (i = 0; i < 10; i++) {

        //create a date/time object that is 'i' weeks from now

        NSCalendarDate *iWeeksFromNow;
        iWeeksFromNow = [now dateByAddingYears:0
                                         months:0
                                           days:(i * 7)
                                          hours:0
                                        minutes:0
                                         second:0];
    }

     //create the LotteryEntry object

     LotteryEntry *newEntry = [[LotteryEntry alloc]init];
     [newEntry prepareRandomNumbers];
     [newEntry setEntryDate: iWeeksFromNow]; 

//Error says "Use of undeclared identifier "iWeeksFromNow'. Did I not declare it above?

     //add the lottery entry object to the array

     [array addObject:newEntry];

     }

     for (LotteryEntry *entryToPrint in array) { 

//Error says " Expected identifier or '('


         //Display it's contents

         NSLog(@"%@", entryToPrint);

     }





[pool drain];
return 0;       
//Error says " Expected identifier or '('
}                  
//Error says " Expected External declaration

5 Answers 5

2

You are declaring iWeeksFromNow inside a for loop, that's why the compiler doesn't consider it to exist outside declare it outside, and assign values to it inside

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, Black Frog is right, the for loop is cut short, it should go to the } under [array addObject:newEntry], instead of just after iWeeksFromNow
1

You have an extra closing } as you call the -dateByAddingYears method.

Comments

1

First error : you declare iWeeksFromNew inside a for loop, thus it's unreachable from outside. You have to declare before the beginning of the loop.

Second error : you have a bracket '}' after [array addObject:newEntry]; so the compiler thinks its the end of your method, remove it.

That should fix all other error you have

Comments

0

First, iWeeksFromNow is declared within the scope of a for loop, so it will be visible only within that loop. Second, as pointed out by Black Frog, you have an extra closing parenthesis.

Comments

0

Move the declaration out that loop block. You've got a scope problem here: The iWeeksFromNew only exists within the loop

NSCalendarDate *iWeeksFromNow;
int i;
for (i = 0; i < 10; i++) {

    //create a date/time object that is 'i' weeks from now
    iWeeksFromNow = [now dateByAddingYears:0
                                     months:0
                                       days:(i * 7)
                                      hours:0
                                    minutes:0
                                     second:0];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.