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 8343607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:07:08+00:00 2026-06-09T06:07:08+00:00

I’m brand new at programming and For my first attempt at an app, i’m

  • 0

I’m brand new at programming and For my first attempt at an app, i’m writing a basic calculator. Right now, I can make it do calculate using all 4 operators (+,-,*,/), but if I press equals a second time it crashes. How do I make it run the calculation again? For example, if i enter “2+2=4”, how do i press equals a second time to produce “6”, and then a third time to produce “8” and so on…

Here is what I have so far. I’m using a switch statement.

-(void)equalsButton:(id)sender

{
    second = [display.text integerValue];

    int result;

    NSArray* components;


    switch (operator)
    {
        case 0:
            components = [display.text componentsSeparatedByString:@"+"];
            first = [(NSString*)[components objectAtIndex:0] integerValue];
            second = [(NSString*)[components objectAtIndex:1] integerValue];
            result = first + second;
            break;
        case 1:
            components = [display.text componentsSeparatedByString:@"-"];
            first = [(NSString*)[components objectAtIndex:0] integerValue];
            second = [(NSString*)[components objectAtIndex:1] integerValue];
            result = first - second;
            break;
        case 2:
            components = [display.text componentsSeparatedByString:@"*"];
            first = [(NSString*)[components objectAtIndex:0] integerValue];
            second = [(NSString*)[components objectAtIndex:1] integerValue];
            result = first * second;
            break;
        case 3:
            components = [display.text componentsSeparatedByString:@"/"];
            first = [(NSString*)[components objectAtIndex:0] integerValue];
            second = [(NSString*)[components objectAtIndex:1] integerValue];
            result = first / second;
            break;
            }

    NSString * result1 = [NSString stringWithFormat:@"%i",result];
    display.text = result1;


}
  • 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-06-09T06:07:09+00:00Added an answer on June 9, 2026 at 6:07 am

    From what I’m gathering, you are using that display field as both an input and an output. So when you compute the first time, it now has the result only and you lost the operator and second operand, which you are trying to re-apply to the result.

    So I would say that one thing you could do if you don’t want to make too many changes is to store the operator and the second operand in instance variables every time you compute the result. That way, if you can’t determine the operator, it means you want to recall the last one. So essentially it would be adding a new case in your switch statement for:

    • use display.text as the first operand
    • use the last saved operator as the operator
    • use the last saved value as the second value

    Once you get to that point, you will probably want to merge redundant code into an evaluate:(int)operator a:(int)a b:(int)b method or something along those lines so you don’t have to repeat all the eval code for all the operators in that new case block.

    update

    I don’t have XCode on my work computer so I can’t try it, plus I don’t have the rest of your code, but try this. It should work for +. If it does, replicate for the other operators.

    -(void)equalsButton:(id)sender
    
    {
    int result;
    
    NSArray* components;
    
    
    switch (operator)
    {
        case 0:
            components = [display.text componentsSeparatedByString:@"+"];
            first = [(NSString*)[components objectAtIndex:0] integerValue];
            if([components count] > 1) {
                second = [(NSString*)[components objectAtIndex:1] integerValue];
            }
            result = first + second;
            break;
        case 1:
            components = [display.text componentsSeparatedByString:@"-"];
            first = [(NSString*)[components objectAtIndex:0] integerValue];
            second = [(NSString*)[components objectAtIndex:1] integerValue];
            result = first - second;
            break;
        case 2:
            components = [display.text componentsSeparatedByString:@"*"];
            first = [(NSString*)[components objectAtIndex:0] integerValue];
            second = [(NSString*)[components objectAtIndex:1] integerValue];
            result = first * second;
            break;
        case 3:
            components = [display.text componentsSeparatedByString:@"/"];
            first = [(NSString*)[components objectAtIndex:0] integerValue];
            second = [(NSString*)[components objectAtIndex:1] integerValue];
            result = first / second;
            break;
            }
    
    NSString * result1 = [NSString stringWithFormat:@"%i",result];
    display.text = result1;
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
We're building an app, our first using Rails 3, and we're having to build
I am writing an app with both english and french support. The app requests
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I am using Paperclip to handle profile photo uploads in my app. They upload

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.