Console exits when .at(i+1) is used

Hi all,

I've got a small problem. Im working on a lexical analyser for school bu when I add this in my code char nextChar = input.at(i+1);

And the console has to output that char. It just exits doing nothing.

This is the method in which the problem occurs:
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
//Scans the string given to it for illegaltokens. It uses the booleans to set the correct input and output types.
void Scanner::ScanForIllegalTokens(string input,bool inputConsole,bool outputConsole)
{
     vector<string> unary_op = Tokenlister->unary_op;
     vector<string> binary_op = Tokenlister->binary_op;
     
     if(inputConsole && outputConsole)
     {
          int lineNumber = 1;
     
          for(int i = 0; i<input.size();i++)
          {
              char currentChar = input.at(i);
              char nextChar = input.at(i+1);// This is the problem char.
              if(currentChar == '\n')
              {
                lineNumber++;
              }
              for(int j = 0; j<binary_op.size();j++)
              {
                 string currentString;
                 currentString = currentChar;
                if(currentString == binary_op.at(j))
                {
                  cout<<"CurrentChar = "<<currentChar<<endl << "nextChar = "<<endl;
                  cout<<"Token found"<<endl;
                }
              } 
          }
          cout << "this input has: "<<lineNumber<<" linenumbers"<<endl;
          
          while(input.find(" ")!= string::npos)
          {
           input.replace(input.find(" "),1,"");
          }
          cout << input<<endl;
     }
}


It completely works accept when I add the char I mentioned earlier. Don't worry about anything else in the method that all works fine.

Thanks in advance, Rope.
closed account (z05DSL3A)
Check that i+1 is not larger than input.size() befor using it?
Ah off course. Thanks Grey Wolf you've been a great help so far!
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
closed account (z05DSL3A)
"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
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
closed account (z05DSL3A)
I was editing my post while you wrote yours. See above.
Tried your code but it didn't work.
But I fixed it. Inserted an '\0' char at input.end(). Works perfectly.

Thanks again!
closed account (z05DSL3A)
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++)
Topic archived. No new replies allowed.