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)
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.
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.
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;
}
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;
}
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!