MacUser
MacUser

Reputation: 491

UITextField int data type xcode

I am trying to get a mathmetical equation to recognise a + /- sign of an integer (either -1 or +1) entered in a UItextfield (s1, s2). So if the user enters different signs the equations will be subtracted from each other. It seems that the sign is not being recognised for some reason and the program just adds d1 and d2.

-(IBAction)calculateD:(id)sender{
float n1, r1, n2, r1, d, d1, d2;
int s1, s2;

s1= [textfieldS1.text intvalue]; //etc for all variables

d1 = s1 * ((n1-1)/r1);
d2 = s2 * ((n2-1)/r2);

if (s1 != s2) { d = d1 - d2;} 
else { d = d1 + d2;
}}

Any problems apparent in this code please?

Upvotes: 0

Views: 368

Answers (1)

lukasz
lukasz

Reputation: 771

I have no idea what you are trying to do here. Variables are not initialized and there is no specific reference to actual UITextField inside of the -calculateD: method. With this said, here are some hints, hope it will come in hand.

The signs s1, s2 are actually taken twice into a consideration. Once to produce d1, d2, and later to decide the (s1 != s2). Because of this, the latter will make sure you add two numbers of the same sign, possibly negating what you really want to obtain here. Example:

  • say that s1=+1, s1=+1, then you got d = ((n1-1)/r1) + ((n2-1)/r2);
  • say that s1=+1, s1=-1, then you got d = ((n1-1)/r1) + ((n2-1)/r2); the same as before;

Just drop the if, and leave a single: d = d1 + d2.

Upvotes: 2

Related Questions