user1492776
user1492776

Reputation: 280

stackoverflow when try to pass variable to bool function

i create a counting timer(on a label) and a variable that contain the label integer value(named count). also i create a function that check if my number is 7, divided by 7 or contain 7. when i try to pass my count value to the check function my app is stack. i try for a long time to find why the stack overflow is occur but i didn't success. how can i solve it? here is my code:

-(IBAction)start:(id)sender
{
    timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];

    MainInt = 0;
    numbersTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(countup) userInfo:nil repeats:YES]; 
}
-(void)countup
{
    MainInt += 1;
    numbersLabel.text = [NSString stringWithFormat:@"%i", MainInt];
    count = numbersLabel.text.intValue;
    NSLog(@"num: %d", count);

   if ([self checknum:(count) == false]) {
   NSLog(@"BOOM");
   }

}
-(BOOL)checknum:(int)number
{
    while (number<10) 
    {
        if(number ==7)
        {
            NSLog(@"boom, i=%d", number);
            return true;
        }


    }

    while (number>=10 && number<1000) 
    {
        if(number % 7 == 0)
        {
            NSLog(@"boom i=%d", number);
            return true;

        }

        if([self revese:(number)])
        {
            NSLog(@"boom reverse num = %d", number);
            return true;

        }

    }

    return false;   
}


-(BOOL) revese:(int)number
{
    if(number < 10 && number != 7)
        return false;
    if(((number % 10) == 7) || ((number / 10) == 7))
        return true;
    else {
        [self revese:(number / 10)];
    }
    return false;

}

thanks!

Upvotes: 0

Views: 399

Answers (3)

Warren Burton
Warren Burton

Reputation: 17378

The statement

if ([self checknum:(count) == false]) {
   NSLog(@"BOOM");
}

is nonsense. You are effective asking

BOOL isLessThanOne = (count < 1);
if ([self checksum:isLessThanOne]) {
           NSLog(@"BOOM");
}

change this for

if ([self checksum:count] == NO) {
       NSLog(@"BOOM");
    }

Upvotes: 2

mathematician1975
mathematician1975

Reputation: 21351

It is very hard to tell as I cannot really see what you are doing, but if you have stack overflow, it is very likely that your problem is the function revese which is recursive. I would bet that you have a certain value being passed to revese that is causing it to be called over and over again based on some of the unusual logic you are using in your conditional statements. You should really step through this function carefully with your debugger to identify why this is happening.

Upvotes: 0

spring
spring

Reputation: 18537

There are lots of issues but the first ones are these:

while (number<10) 

//

while (number>=10 && number<1000)

You want an if/else type conditional statement here. The way you have it now, since you never adjust the value of number, you will get caught in an infinite loop if the strict conditions you are testing later in the code are not met. Something more like:

if(number<10){ 
// do some tests

} else if (number<1000){
// do some other tests


}

There are other issues but those are a start.

Upvotes: 1

Related Questions