Mar 25, 2008 at 10:47am UTC
Check that i+1 is not larger than input.size() befor using it?
Mar 25, 2008 at 11:12am UTC
Ah off course. Thanks Grey Wolf you've been a great help so far!
Mar 25, 2008 at 12:56pm UTC
I'm having anothe problem with the same code only a bit changed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
void Scanner::ScanForIllegalTokens(string input,bool inputConsole,bool outputConsole)
{
vector<string> unary_op = Tokenlister->unary_op;
vector<string> binary_op = Tokenlister->binary_op;
vector<char > invalid_tokens = Tokenlister->invalid_tokens;
if (inputConsole && outputConsole)
{
int lineNumber = 1;
for (int i = 0; i<input.size();i++)
{
char currentChar = input.at(i);
if (currentChar != '\0' )
{
char nextChar = input.at(i+1); //problem char
if (currentChar == '\n' )
{
lineNumber++;
}
for (int j = 0; j<binary_op.size();j++)
{
string currentString;
currentString = currentChar;
if (currentChar == invalid_tokens.at(j))
{
cout<< "Invalid token: " <<currentChar<<" at line: " <<lineNumber<<endl;
}
}
}
}
while (input.find(" " )!= string::npos)
{
input.replace(input.find(" " ),1,"" );
}
cout << input<<endl;
}
I thought I'd use " != '\0' " instead of " < input.size() ". Since a string always ends with the \0 token. If I'm correct.
I did this because I also need the last character from the string. Which is not accessed in the code because I use if(i+1 < input.size())
So if this would be the string "test test test {}" '}' would be the last character followed by the '\0' char.
How ever the console exits again when encountering the char nextChar = input.at(i+1).
hope this info is clear enough.
Thanks in advance, Rope.
Edit: To clear things up a bit. I want to get the last char from the string AND be able to use nextChar = input.at(i+1).
Last edited on Mar 25, 2008 at 12:58pm UTC
Mar 25, 2008 at 1:19pm UTC
"I thought I'd use " != '\0' " instead of " < input.size() ". Since a string always ends with the \0 token. If I'm correct."
std::string is not null terminated, so you are is still going out off bounds with input.at(i+1).
This may work for you:
1 2 3 4 5 6 7 8
for (int i = 0; i < input.size();i++)
{
char currentChar = input.at(i);
char nextChar = '\0' ;
if ( i+1 != input.size())
nextChar = input.at(i+1);
...
Last edited on Mar 25, 2008 at 1:37pm UTC
Mar 25, 2008 at 1:37pm UTC
Then how do I insure I get the last char in the string and make sure that nextChar is not more then input.size()+1?
Since nextChar will always be input.size()+1 if currentChar is the last char in the string.
What if I were to append an '\0' char at the end of the string and then check if the current char doesn't equal '\0'? That should work right?
Last edited on Mar 25, 2008 at 1:41pm UTC
Mar 25, 2008 at 1:40pm UTC
I was editing my post while you wrote yours. See above.
Mar 25, 2008 at 1:50pm UTC
Tried your code but it didn't work.
But I fixed it. Inserted an '\0' char at input.end(). Works perfectly.
Thanks again!
Mar 25, 2008 at 2:21pm UTC
I’m glad you got it working.
I doubt if it will cause you a problem in your current code but it is worth noting that the return of string.size() is of type size_t (size_t is an unsigned integral type). So your for loop should use a size_t or unsigned int as its expression type.
for (size_t i = 0; i < input.size();i++)