Amazon.com Widgets What's wrong here
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 19, 2013, 08:33:10 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  iOS Programming
| |-+  General Questions
| | |-+  What's wrong here
Pages: [1]   Go Down
Print
Author Topic: What's wrong here  (Read 386 times)
Alderon
Newbie
*
Posts: 2


Email




« on: February 19, 2013, 07:40:57 PM »

Hi guys, I'd like your help in the following code:
header:
Code: (Objective-C)
#import <Foundation/Foundation.h>

@interface ActiveDirectory : NSObject
{
    char nome;
    char sobrenome;
    int num;
}
-(void)login;

@end
Method
Code: (Objective-C)
#import "ActiveDirectory.h"

@implementation ActiveDirectory
-(void)login{
    NSLog(@"Digite o seu nome");
    scanf("%s", &nome);
   
    NSLog(@"Digite seu sobrenome");
    scanf("%s", &sobrenome);
   
    NSLog(@"Digite um numero:\n1- Boa pratica 1\n2 -Boa pratica 2\n3- Boa pratica 3\n4- Boa pratica 4\n");
    scanf("%d",&num);
   
    NSString *result = [[NSString alloc]  initWithCString:&(nome) encoding:NSUTF8StringEncoding];
    NSString *result1 = [[NSString alloc] initWithCString:&(sobrenome) encoding:NSUTF8StringEncoding];
   
    switch (num) {
        case 1:
        {
            unichar c = [result characterAtIndex:0];
            result = [[NSString stringWithCharacters:&c length:1] lowercaseString];
            result1 = [result1 lowercaseString];
            result = [result stringByAppendingString:result1];
            NSLog(@"%@",result);
            break;
        }
        case 2:
        {
            unichar c = [result1 characterAtIndex:0];
            result = [result lowercaseString];
            result1 = [[NSString stringWithCharacters:&c length:1] lowercaseString];
            result = [result stringByAppendingString:result1];
            NSLog(@"%@",result);
            break;
        }
        case 3:
        {
            result = [result lowercaseString];
            result1 = [result1 lowercaseString];
            result = [result stringByAppendingString:result1];
            NSLog(@"%@",result);
            break;
        }
        case 4:
        {
            result = [result lowercaseString];
            result1 = [result1 lowercaseString];
            result1 = [result1 stringByAppendingString:result];
            NSLog(@"%@",result1);
            break;
        }
        default:
            NSLog(@"Numero invalido.Digite novamente:");
            scanf("%d",&num);
            break;
    }
}
@end
Main:
Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "ActiveDirectory.h"

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

    @autoreleasepool {
       
        // insert code here...
        ActiveDirectory *segu;
        segu = [[ActiveDirectory alloc] init];
        [segu login];
        NSLog(@"Hello, World!");
       
    }
    return 0;
}


Why am I getting wrong results with this ?

Thanks in advance.
Logged
applefan90
Newbie
*
Posts: 29






« Reply #1 on: February 22, 2013, 10:24:33 PM »

Code: (Objective-C)
@interface ActiveDirectory : NSObject  

    char nome; 
    char sobrenome; 
    int num; 
}

C character (char) variables can only store a single character.  For instance, this would work:

Code: (Objective-C)
char nome = 'a';
char sobrenome = 'z';

C strings, on the other hand, must be used in one of three ways.  For example:

Code: (Objective-C)
char *cp = "test";
char cp2[] = "test2";
char cp3[] = { 't', 'e', 's', 't' };
printf("%s %s %s\n", cp, cp2, cp3);

The first is simply a character pointer.
The second is a character array.
The third is also a character array, but shows that each letter is an individual character (char) (hence why it is called a character array).

So, to answer your question, to fix your code you should change your instance variable declarations in your @interface to this:

Code: (Objective-C)
@interface ActiveDirectory : NSObject  

    char *nome; 
    char *sobrenome; 
    int num; 
}

or this:

Code: (Objective-C)
@interface ActiveDirectory : NSObject  

    char nome[]; 
    char sobrenome[]; 
    int num; 
}

(you can mix and match between using *nome and nome[] any way you like. 

To reiterate, the reason why your code is not working is that char variables can only store a single character (one letter, symbol, etc.).

 Smiley
Logged
Alderon
Newbie
*
Posts: 2


Email




« Reply #2 on: February 23, 2013, 12:54:30 PM »

Thanks man, that really helped. Just one thing: the pointer version did not work only the array version and i had to declare the length of the array up front. Do you know why ?

Logged
applefan90
Newbie
*
Posts: 29






« Reply #3 on: February 23, 2013, 01:32:19 PM »

I really am not sure. Later I will copy your code into an Xcode project and play with it to see if I can get it working (posting from my phone right now).

In the mean time, here are a few things I would look at:

The method initWithCString:
You are doing something like this:
Code: (Objective-C)
myString = [NSString alloc] initWithCString:&(nome)]...

Change all those occurrences to this:

Code: (Objective-C)
myString = [NSString alloc] initWithCString:nome]...

You are trying to pass a pointer to a pointer, whereas the method is expecting simply a pointer.

Another thing to try: (I am not extremely familiar with the intricacies of snanf() though)
Change your scanf calls that capture your C strings to this.

Code: (Objective-C)
scanf("%c", &myCharPointerVar);

Again, I am not 100% sure about this last one, but it is something to try.
« Last Edit: February 23, 2013, 01:34:38 PM by applefan90 » Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.