Adding two numbers using 3 Text Field ??

Discussion in 'iOS Programming' started by int3rc3pt0r, Feb 9, 2010.

  1. int3rc3pt0r macrumors newbie

    Joined:
    Feb 9, 2010
    #1
    Hey Guys ,

    I am totally new to iPhone programming ..
    So please don't be hard on me :p

    Ok here is the problem

    AdditionViewController.h
    Code:
    #import <UIKit/UIKit.h>
    
    @interface AdditionViewController : UIViewController 
    {
    	IBOutlet UITextField *Number1;
    	IBOutlet UITextField *Number2;
    	IBOutlet UITextField *SumAnswer;
    }
    
    @property (retain, nonatomic) UITextField *Number1;
    @property (retain, nonatomic) UITextField *Number2;
    @property (retain, nonatomic) UITextField *SumAnswer;
    -(IBAction)buttonPressed1:(id)sender;
    
    @end
    

    AdditionViewController.m
    Code:
    #import "AdditionViewController.h"
    
    @implementation AdditionViewController
    @synthesize Number1;
    @synthesize Number2;
    @synthesize SumAnswer;
    
    
    -(IBAction)buttonPressed1:(id)sender
    {
    	SumAnswer.text = Number1.text + Number2.text; 
           // I want the above line to be edited so that I can get the addition
             of both the numbers in the 3rd Text Field 
    }
    
    -
    -
    -
    -
    -
    @end
    
    I am really new to iPhone programming .. I am trying to understand how
    NSString can be used here .. But I am finding it a bit complicated .


    If someone please tell me what the code should be ... It will be a great help .
    I have to submit a mini project to my school asap .

    Thanking You ,
    int3rc3pt0r
     
  2. int3rc3pt0r thread starter macrumors newbie

    Joined:
    Feb 9, 2010
    #3
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    You say you're not but it certainly looks that way. The code you're looking for can be found, with some adjustments, in those threads. Don't expect us to do your homework for you.
     
  4. int3rc3pt0r thread starter macrumors newbie

    Joined:
    Feb 9, 2010
    #5
    :( .. Ok .. I will try that ...
    If I get any error I will post it here ...
    Thanks ...
     
  5. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    Basically you can't add NSString objects using the binary + operator: it only works on numeric primitive types.
     
  6. int3rc3pt0r thread starter macrumors newbie

    Joined:
    Feb 9, 2010
    #7
    So how am I supposed to do it without NSString ??
     
  7. ChOas macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #8
    1: Convert the NSStrings to integers/floats
    2: Add the numbers
    3: Convert the result back to an NSString.

    pseudo: sumanswer = string((num1 as float) + (num2 as float));

    It's all explained in the links already given.
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #9
    You'll need to convert the NSString objects into something you can do maths on. The documentation or the threads linked above will help you. I'm certainly not about to tell you: learning to program is not copy/pasting from what others do, it's about understanding what is happening, building a mental model of how the system works and how your code fits into that. Personally I'd suggest learning plain, basic C as that will teach you a lot about how the machine works. You might actually then understand why @"10"+@"10" doesn't result in @"20". If Objective-C supported operator overloading (it doesn't) it might result in @"1010"...
     
  9. dalvin200 macrumors 68040

    Joined:
    Mar 24, 2006
    Location:
    Nottingham, UK
    #10
  10. int3rc3pt0r thread starter macrumors newbie

    Joined:
    Feb 9, 2010
    #11
    Hey thankx for the guidance guys ...
    I will go ahead with links ... I got the picture now ..thank a ton

    Thankx
     

Share This Page