Jump to content

Objective C Problem


msp301
 Share

Recommended Posts

I'm trying to get this to generate a random number to then refer to a list of words that would then be outputted in a text field, but I keep getting the error that "assignment makes integer from pointer without cast" ... I'm new to this language, can anyone see what I'm doing wrong ??

Thanks again :)

               char text;
        int random_num = random() % 3;
        
        switch (random_num) {
            case 0:
                text = "dog";
                break;
                
            case 1:
                text = "cat";
                break;
                
            case 2:
                text = "rabbit";
                break;

            default:
                text = "ERROR";
                break;
        }
        
        self.string = text;
        NSString *wordString = string;
        NSString *greeting = [[NSString alloc] initWithFormat:@"%@!", wordString];
        label.text = greeting;
        [greeting release];

Link to comment
Share on other sites

Sorry, I'm not good with C, but what I did in VB was...

Make two arrays. One full of random numbers and the other with the actual information the application needs to use. Don't know about C but VB has a Array.Sort method that I use to sort the random array as the "key value", providing the second array as a second parameter, which well also sort that array's elements based on how the first array is sorted.

Hopefully that'll steer you into the right direction...

Link to comment
Share on other sites

Sorry, I'm not good with C, but what I did in VB was...

Make two arrays. One full of random numbers and the other with the actual information the application needs to use. Don't know about C but VB has a Array.Sort method that I use to sort the random array as the "key value", providing the second array as a second parameter, which well also sort that array's elements based on how the first array is sorted.

Hopefully that'll steer you into the right direction...

Thanks for your reply, but I don't want to create an array already containing "random" numbers because I want a pointer to be randomly generated which will then refer to a position in my list, which in this case is being defined via the use of a switch statement rather than an array as I was getting confused about how to refer to an array in Objective-C

Link to comment
Share on other sites

All I can see is that it looks like your variable 'text' is of type char not an array of chars which would make up a string. If thats the problem, your assigning a reference to that variable to a string in the switch statement, which it won't have any of.

Can't see anything else.

Making arrays of things is just plain horrible for this sort of problem! Especially having to use something like array sort on that, you'll be adding huge amount of overhead.

Link to comment
Share on other sites

Remember this is Objective-C not C.

/* I know for a fact that you can't do something like this
 in C EDIT: Actually I may be wrong >.< */
text = "mystring";

However this is Obj-C of which I'm not familiar with, Sorry.

/me wonders if TomB is around, he would be able to help you.

Link to comment
Share on other sites

Freeb, how would you do a string variable in C anyway?

string variablename = "some text"; ?

Thanks so much guys for all your help :) ... I've managed to correct my solution so that it works and thought I'd post my solution for anyone who would be interested to see

Thanks again

NSString *text;
        int random_num = random() % 3;
        
        switch (random_num) {
            case 0:
                text = @"dog";
                break;
                
            case 1:
                text = @"cat";
                break;
                
            case 2:
                text = @"rabbit";
                break;

            default:
                text = @"ERROR";
                break;
        }
        
        self.string = text;
        NSString *wordString = string;
        NSString *greeting = [[NSString alloc] initWithFormat:@"%@!", wordString];
        label.text = greeting;
        [greeting release];

Link to comment
Share on other sites

Freeb, how would you do a string variable in C anyway?

string variablename = "some text"; ?

/ * This is a simple way to initialise a character array, the compiler
    will work out of the length of the string and place it in the sqaure 
    brackets. */

char mystr[] = "this is my string"

/* Like I said in my previous post you cannot do text = "blaa blaah" 
    (EDIT: Again this may be wrong.)  You have to use the strcpy function. */

/* EDIT: make sure mystr has enough memory allocated to it to handle the
    length of the string your going to copy */
strcpy(mystr, "new string value");

Read more here -> http://www.iso-9899.info/wiki/StringsByExample and here ->http://www.iso-9899.info/wiki/String

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...