#strtok returns null even though there still are substrings not containing the separator

52 messages · Page 1 of 1 (latest)

junior bramble
#

I'm trying to write a program (a minicompiler) in which I can have multiple commands such as "write", "repeat", "stop" (which signifies the end of a repeat command) and some expressions (ex: a = n * b +/- ...).
For some reason, when using strtok to take out each element out of the expression line (where the line is c = a + b), I get null when trying to get the next element (which should be '='). Anyone know why this happens?

still oxideBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

mighty pivot
#

can we see the code?

junior bramble
#
                word2 = strtok(NULL, " "); /// =
                word2 = strtok(NULL, " "); /// expression```
junior bramble
#

this is just the repeat function, by the way, but its the same code as in the main function, since I need it to repeat the lines [from, to]

chilly sparrow
#

What's the input?

junior bramble
#
INCEPE
   a = 1
   b = a
   SCRIE a
   SCRIE b
   REPETA 10
      c = a + b
      a = b
      b = c
      SCRIE c
   STOP
STOP
#

INCEPE means the start of the program

#

SCRIE <var> means write var

#

REPETA <n> is repeat n times the lines between repeat and stop

#

but when doing the expression c = a + b, strtok only gives me c, then returns null

chilly sparrow
#

int var1 = word2[0] - 'a';
What is the value of word2 when the program is at this line?

junior bramble
#

first, word2 = strtok(line[i], " ")

#

line[i] is c = a + b

chilly sparrow
#

So you did look into the value of word2 right?

junior bramble
chilly sparrow
#

Try checking the string word2 + 2 at that moment.

junior bramble
#

but then, when doing word2 = strtok(NULL, " "), I expect it to be =

#

it's "="

#

but shouldn't "word2 = strtok(NULL, " ")" work?

chilly sparrow
#

I'm seeing something abnormal there. The rest of the line should be = a + b.

#

Just realized this is C++ also, you should not use NULL but rather nullptr.

#

Check the contents of line[i] at the beginning of the loop iteration.

junior bramble
chilly sparrow
#

That doesn't look like the beginning of the loop iteration.

junior bramble
junior bramble
chilly sparrow
#

It's not the issue indeed, just a reminder about more proper C++ code.

chilly sparrow
#

I.e. on the line of word2 = strtok(line[i], " ");.

junior bramble
#

it's c = a + b

chilly sparrow
#

Those are not spaces.

junior bramble
chilly sparrow
#

Looks like the string has already been trampled on using strtok before that.

junior bramble
#

in the main loop, I get the line with cin.getline

junior bramble
chilly sparrow
junior bramble
#

so that I don't lose the string when using strtok

chilly sparrow
#

Yes.

junior bramble
#

Oh, thank you!

chilly sparrow
#

But you should probably just use std::string and its functions.

#

Are you made use C strings?

junior bramble
chilly sparrow
#

Then probably a good time to learn about it.

junior bramble
#

!solved