# Sending values to a control

• 12-02-2005
Zyk0tiK
Sending values to a control
Hey guys :)

I'm really new to C coding, only been doing it a day or so... but i'm trying to create a calculator, I've never been able to really figure a way out of doing it properly before, so I thought I might as well try :)

I use a Mac so i'm using Xcode with Cocoa, this is my problem... When the "EQUALS" button is pressed I want it to send 3 values to my controller which then works out what calculation to do and sends the correct value back. I'm not sure the code on how to do that works so think someone could help? I know this probably isn't the right way of doing things but I wanna sorta figure things out, make new calculators, learn new ways of doing things, etc. And this is my current one.

Ok, the code:

Code:

```- (IBAction)buttonEquals:(id)sender {         number1 = 5;         number2 = 5;                 totals = [Calculate: number1 number2 type]; // This is clearly where it's going wrong :/                 [totalField setFloatValue:totals];         }```
That's the button that wants to send the values to the control to add the values.

Code:

```#import <Cocoa/Cocoa.h> int total; @interface Calculate : NSObject {         switch (type)         {                 case 1:                         total = number1 + number2;                         break;                                         case 2:                         total = number1 - number2;                         break;                                         case 3:                         total = number1 * number2;                         break;                                         case 4:                         total = number1 / number2;                         break;                                 }         return total;         } @end```
And that's my feeble attempt at making the function to calculate the appropriate values.

Hope you can help!

edit: oh, i'm using fixed values for the calculations just to get the first part working correctly :P
• 12-02-2005
quzah
Ok, this is C how again?

Quzah.
• 12-02-2005
Zyk0tiK
Haha hmm, well it's Objective-C using Cocoa, I guess it's not standard C the way you're used to it...
• 12-02-2005
SlyMaelstrom
Well, I'd be willing to bet if this forum was called "Objective-C using Cocoa" alot of people wouldn't be here.

Your switch statement looks alright, anyway. :)
• 12-02-2005
quzah
Of course you may want to add a check to make sure you don't divide by zero. ;)

Quzah.
• 12-02-2005
Zyk0tiK
No, the switch statement is fine,

It's just the statement i'm using to TRY and send 3 values to my calculation controller....

Code:

`        totals = [Calculate: number1 number2 type]; // This is clearly where it's going wrong :/`
that bit. I'm basically trying to say "on the function named "Calculate" send it 3 values, number1, number2 and type."

How would you do that in ANSI C?
• 12-02-2005
Zyk0tiK
Woo hoo I got it! :D:D

incase anyone cares:

Code:

`totals = [calculation calculateValue:number1 with:number2 calcType:type];`
is the cocoa way to send values to a controller :P it probably means nothing to any of you though :P