views:

1000

answers:

3

I know how to do a global variable, but whenever I try to define a global variable with a random number function, xcode says "initializer element is not constant." The compiler doesn't want to make a variable from a random number because the random number function is not constant.

How do I generate a random number and then use that same value for more than one action? (For example, to define a color and then write that value to a label?)

Code:

#import "Slider_with_IBAppDelegate.h"

float * const hue = ((arc4random() % ((unsigned)100 + 1))/100.0);

//^this is where I get the error: "initializer element is not constant"

@synthesize label

//write value to label
- (IBAction) doButton {
label.text = [NSString stringWithFormat:@"%f", hue];
}

//set background color
- (void)applicationDidBecomeActive:(UIApplication*)application
{
self.label5.backgroundColor = [UIColor colorWithHue:hue            
                                             saturation:1.0           
                                             brightness:1.0            
                                                  alpha:1.0];
}

----edit------

Thanks for the suggestions. It still doesn't work for me, though, what am I doing wrong?

New code:

#import "Slider_with_IBAppDelegate.h"

float const hue = ((arc4random() % ((unsigned)100 + 1))/100.0);

//^I still get the error: "initializer element is not constant."

@synthesize label

//write value to label
- (IBAction) doButton {
label.text = [NSString stringWithFormat:@"%f", hue];
}  
//^this is where I get the error "'hue' undeclared (first use of this function)"

//set background color
- (void)applicationDidBecomeActive:(UIApplication*)application
{

hue = ((arc4random() % ((unsigned)1000 + 1))/1000.0);
/*here I get the error "assignment of read-only variable 'hue.'"  
If I insert "float" just before hue, I do not get this error, 
but it still won't compile because of the error above.*/

self.label5.backgroundColor = [UIColor colorWithHue:hue                        
                                         saturation:1.0                                         
                                         brightness:1.0                                          
                                              alpha:1.0];
}
+3  A: 

Make it non-const and initialize it in applicationDidBecomeActive. Is there a reason it must be constant?

alltom
I tried that but then I couldn't call the same value for the label. It gave me the error "function undefined: first use of this function" in doButton even though it was defined in applicationDidBecomeActive.
Evelyn
What am I doing wrong?
Evelyn
I meant for you to keep the hue declaration where it was (without the pointer *, like Peter said), but move the assignment to applicationDidBecomeActive: float hue; ... - (void)applicationDidBecomeActive:(UIApplication*)application { hue = ((arc4random() % ((unsigned)1000 + 1))/1000.0);That way both functions can see the declaration of hue, but it's given a new value every time the application is launched.
alltom
I didn't realize that wouldn't format properly. I hope my explanation got through even though my code didn't. :)
alltom
Thanks for not giving up on me. :)It still doesn't work. Even when I do exactly that, I now get a new message that says "error: assignment of read-only variable 'hue'" under applicationDidBecomeActive. I'll post the edits.
Evelyn
The "const" at the top makes the variable read-only, so it can only be given a value once. Remove the "const" and the "((arc4random() ..." from the top leaving "float hue". hue will still be visible to every function, but its value can be changed whenever you want. This should fix your errors. [Also, when you put "float" before "hue" in applicationDidBecomeActive, that creates another variable called "hue" just inside that function, which is why that makes the function compile but not everything else.]
alltom
A: 

I know how to do a global variable, but whenever I try to define a global variable with a random number function, xcode says "incompatible types in initialization."

float * const hue = ((arc4random() % ((unsigned)100 + 1))/100.0);

That's not a function; it's an expression. I'd be surprised if you're not also getting an error here, because you can't initialize a global variable with an expression that isn't constant. As alltom.com says, you need to assign to it from applicationDidBecomeActive:.

The warning is because you've given the variable a pointer type (float *), but you're not assigning a pointer to it. Cut out the asterisk, because you're not going to put a pointer in this variable.

Xcode doesn't want to make a variable from a random number because the random number function is not constant.

Xcode doesn't care one way or the other. It's just reporting the findings of the compiler. By default, the compiler for Objective-C is GCC, but Xcode supports other compilers (and Xcode does come with one other C/Objective-C compiler: LLVM-GCC).

… I couldn't call the same value for the label.

You're not showing a label here, and you can't call a value. You can only call a function, and you don't have one in the code shown.

It gave me the error "function undefined: first use of this function" in doButton even though it was defined in applicationDidBecomeActive.

No, it wasn't. Assigning to a variable does not create a function.

Peter Hosey
Thanks for helping me get my terms straight. :)Yes, I am getting an error after the global variable because of the fact that the random number isn't constant. That's the whole point of my question.The label is defined in the header file. doButton writes text to the label--all I want is to print the hue value of label5.I did try initializing it in applicationDidBecomeActive, I just didn't show that version of the code here. Maybe I just missed something. I'll try it again.
Evelyn
A: 

In case anyone is wondering, I finally found a way to do this effectively. (I am sure this is what alltom was saying, I was just too dumb to understand.)

I declared a float and a seed in my .h file:

- (float)generate:(id)sender;
- (void)seed;

And in the implementation file, I defined the float as a random number, and I used srandom() as a random seed generator.

- (float)generate:(id)sender
{

//Generate a number between 1 and 100 inclusive
int generated;
generated = (random() % 100) + 1;

return(generated);
} 

- (void)seed {
srandom(time(NULL));
}

Then anywhere I wanted to retain a random number, I used

srandom(time(NULL));
generated1 = ((random() % 100) + 1)/100.0;

to initiate the number, and from there I was able to use generated1, generated2, hue, etc. as variables in any function I wanted (and I made sure to declare these variables as floats at the top of the file).

Evelyn