By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,226 Members | 1,221 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,226 IT Pros & Developers. It's quick & easy.

Simple Bool Function Malfunction

P: 16
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:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. bool OpStatus(false);
  6. char TestOp, UseOp;
  7. int i;
  8. void IsValidOp(char TestOp, int i);
  9.  
  10. void main()
  11.      {    
  12.      while (1) 
  13.           {                                           
  14.           cout << "Enter desired operation: ";
  15.           cin >> TestOp;
  16.  
  17.           IsValidOp(OpStatus,i);
  18.     if (OpStatus==false)
  19.     {
  20.                 cout << "Invalid operator. Try again." << endl;
  21.     continue;
  22.     } else TestOp = UseOp;
  23.           }
  24.      }
  25.  
  26.  
  27. void IsValidOp(char TestOp, int i)
  28.     {
  29.     char ValidOp[] = "+-*/cCxX";
  30.     for (i=0; OpStatus=false, (i<strlen(ValidOp)); i++)
  31.         {
  32.         if (TestOp==ValidOp[i])
  33.             OpStatus = true;
  34.         }
  35.     }
  36.  
Mar 11 '07 #1
Share this Question
Share on Google+
5 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
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:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. bool OpStatus(false);
  6. char TestOp, UseOp;
  7. int i;
  8. void IsValidOp(char TestOp, int i);
  9.  
  10. void main()
  11.      {    
  12.      while (1) 
  13.           {                                           
  14.           cout << "Enter desired operation: ";
  15.           cin >> TestOp;
  16.  
  17.           IsValidOp(OpStatus,i);
  18.     if (OpStatus==false)
  19.     {
  20.                 cout << "Invalid operator. Try again." << endl;
  21.     continue;
  22.     } else TestOp = UseOp;
  23.           }
  24.      }
  25.  
  26.  
  27. void IsValidOp(char TestOp, int i)
  28.     {
  29.     char ValidOp[] = "+-*/cCxX";
  30.     for (i=0; OpStatus=false, (i<strlen(ValidOp)); i++)
  31.         {
  32.         if (TestOp==ValidOp[i])
  33.             OpStatus = true;
  34.         }
  35.     }
  36.  
I think your problem might lie in the declaration:
Expand|Select|Wrap|Line Numbers
  1. bool OpStatus(false);
  2.  
Try doing:
Expand|Select|Wrap|Line Numbers
  1. bool opStatus;
  2.  
And then right before you use it, set it to whatever you want the initial value to be.
Mar 11 '07 #2

sicarie
Expert Mod 2.5K+
P: 4,677
And upon closer inspection:
[code]
for (i=0; OpStatus=false, (i<strlen(ValidOp)); 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.
Expand|Select|Wrap|Line Numbers
  1.     OpStatus = false;
  2.     for (i=0; (i<strlen(ValidOp)); i++) {
  3.  
That will keep OpStatus from being reset every time.
Mar 11 '07 #3

P: 16
Changed the bool declaration to just bool OpStatus; and altered the function as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. void IsValidOp(char TestOp, int i)
  3.      {
  4.      char ValidOp[] = "+-*/cCxX";
  5.      OpStatus = false;
  6.      for (i=0; (i<strlen(ValidOp)); i++)
  7.           {
  8.           if (TestOp==ValidOp[i])
  9.     OpStatus = true;
  10.           }
  11.        }
  12.  
... and I'm still constantly getting a false returned! What I wouldn't give for an obvious solution once and a while.
Mar 11 '07 #4

Ganon11
Expert 2.5K+
P: 3,652
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.
Mar 11 '07 #5

P: 16
Thanks, all makes much more sense than what I was trying!
Mar 11 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.