What's wrong with this line?

Mar 8, 2012 at 2:51pm
There's this one line of code that's giving me trouble and I can't seem to figure out what I'm doing wrong. Any suggestions?

 
if(tok2.value.at(0) == '(' )


Basically, I just want to check if a string has parentheses (tok2 is a struct, and value is its string component).
Mar 8, 2012 at 2:57pm
Hi
what does tok2.value.at(0) return? could you check it out ?
Last edited on Mar 8, 2012 at 2:57pm
Mar 8, 2012 at 3:05pm
wouldn't it be better to use the [] operator instead?

 
if( tok2.value[0] == '(' )


Also, what is the error you're getting? All you said, really, is that it's not working.
Last edited on Mar 8, 2012 at 3:06pm
Mar 8, 2012 at 3:10pm
Perhaps value is an empty string? You can try to catch an exception and see what is wrong.
Mar 8, 2012 at 4:18pm
You could do something like this this:
1
2
3
4
5
6
7
#include <algorithm>
//...

int match[] = {'(',')'};
std::string::iterator it = std::find_first_of(tok2.value.begin(),tok2.value.end(),match, match +2);
	if(it != tok2.value.end())
		std::cout << "FOUND" << std::endl;  //You know you have found it. 
Topic archived. No new replies allowed.