I'm trying to create an ad hoc scanner that reads a calculator language.
When I execute the program, I get an error saying the following:
"Debug Assertion Failed!
Expression: string subscript out of range"
If I click the "Ignore" button a bunch of times, it will eventually produce the output file of my liking.
My sample input file has this written inside of it:
I've tried looking into this problem and believe it has something to do with my string size but I'm unsure. Could anyone tell me what might be the fix here?
It definitely has something to do with your string size. The fix: Pay attention to the size of the string.
The first thing you do is read in the token "read". Without checking the size of the string you read in, you check holder and holder which, in this case, happen to be present in the string, but since you didn't check whether the input operation was successful or the size of the data extracted, you have no way of knowing that.
Next, you check holder to see if it's a space. 4 is not a valid index for a string holding a string of size 4. Valid indices would be 0-3. There are similar problems strewn throughout your code.
It would be better to have an IsOperator function (that returns a bool) which has a switch inside it. This is much more scalable than a huge if statement. And you have those if's in several places.
What made me notice this was the code being 2 screens wide . There is a sort of a rule that code should not be more than 80 chars per line. This makes it easier to read - I hate scrolling sideways all the time, and it is easier to print if that is needed.
The other contributor to this is the indenting. I see a lot of code on this site with indenting at 8 chars - which is too much. I am guessing this is because OP's have their tabs set to being actual tabs which are 8 spaces, whereas tabs being 4 spaces is much better.
is invalid because the size of the string is unknown and it can be less than [x++1] and even [x] (for example when an empty string was entered). You should check that the subscript is always less than the size of the string.