1

I have a function that I'm using to try to check whether a string is in the correct format. I'm trying to do that by looking at each character and determining if it is of the correct type. However no matter what I try I get an error that I cannot figure out. The code is below:

bool valid(string checkcode) { if(checkcode.length()!=6) return false; else if(isalpha(checkcode.at(0)))&(isalpha(checkcode.at(1)))&(isdigit(checkcode.at(2)))&(isdigit(checkcode.at(3)))&(isalpha(checkcode.at(4)))&(isalpha(checkcode.at(5))) return true; else return false; } 

The error I'm getting is at the first '&' and it says "Error: expression must be an Ivalue or function designator" I'm really stuck here, any help is appreciated.

2
  • 1
    dont you mean && not &? Commented Apr 18, 2013 at 17:09
  • Please get in the habit of copying and pasting compiler messages instead of re-typing them yourself. That way, you'll correctly get lvalue instead of Ivalue. That's lowercase el, not capital i. Commented Apr 18, 2013 at 17:40

3 Answers 3

3
 isalpha(checkcode.at(0)))&(isalpha(checkcode.at(1))) //bit and 

should be

isalpha(checkcode.at(0)))&&(isalpha(checkcode.at(1))) //^^logical and 

You need to use logical and in this case.

You also need to make sure that your parentheses match.

//better to format multiple conditions and make sure () match if( (isalpha(checkcode.at(0))) &&(isalpha(checkcode.at(1))) &&(isdigit(checkcode.at(2))) &&(isdigit(checkcode.at(3))) &&(isalpha(checkcode.at(4))) &&(isalpha(checkcode.at(5))) ) return true; 
Sign up to request clarification or add additional context in comments.

2 Comments

When I do that I get "Error: expected an expression"
@Danger_Fox see updated post because your () does not match well
3
bool valid(string checkcode) { return checkcode.length() == 6 && (isalpha(checkcode.at(0))) && (isalpha(checkcode.at(1))) && (isdigit(checkcode.at(2))) && (isdigit(checkcode.at(3))) && (isalpha(checkcode.at(4))) && (isalpha(checkcode.at(5))); } 

Comments

1

The reason for your error is that your parentheses are in the wrong places. The & that the compiler complains about is outside the conditional expression of the if statement.

The compiler sees this as the condition to be tested:

if(isalpha(checkcode.at(0))) 

The remaining part is considered the statement to execute when the condition is true:

&(isalpha(checkcode.at(1)))... 

Thus, the compiler is correct. When it sees a unary & operator, it expects the operand to be something that it can take the address of, such as an lvalue or a function.

Careful reading of error messages and code will help you find this kind of error next time. (That is, when the compiler complains about a missing "lvalue or function designator," ask yourself what would make it expect such a thing in the first place. It wants those when it's taking the address of something, so consider why it thinks it should be taking the address of anything. It does that with a unary & operator, but you intended to have a binary operator, so look closely at the code to determine why it's not interpreted as a binary operator. You know C++ syntax, so you know that if statements need to be entirely surrounded in parentheses, and so you know that only the first expression is part of the condition. That's not what you intended, so you'll realize that the closing parenthesis is in the wrong place.)

As it is, the single-ampersand & operator isn't what you should use for Boolean expressions. You should use && instead. That difference would not lead to the error you saw, and it would have no discernible effect on the run-time behavior of your code, either.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.