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?
#strtok returns null even though there still are substrings not containing the separator
52 messages · Page 1 of 1 (latest)
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.
can we see the code?
the issue is here, word2 returns null instead of '=' after calling strtok
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]
What's the input?
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
int var1 = word2[0] - 'a';
What is the value ofword2when the program is at this line?
c
first, word2 = strtok(line[i], " ")
line[i] is c = a + b
So you did look into the value of word2 right?
yes, it is c at first
Try checking the string word2 + 2 at that moment.
but then, when doing word2 = strtok(NULL, " "), I expect it to be =
it's "="
but shouldn't "word2 = strtok(NULL, " ")" work?
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.
word2 only gets the substring up until space, but line[i] (the whole string) is "c = a + b"
That doesn't look like the beginning of the loop iteration.
oh, I'll fix that, but I don't think thats the issue though
what do you mean
It's not the issue indeed, just a reminder about more proper C++ code.
Check line[i] when the program is at the beginning of the loop iteration.
I.e. on the line of word2 = strtok(line[i], " ");.
it's c = a + b
Those are not spaces.
Oh
Looks like the string has already been trampled on using strtok before that.
in the main loop, I get the line with cin.getline
You can't use strtok on the same string multiple times?
this is the main loop
Indeed, as it modifies the string.
oh, so should I just copy the string in another array with strcmp and then use strtok on that array?
so that I don't lose the string when using strtok
Yes.
Oh, thank you!
But you should probably just use std::string and its functions.
Are you made use C strings?
no, but I'm not really familiar with using std::string
Then probably a good time to learn about it.
Yes, anyway, thank you for your help!
!solved