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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:15:21+00:00 2026-06-14T10:15:21+00:00

Hey guys so I’m trying to learn objective c as an independent study for

  • 0

Hey guys so I’m trying to learn objective c as an independent study for my school and one of my projects just to practice is making a calculator program where the user inputs a string of numbers and operators and the program breaks it apart and does the calculation. Right now I have the input and I’m trying to have it find the operators and put them all in an array so that it can sort them to find order of operations and use the indices to find the different terms; however, when I try to print out the array to see if its holding the chars, it outputs some weird symbols like the apple logo or an upside down question mark. I’ve made an SSCCE of my problem — does anyone know whats going on?

int main(int argc, const char * argv[])
{
@autoreleasepool {

    NSString *calculation;
    char str[50] = {0};
    int count = 0;

    NSLog(@"What would you like to calculate?");
    scanf("%s", str);
    calculation = [NSString stringWithUTF8String:str];

    for (int i = 0; i < [calculation length]; i++) {
        NSRange range = NSMakeRange (i, 1);
        if([[calculation substringWithRange: range] isEqualToString: @"*"] ||
           [[calculation substringWithRange: range] isEqualToString: @"/"])
            count++;
    }

    char operators[count];
    for (int i = 0; i < count; i++) {
        for (int j = 0; j < [calculation length]; j++) {
            NSRange range = NSMakeRange (j, 1);
            NSString *s = [s initWithString:[calculation substringWithRange: range]];
            if([s isEqualToString:@"*"]){
                operators[i] = '*';
                break;
            }
            if([s isEqualToString:@"/"]){
                operators[i] = '/';
                break;
            }
        }
        NSLog(@"%c", operators[i]);
    }

}
return 0;
}
  • 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-14T10:15:22+00:00Added an answer on June 14, 2026 at 10:15 am

    To answer your question about what is going on in your code. The “weird symbols” you are seeing are the un-initialized values in memory at your operators[] array.

    To see the reason the char[] is left with garbage values look at this section of your code:

        ...
        NSString *s = [s initWithString:[calculation substringWithRange: range]];
        if([s isEqualToString:@"*"]){
            operators[i] = '*';
            break;
        }
        ...
    

    You are sending initWithString to s. s is not yet allocated. This results in s being a nil. And since sending a message like isEqualToString: to a nil will essentially evaluate NO even when an @"*" is in that range your assignment to operators[i] will never happen.

    Easy fix though, just replace the s assignment with this:

    NSString *s = [calculation substringWithRange:range];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys I can't seem to get the syntax here right, I'm just trying
Hey guys I am making a terminal application using Swing and Apache Commons. I
Hey guys I am making a 2D game and I have generating a random
Hey guys I just noticed that my join statement here SELECT * FROM reports
Hey guys, I'm currently trying to implement a function using C that takes in
Hey guys I'm trying to figure how pointers are returned by strcat(), so I
hey guys, how can I just achieve this simple layout? I'm hoping the answer
Hey guys, I'm trying to select a specific string out of a text, but
Hey guys here is my php: $x = array(one, two, three); foreach ($x as
Hey guys I am new to android development, I am currently making an application.The

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.