title: Boolean function always returns true c++
body: <p>I have a public function <code>validInfixCheck()</code> that calls private function <code>isBalanced()</code>. </p>
  
  <p>The <code>isBalanced()</code> function should return false if the string has something other than + - * / or a digit. The issue that I'm having is that <code>isBalanced()</code> is ALWAYS returning true. The logic works fine if I have all the code under the same file, but not when I have an .h and .cpp.</p>
  
  <p>For Example, if I enter 2^(3+4) the function should return false because <code>^</code> is not valid but right now it is returning true.</p>
  
  <p>g++ calculatorMain.cpp CalculatorExp.cpp</p>
  
  <pre><code>Enter infix expression to evaluate:
  2^(3+4)
  This is what you entered: 2^(3+4)
  expression is well formed
  </code></pre>
  
  <p>calculatorMain.cpp</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include "CalculatorExp.h"
  #include&lt;iostream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  //prototype declarations
  string getInfixExpression();
  
  int main()
  {
      CalculatorExp calc; 
      string inputExpression;
      inputExpression = getInfixExpression();
  
      cout&lt;&lt;"This is what you entered: "&lt;&lt; inputExpression &lt;&lt;endl;
  
      calc.validInfixCheck(inputExpression);
  
      return 0;
  }
  
  string getInfixExpression()
  {
      string exp;
      cout&lt;&lt;"Enter infix expression to evaluate: "&lt;&lt;endl;
      cin&gt;&gt;exp;
  
      return exp;
  }
  </code></pre>
  
  <p>CalculatorExp.cpp</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include "CalculatorExp.h"
  #include&lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;stack&gt; 
  
  using namespace std;
  
  CalculatorExp::CalculatorExp()
  {
    //default constructor 
  }
  // public //
  
  // valid input
  bool CalculatorExp::validInfixCheck(string inputExpression)
  {
      // Checks string is well formed
      if(isBalanced(exp))
      {
      cout &lt;&lt; "expression is well formed" &lt;&lt; endl;
      }
      else
      {
          cout &lt;&lt; "expression is NOT well formed" &lt;&lt; endl;
      }
  
      // Check string has balanced ( )
      //...
  
      return 0;
  }
  
  // private //
  bool CalculatorExp::isBalanced(string exp)
  {
          int expLength = exp.length();
          bool balancedSoFar = true;
          int i = 0; // Tracks character position in string
  
          while(balancedSoFar &amp;&amp; i &lt; expLength)
          {
              char ch = exp[i];
              i++;
              if(!isdigit(ch))
              {
  
              switch (ch)
              {
  
                  case '+':
                  balancedSoFar = true;
                  break;
  
                  case '-':
                  balancedSoFar = true;
                  break;
  
                  case '*':
                  balancedSoFar = true;
                  break;
  
                  case '/':
                  balancedSoFar = true;
                  break;
  
                  case '(':
                  balancedSoFar = true;
                  break;
  
                  case ')':
                  balancedSoFar = true;
                  break;
  
                  default:
                  balancedSoFar = false;
                  break;
              }
              }
          }
          if(balancedSoFar)
          {
              return 1;
              // cout &lt;&lt; "exp is balanced" &lt;&lt; endl;
              // Probably run the check braces function here
          }
          else 
          {
              return 0;
              // cout &lt;&lt; "exp is not balanced" &lt;&lt; endl;
              // Probably run the program to enter a new exp again
          }
  }  // end isBalanced
  </code></pre>