I am creating a very simple calculator. To save the first user inputed float, i save it as a string in the addition action as to save it when they click on the addition button. Then later I call upon it again to add it to the second user inputed float. However when i call upon it again it gives the error: Use of undefined identifier num1. The same thing happens with the operation integer. Here is the relevant code:
- (IBAction)addition {
NSString *number1 = total.text;
float num1 = [number1 floatValue];
int operation = 1;
total.text = @"";
}
- (IBAction)equal {
NSString *number2 = total.text;
float num2 = [number2 floatValue];
if (operation == 1) {
int num3 = num1 + num2;
NSString *znumber1 = [NSString stringWithFormat:@"%f", num1];
}
You need to declare
num1as an “instance variable” so it’s accessible by both functions (usually declared within your@interfaceblock).The way you’re declaring it only allows for the code within that function’s scope to access it.
I would reccomend reading up on variable scope before continuing.