You do not check that the end of the string was encountered
for(j = 0; line[i] != ':'; ) // what about line[i] == '\0' ? |
Right, I forgot to add that part, thank you.
Further you may not use speaker[j++] because the memory for the string speaker was not allocated. You should use lines[curLine].speaker += line[i++]; |
Awesome, you got it fixed in a blink
Now the two strings have the same address until the first call to operator+=, which makes it change
Thank you very much
---------------------------------------------------------------
Neither of those loops should be looping on the in.good() condition. |
Why? I can see that I save a call, but is there a specific reason for that?
Some things you might consider. You allocate memory for lines here, but don't make any attempt to see if any memory was allocated previously (and if it was, you have an obvious memory leak here.) Using a string as opposed to an array of char for line. |
Well, actually when I encounter errors, like lines ending before a closing : or " is found, I delete[] the memory. It's not possible that other memory is allocated previously because this function is called only once
1 2 3 4 5 6 7 8 9 10 11
|
case '"':
++i;
while(line[i] !='"')
{
if(line[i] == '\0')
{
std::cout << "Error while scanning file: unexpected EOL at line " << curLine+1 << ", position " << i+1 << std::endl;
delete[] lines;
return;
}
script[curLine].text += line[i++];
|
I also changed the for to a while since j is not needed anymore