Challenge 2: "Passing argument 1 of 'atoi' from

I cannot get this code to work. The program will run, but it skips all of the numbers and returns “null” and then “Found One!”.

Also, i get the error message “Passing argument 1 of ‘atoi’ from incompatible pointer type”. This message is referring to the line that has this: int num=atoi(string)

Here is the code:

#import <Foundation/Foundation.h>
#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>

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

NSLog(@"Where should I start counting? \n\n\n");	

// get user input
const char *number =readline(NULL);

// turn user input string into NSString
NSString *string=[NSString stringWithUTF8String:number];

int num=atoi(string);
NSInteger i=[string integerValue];
for (i=num; i>-1; i-=3) {
	NSLog(@"%@\n",i);
	if (i%5 ==0) {
		NSLog(@"Found One!");
	}
}



[pool drain];
return 0;

}

This is the code you wanted to write:

#include <readline/readline.h>

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    NSLog (@"Where should I start counting? \n\n\n");
    
    // get user input
    const char *number = readline (NULL);
    
    const long num = atoi (number);
    
    for (long i = num; i>-1; i-=3) {
        NSLog (@"%ld\n", i);
        if (i%5 == 0) {
            NSLog (@"Found One!");
        }
    }
    
    [pool drain];
    return 0;
}

Or this:

#include <readline/readline.h>

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    NSLog (@"Where should I start counting? \n\n\n");
    
    // get user input
    const char *number = readline (NULL);
    
    // turn user input string into NSString
    NSString *string = [NSString stringWithUTF8String:number];
    
    const long num = [string integerValue];
    
    for (long i = num; i>-1; i-=3) {
        NSLog (@"%ld\n", i);
        if (i%5 == 0) {
            NSLog (@"Found One!");
        }
    }
    
    [pool drain];
    return 0;
}

This is my solution,
Used stringWithUTF8String like the book instructed, and then Googled for the function to convert NSString to Integer:

[code]#import <Foundation/Foundation.h>
#include <readline/readline.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {
NSLog(@“Were should I start counting?”);
const char * input = readline(NULL);
NSString *startNum = [NSString stringWithUTF8String:input];
NSLog(@"%@", startNum);

   //Convert NSString to Integer
    int firstNum = [startNum intValue];
    
    do {
        int countDown = firstNum-3;
        NSLog(@"%d", countDown);
        if (countDown % 5 ==0){
            printf("Found One!\n");
        }
        firstNum-=3;
    }while (firstNum > 0);
    
    }
return 0;

}
[/code]

However I personally feel that this is not objective-C anymore, especially on the do while loop part, this is just C haha.
Any criticises are badly welcome. :smiley:

I took a slightly different approach from what a few others have posted. Instead of using a C string from the previous code, I simply passed it into the argument for the method stringWithUTF8String.

[code]#import <Foundation/Foundation.h>
#import <readline/readline.h>

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

    // Changed printf to use NSLog
    NSLog(@"Where should I begin counting?");
    
    // Used the provided code to pass in a C String command.
    NSString *userInput = [NSString stringWithUTF8String:readline(NULL)];

    // Converting the String to an Int, this was my solution which I had to google to find out how to convert the string to an int.
    NSInteger startingNumber = [userInput integerValue];
    
    // Loop is mostly the same but now featured NSLog and %ld for new int.
    while (startingNumber >= 0) {
        NSLog(@"%ld", (long)startingNumber);
        if (startingNumber % 5 == 0) {
            NSLog(@"Found one!");
        }
        startingNumber -= 3;
    }
return 0;
}

}
[/code]

Hi guys!

This is my solution for Challenge 2 which is pretty similar to others solution you have reached. My doubt is if I should use ‘NSInterger’ to convert ‘NSString’ into an ‘int’ data type or I should use ‘long’ instead.


#import <Foundation/Foundation.h>
#import <readline/readline.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog(@"Where should I start counting? ");
        
        /* get user input */
        const char *userInput = readline(NULL);
        
        /* convert strint into NSString */
        NSString *string = [NSString stringWithUTF8String:userInput];
        
        /* convert NSStrint into NSInteger */
        NSInteger i = [string integerValue];
        
        while (i >= 0) {
            NSLog(@"%ld\n", i);
            if(i%5 == 0) {
                NSLog(@"Found one!\n");
            }
            i -= 3;
        }
    }
    return 0;
}

[code]#import <Foundation/Foundation.h>
#import <readline/readline.h>

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

    NSLog(@"Where should I start counting?\n");
    
    // Get user input
    const char *userInput = readline(NULL);
    
    // Convert string to NSString
    NSString *string = [NSString stringWithUTF8String:userInput];
    
    // Convert NSString to NSinteger
    NSInteger startCount = [string integerValue];
    
    while (startCount >= 0) {
        NSLog(@"%ld",startCount);
        if (startCount % 5 == 0) {
            NSLog(@"Found one!\n");
    }
        startCount -= 3;
    }
}
return 0;

}
[/code]

Can someone tell me where I went wrong? I keep getting the “Build Failed” notification and I can’t figure out why! Any help would be very much appreciated!

@mistergarlee This was stopping me as well. Finally remembered, you have to link the libreadline.tbd library.

@drewjsch, you just have to do “int i;” or “long i;”, I think. You’ve switched i and num. I did it this way:

NSInteger num = [number intValue];
long i;
for (i =num …

Everything else looks good. Don’t forget to link the libreadline.tbd library.