Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 846307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:35:34+00:00 2026-05-15T06:35:34+00:00

I’m trying to make a simple calculator application in cocoa. The program hangs when

  • 0

I’m trying to make a simple calculator application in cocoa. The program hangs when I click on one of my buttons. I think I’ve traced the problem to the part of my controller that adds a digit to the end of the number currently on the display:

- (void)updateNumber:(int)buttonClicked{
 *self.activeNumberPointer = *self.activeNumberPointer * 10 + buttonClicked;
 [outputField setFloatValue:*self.activeNumberPointer];
}

I used a pointer to the “activeNumber” in order to allow my program to tell which of the two operands I’m editing.

Any help appreciated, thanks.

(edit): My declaration and @property:

//CalculatorController.h
@interface CalculatorController : NSObject {
    IBOutlet NSTextField *outputField;
    float variable1, variable2;
    float *variable1Pointer, *variable2Pointer;
    float *activeNumberPointer;
}

float variable1 = 0;
float variable2 = 0;
float* variable1Pointer = &variable1;
float* variable2Pointer = &variable2;
float* activeNumberPointer = &variable1;

@property (readwrite) float variable1, variable2;
@property (readwrite) float *vairable1Pointer, *variable2Pointer;
@property (readwrite) float *activeNumberPointer;

...

Full XCode file available here: http://rapidshare.com/files/397664243/Calculator_2.zip
(FYI: I actually used leftNumberValue and rightNumberValue instead of variable1 and variable2 in the project)
Since I’m new to Objective-C and XCode, any general criticisms are welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:35:34+00:00Added an answer on May 15, 2026 at 6:35 am

    Not clear on pointers in objective-C with simple variable types, and not totally clear why you’re using pointers at all.

    *self.activeNumberPointer = *self.activeNumberPointer * 10 + buttonClicked;
    

    You are using the * operator (dereference) wrong on the left side of this expression. When doing assignment, you don’t want to dereference like that.

    http://www.cplusplus.com/doc/tutorial/pointers/

    Thats a C++ reference, but a good start for pointer logic.

    I would probably use an NSMutableArray or some construct like that (you want to store more than just two numbers right?), and use the integer index to point to the “current” number you’re displaying on the screen.

    Further, I don’t know if its a typo, or some missing code etc, but…

    @interface CalculatorController : NSObject {
        IBOutlet NSTextField *outputField;
        float variable1, variable2;
        float *variable1Pointer, *variable2Pointer;
        float *activeNumberPointer;
    }
    
    float variable1 = 0;
    float variable2 = 0;
    float* variable1Pointer = &variable1;
    float* variable2Pointer = &variable2;
    float* activeNumberPointer = &variable1;
    

    … you seem to be declaring all the variables above in the interface definition as ivars — or instance variables that are directly attached to your CalculatorController, and then outside of the interface definition, you are redeclaring these as GLOBAL variables. There’s no need for the second set of definitions to use these in your implementation

    If you are looking to initialize these instance variables, commonly there is a function called -init that you can override (since you are subclassing NSObject), and its akin to a constructor in other languages. This would live in your implementation file (.m or .mm). It might look something like this:

    -(CalculatorController *)init{
       if( self = [super init] ){
           // Do your initialization here etc
       }
       return self;
    }
    

    There’s also another function -awakeFromNib that you can override if this is being instantiate inside a XIB/NIB file as part of your user interface definition. The -awakeFromNib function gets called when the class and the UI have been loaded.

    Edit: to be clear about the pointer business….
    self.activeNumberPointer = *self.activeNumberPointer * 10 + buttonClicked;

    This is right. You DON’T use the dereference operator on the left hand side, but you DO use the dereference operator on the right hand side, as you are looking for the value of self.activeNumberPointer.

    Josh

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 439k
  • Answers 439k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is no file dialog available from the Qt designer… May 15, 2026 at 4:53 pm
  • Editorial Team
    Editorial Team added an answer Hold down the Shift key and press Enter (Shift+Enter) May 15, 2026 at 4:53 pm
  • Editorial Team
    Editorial Team added an answer Using your notation: eax@0 = ... whatever it was before… May 15, 2026 at 4:53 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.