gmustudent
gmustudent

Reputation: 2199

Getting User Data From Console

Just started programming in Objective-C yesterday with a Java background and I am lost. I have a program that works, and it's purpose is to take in the name, age, and weight of a person from the console and then output those values back to the user. My problem is when I type the data into the console it will only show me the first letter that I type in. Then it will not show me any other characters of my string. So here is some sample console data to demonstrate my point.

Console:

2012-11-14 17:56:05.673 Tutorial[1757:403] Please Enter In Your Name
warning: this program uses gets(), which is unsafe.
C
2012-11-14 17:56:09.494 Tutorial[1757:403] Please Enter Your Age
1
2012-11-14 17:56:11.239 Tutorial[1757:403] Please Enter Your Weight
2
2012-11-14 17:56:13.205 Tutorial[1757:403] 
Name: Chris 
Age: 18 
Weight: 200

As you can see it will only show me the first letter of what I type but it actually uses the enter string. Why on earth is this happening?

Function Declarations:

#import "Person.h"

Person * readPersonData (Person * object);
void writePersonInformation(Person * object);

Main Method:

int main (int argc, const char * argv[])
{
    @autoreleasepool
    {
        Person * p1 = [[Person alloc] init];
        p1 = readPersonData(p1);
        writePersonInformation(p1);
    }
}

Function Implementations:

Person * readPersonData (Person * object)
{
    char nameCharacters[100];
    NSString * objectName;
    int objectAge, objectWeight;

    NSLog(@"Please Enter In Your Name");
    gets(nameCharacters);
    objectName = [[NSString alloc] initWithUTF8String:nameCharacters];
    [object setName : objectName];

    NSLog(@"Please Enter Your Age");
    scanf("%i", &objectAge);
    [object setAge : objectAge];

    NSLog(@"Please Enter Your Weight");
    scanf("%i", &objectWeight);
    [object setWeight : objectWeight];

    return object;
}
void writePersonInformation(Person  * object)
{
    NSLog(@"\nName: %@ \nAge: %i \nWeight: %i", object.getName, object.getAge, object.getWeight);
}

Upvotes: 1

Views: 1297

Answers (3)

trojanfoe
trojanfoe

Reputation: 122381

I think the issue relates to a problem with Xcode 4.5 (see this).

You don't say in your question whether you are running from the debugger console or from the command line, however what happens when you try it from the command line?

You should be using fgets() however, if for nothing else than to stop that runtime warning message.

Upvotes: 1

evanmcdonnal
evanmcdonnal

Reputation: 48076

Use fgets passing it stdin for the file instead of gets which is generally marked as unsafe by most compilers. This will at least removing the warning which is likely causing the input to not be displayed. Here is a post on how to use fgets

Safe Alternative to gets

Alternatively, you could try to suppress warnings, there is probably some compiler directive to do it (I know there is in VS). I used to do my own bounds checking on a lot of "unsafe" functions and found that to reduce some annoyance from the compiler.

Upvotes: 0

RonaldBarzell
RonaldBarzell

Reputation: 3830

gets() is known to have security issues due to the possibility of a buffer overrun, so the recommendation for quite a while was to use fgets() instead.

Upvotes: 1

Related Questions