Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective C: looping issue
    primarykey
    data
    text
    <p>I'm just starting out with objective c (coming from java) and I'm working on a calculator program just to practice with the syntax and some basic stuff. The way I'm going about it is having the user input a string and looking through for operators (taking order of operations into account) and then finding the term surrounding that operator, calculating it, replacing the term with the answer, and repeating for all the terms; however, I'm having an issue with the method I'm using to calculate the term. I pass in the index of the operator and have it loop backwards until it hits another operator to find the number immediately before it, and do the same forwards for the number after. My issue is that the loop does not stop when it hits the operators, and instead just continues until the end of the string in both directions. It's probably something really simple that I've overlooked but I've been trying to figure this out for a while and can' seem to get it. I've included an SSCCE of just the first half of the method, with a predetermined string and operator index. (also, a secondary question: is there any better way to post code blocks on this site rather than manually putting in 4 spaces before every line?)</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; int firstNumInTerm(int index); NSString *calculation; int main(int argc, const char * argv[]) { @autoreleasepool { calculation = @"51-43+378*32"; int firstNumber = firstNumInTerm(9); NSLog(@"The number before the term is: %i", firstNumber); } return 0; } int firstNumInTerm(int index){ int firstNumIndex = index - 1; int firstNumLength = 1; NSRange prevChar = NSMakeRange(firstNumIndex - 1, 1); while ([calculation substringWithRange:prevChar] != @"*" &amp;&amp; [calculation substringWithRange:prevChar] != @"/" &amp;&amp; [calculation substringWithRange:prevChar] != @"+" &amp;&amp; [calculation substringWithRange:prevChar] != @"-" &amp;&amp; firstNumIndex &gt; 0) { NSLog(@"prevChar: %@", [calculation substringWithRange:prevChar]);//TEST firstNumIndex--; firstNumLength++; prevChar = NSMakeRange(firstNumIndex - 1, 1); } NSRange firstRange = NSMakeRange(firstNumIndex, firstNumLength); int firstNum = [[calculation substringWithRange:firstRange] intValue]; NSLog(@"firstNum String: %@", [calculation substringWithRange:firstRange]);//TEST NSLog(@"firstNum int: %i", firstNum);//TEST return firstNum; } </code></pre>
    singulars
    1. This table or related slice is empty.
    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