Trouble Using a Switch Statement With a TextField

Joined
Aug 21, 2011
Messages
36
Reaction score
0
Points
6
Your Mac's Specs
Late 2010 MacBook Pro 13 Inch
Hey guys, :) this one is for the coders out there. I'm trying to make a calculator application that has input fields for two numbers and an operator. The only problem is whenever I try to check for certain operators it gives me the error, "Statement requires expression of integer type ('NSTextField *' invalid)"

This is my main file:
Code:
#import "PrecalculatorAppDelegate.h"
#import <AppKit/AppKit.h>

@implementation PrecalculatorAppDelegate

@synthesize window;


- (IBAction)findSolution:(id)sender {
    NSNumber* PCResult;
    
    [color="red"]switch (operatorEntered) [/color]{
        case '+':
            PCResult = [NSNumber numberWithFloat:([firstNumberEntered floatValue]+[secondNumberEntered floatValue])];
            NSRunAlertPanel(@"Answer", [PCResult stringValue], @"Ok", NULL, NULL);
            break;
            
        default:
            break;
    }
    
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
}

@end
(I've highlighted the error)

And this is my header:
Code:
#import <Cocoa/Cocoa.h>

@interface PrecalculatorAppDelegate : NSObject <NSApplicationDelegate> {
@private
    NSWindow *window;
    
    IBOutlet NSTextField *firstNumberEntered;
    IBOutlet NSTextField *operatorEntered;
    IBOutlet NSTextField *secondNumberEntered;
    IBOutlet NSButton *findSolution;
}

@property (assign) IBOutlet NSWindow *window;

@end

Help would be greatly appreciated! :D
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
Well, the ivar operatorEntered is an object pointer type. Casting it to an appropriate int type wouldn't solve your goal though because you'd be comparing an object structure to a C string.

You'll need to convert that text to a C string, or forget the switch statement and use an appropriate NSString comparison method such as isEqualToString:.
 
OP
C
Joined
Aug 21, 2011
Messages
36
Reaction score
0
Points
6
Your Mac's Specs
Late 2010 MacBook Pro 13 Inch
You mean like this:

Code:
- (IBAction)findSolution:(id)sender {
    NSNumber* PCResult;
    
    if ([(NSString*)operatorEntered isEqualToString:@"+"]) {
        PCResult = [NSNumber numberWithFloat:([firstNumberEntered floatValue]+[secondNumberEntered floatValue])];
        NSRunAlertPanel(@"Answer", [PCResult stringValue], @"Ok", NULL, NULL);
    }
    
}

It still isn't working. The alert isn't sending... could it have something to do with the PCResult variable?
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
The operatorEntered ivar is a NSTextField, so first you have to access the string in that field. Hint; NSTextField is subclassed from a NSControl which has a method named stringValue.

Also, to make sure you are entering the if statement as expected, place an NSLog() in it so you can see if it gets printed in the console. Or just create a break point and step through it to see where you are branching.
 
OP
C
Joined
Aug 21, 2011
Messages
36
Reaction score
0
Points
6
Your Mac's Specs
Late 2010 MacBook Pro 13 Inch
Thanks a bunch it works! :D

Code:
#import "PrecalculatorAppDelegate.h"
#import <AppKit/AppKit.h>

@implementation PrecalculatorAppDelegate

@synthesize window;


- (IBAction)findSolution:(id)sender {
    NSNumber* PCResult;
    
    if ([[operatorEntered stringValue] isEqualToString:@"+"]) {
        PCResult = [NSNumber numberWithFloat:([firstNumberEntered floatValue]+[secondNumberEntered floatValue])];
        NSRunAlertPanel(@"Answer", [PCResult stringValue], @"Ok", NULL, NULL);
    }
    
    else if ([[operatorEntered stringValue] isEqualToString:@"-"]) {
        PCResult = [NSNumber numberWithFloat:([firstNumberEntered floatValue]-[secondNumberEntered floatValue])];
        NSRunAlertPanel(@"Answer", [PCResult stringValue], @"Ok", NULL, NULL);
    }

    else if ([[operatorEntered stringValue] isEqualToString:@"*"]) {
        PCResult = [NSNumber numberWithFloat:([firstNumberEntered floatValue]*[secondNumberEntered floatValue])];
        NSRunAlertPanel(@"Answer", [PCResult stringValue], @"Ok", NULL, NULL);
    }
    
    else if ([[operatorEntered stringValue] isEqualToString:@"/"]) {
        PCResult = [NSNumber numberWithFloat:([firstNumberEntered floatValue]/[secondNumberEntered floatValue])];
        NSRunAlertPanel(@"Answer", [PCResult stringValue], @"Ok", NULL, NULL);
    }
    
    else {
        NSRunAlertPanel(@"Answer", @"Operator means +, -, *, or /. ;)", @"Ok", NULL, NULL);
    }


    
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
}

@end

If anybody wants it^! :D I do have one question though. I don't like the NSRunAlertPanel is there a way to make a textfield or a label where the answer can be written to?
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
You're welcome.

I do have one question though. I don't like the NSRunAlertPanel is there a way to make a textfield or a label where the answer can be written to?

How about a NSTextField? Use the setStringValue: method to set the text.

If you are using Interface builder to build the UI, then in the Tools screen, type 'text' in the search panel at the bottom to see what possibilities may exist. Notice that many are based on NSTextField. One of those allows for formatting numbers. If you choose that, click on the formatter icon of the field in your UI to set that up. I haven't actually used it.

Just to note in case it matters, I'm using IB 3.2.6.
 
OP
C
Joined
Aug 21, 2011
Messages
36
Reaction score
0
Points
6
Your Mac's Specs
Late 2010 MacBook Pro 13 Inch
Wow thanks a bunch! :D I finally got it doing what I wanted it to do and the setStringValue: is so much easier than the NSRunAlertPanel! :D

Sorry for the edit but I was wondering if you wouldn't mind answering one final (hopefully) question? I need a way to check if the user has entered in any character other than the digits 0-9. I originally had:

Code:
if ([firstNumberEntered floatValue] == 0 || [secondNumberEntered floatValue] == 0){
        NSRunAlertPanel(@"Error", @"Error: Please enter a valid number", @"Ok", NULL, NULL);
    }

And then the else statement was to do the operations. But the problem with this is that not only does it run the alert if a user enters in other text but it runs the alert if a user enters in 0 for the first or second number.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top