This code gives me the error message "warning: passing argument 1 of 'stringWithCString:encoding:' makes pointer from integer without a cast"

#import <Cocoa/Cocoa.h>
#import <Foundation/NSObject.h>
#import <stdio.h>
//#import <string.h>


@interface Numbers : NSObject
{
    NSString *name;
    int age;
    NSMutableArray *list;
}
//-(void) setName;
-(void) setAge :(int)a;

@end



@implementation Numbers
-(void) setName{
//  NSLog(@"What is your name?");
    char n;
    printf("What is your name? ");
    scanf("%s", &n );
//  name = [NSString stringWithUTF8String:n];
    name = [NSString stringWithCString: n encoding: NSASCIIStringEncoding];
}
...

What could it be? Perhaps a header. I wish there was a better way to take input of strings. Using NSString is recommended over a regular cstring. I want to learn that because I plan on making more complex programs in the future.

This is broken in two different ways.

Firstly, you are using a single char (i.e. one byte only) to try to store all the user input. This is bad. You will trash memory. You need to read up on what a c-string is (i.e. an array of char) and how to use them.

Secondly, the NSString stringWithCString method wants a char pointer, and you're trying to feed it n, which is not a char pointer. It is a char. A char is not the same thing as a char pointer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.