msp301 Posted March 21, 2009 Share Posted March 21, 2009 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]; Quote Link to comment Share on other sites More sharing options...
SomethingToChatWith Posted March 21, 2009 Share Posted March 21, 2009 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... Quote Link to comment Share on other sites More sharing options...
msp301 Posted March 21, 2009 Author Share Posted March 21, 2009 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 Quote Link to comment Share on other sites More sharing options...
stingwray Posted March 21, 2009 Share Posted March 21, 2009 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. Quote Link to comment Share on other sites More sharing options...
freeb Posted March 22, 2009 Share Posted March 22, 2009 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. Quote Link to comment Share on other sites More sharing options...
SomethingToChatWith Posted March 22, 2009 Share Posted March 22, 2009 Freeb, how would you do a string variable in C anyway? string variablename = "some text"; ? Quote Link to comment Share on other sites More sharing options...
msp301 Posted March 22, 2009 Author Share Posted March 22, 2009 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]; Quote Link to comment Share on other sites More sharing options...
freeb Posted March 22, 2009 Share Posted March 22, 2009 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 Quote Link to comment Share on other sites More sharing options...
SomethingToChatWith Posted March 22, 2009 Share Posted March 22, 2009 So I must use a char array and cycle through it to compose strings? Or would this do?... char *a = "some text"; Where whenever I refer to *a in code it would return its string literal? Edit: ok, I guess you just use the function there to assign it. Thanks. Quote Link to comment Share on other sites More sharing options...
freeb Posted March 23, 2009 Share Posted March 23, 2009 With char *a = "some text"; just be aware that it is read only. While you could do char first = a[0]; you could not do a[0] = 's'; However with char[] = "some text"; you can. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.