Why I'm getting this "Range Violation" error?
Mike Parker
aldacron at gmail.com
Fri Jul 9 07:38:50 UTC 2021
On Friday, 9 July 2021 at 07:21:06 UTC, rempas wrote:
>
> When I execute it, I'm getting a range violation error. If I
> try to set "len" to be the length of the "prompt" minus 1, then
> it will work and it will print the "prompt" until the
> questionmark. So I cannot find where the error is...
Because you're indexing the prompt with i before you ensure that
i is valid. Swap the operands in your if condition:
```
while (i < len && prompt[i] != '{')
```
Or better yet, use foreach, which exists to avoid this sort of
mistake:
```
foreach(c; prompt) {
if(c != '{') printf("%c", c);
}
```
More information about the Digitalmars-d-learn
mailing list