Simple Bool Function Malfunction

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Randeh
    New Member
    • Feb 2007
    • 16

    #1

    Simple Bool Function Malfunction

    Hey, I'm guessing my bool never returns true for some reason. There's no compiler error, it just continually prints back that the operator is invalid, even when the user enters a valid operater. I know I'm missing something glarlingly obvious in this section of my program, but I just can't spot it for the life of me:

    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    bool OpStatus(false);
    char TestOp, UseOp;
    int i;
    void IsValidOp(char TestOp, int i);
    
    void main()
         {	
         while (1) 
              {						                   
              cout << "Enter desired operation: ";
              cin >> TestOp;
    
              IsValidOp(OpStatus,i);
    	if (OpStatus==false)
    	{
                    cout << "Invalid operator. Try again." << endl;
    	continue;
    	} else TestOp = UseOp;
              }
         }
    
    
    void IsValidOp(char TestOp, int i)
    	{
    	char ValidOp[] = "+-*/cCxX";
    	for (i=0; OpStatus=false, (i<strlen(ValidOp)); i++)
    		{
    		if (TestOp==ValidOp[i])
    			OpStatus = true;
    		}
    	}
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by Randeh
    Hey, I'm guessing my bool never returns true for some reason. There's no compiler error, it just continually prints back that the operator is invalid, even when the user enters a valid operater. I know I'm missing something glarlingly obvious in this section of my program, but I just can't spot it for the life of me:

    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    bool OpStatus(false);
    char TestOp, UseOp;
    int i;
    void IsValidOp(char TestOp, int i);
    
    void main()
         {	
         while (1) 
              {						                   
              cout << "Enter desired operation: ";
              cin >> TestOp;
    
              IsValidOp(OpStatus,i);
    	if (OpStatus==false)
    	{
                    cout << "Invalid operator. Try again." << endl;
    	continue;
    	} else TestOp = UseOp;
              }
         }
    
    
    void IsValidOp(char TestOp, int i)
    	{
    	char ValidOp[] = "+-*/cCxX";
    	for (i=0; OpStatus=false, (i<strlen(ValidOp)); i++)
    		{
    		if (TestOp==ValidOp[i])
    			OpStatus = true;
    		}
    	}
    I think your problem might lie in the declaration:
    Code:
    bool OpStatus(false);
    Try doing:
    Code:
    bool opStatus;
    And then right before you use it, set it to whatever you want the initial value to be.

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #3
      And upon closer inspection:
      [code]
      for (i=0; OpStatus=false, (i<strlen(Valid Op)); i++)
      [/quote]

      You set OpStatus to false every time you go through the loop. Even if this is set to true, the next time to loop is done, it's set to false - wiping out true.
      Code:
          OpStatus = false;
          for (i=0; (i<strlen(ValidOp)); i++) {
      That will keep OpStatus from being reset every time.

      Comment

      • Randeh
        New Member
        • Feb 2007
        • 16

        #4
        Changed the bool declaration to just bool OpStatus; and altered the function as follows:

        Code:
         
        void IsValidOp(char TestOp, int i)
             {
             char ValidOp[] = "+-*/cCxX";
             OpStatus = false;
             for (i=0; (i<strlen(ValidOp)); i++)
                  {
                  if (TestOp==ValidOp[i])
        	OpStatus = true;
                  }
               }
        ... and I'm still constantly getting a false returned! What I wouldn't give for an obvious solution once and a while.

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          You are going about this the wrong way. Instead of having a void function that changes a global boolean value, why not write a bool returning function? If you find the operator inside the ValidOp array, return true - if, after searching, you haven't found it, return false.

          Comment

          • Randeh
            New Member
            • Feb 2007
            • 16

            #6
            Thanks, all makes much more sense than what I was trying!

            Comment

            Working...