# Thread: Numbers adding together = bad

1. ## Numbers adding together = bad

Hi guys,

Still working on my calculator... now the problem I face is when you press the buttons...

this is how it works...

Code:
```switch (number)
{
case 0:
temp1 = temp1 + 1;
[totalField setFloatValue:temp1];
break;
default:
temp2 = temp2 + 1;
[totalField setFloatValue:temp2];
break;
}```
Ok, not sure if you all understand all of that, but basically that's for the button 1. When you press it, depending on what number of 2 in the calculation you'll be using you it adds it to a float, either temp1 or temp2. The problem is of course if you press "1, 2, 3" it doesn't make "123" it makes 6, cos it adds them up. How do you tell it to put them in sequence instead of adding up?

2. Either use strings in your combo box and convert to floats in calculations or just multiply your temp by 10 everytime you input a new number. The latter can make decimals problematic though.

3. i'm not sure how to do that... i'm really new to C

4. So new to C that you're still calling "Objective C using Cocoa" C. In the C language the code your showing would show no signs of using buttons of interfaces what so ever. Besides the fact, the second option I gave you was basic math. Surely you can do that.

5. no, no I can do the second thing, infact I did that as soon as I read what you posted. However I mean I don't know how to do the first option... Sorry for not showing it's on a button, I didn't think that was nescessary to show you...

here's the complete code for it:

Code:
```- (IBAction)button1:(id)sender
{
switch (number)
{
case 0:
temp1 = temp1 * 10 + 1;
[totalField setFloatValue:temp1];
break;
default:
temp2 = temp2 * 10 + 1;
[totalField setFloatValue:temp2];
break;
}
}```
That's when you press the button, as you can see I included the *10 into it.
This is the code to show that totalField is a text field:

Code:
`    IBOutlet NSTextField *totalField;`
Also, it is C code, mostly... it uses different things to regular C code... It even compiles using GCC...

6. Code:
```- (IBAction)button1:(id)sender
/*...*/
[totalField setFloatValue:temp1];```
That is not C. It's Objective C (I think) which GCC might compile.
Also, it is C code, mostly...

Code:
`temp2 = temp2 * 10 + 1;`
is the same as
Code:
`temp2 = (temp2 * 10) + 1;`

Popular pages Recent additions