Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective C Char Array Issue
    primarykey
    data
    text
    <p>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?</p> <pre><code>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 &lt; [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 &lt; count; i++) { for (int j = 0; j &lt; [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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload