Why I'm getting this "Range Violation" error?

rempas rempas at tutanota.com
Fri Jul 9 08:02:03 UTC 2021


On Friday, 9 July 2021 at 07:38:50 UTC, Mike Parker wrote:
> 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);
> }
> ```

OMG!!! Yes! I feel bad for not catching it now... Meanwhile I 
realized "while" will not work in my example anyway. Thanks for 
the man! Have a nice day!


More information about the Digitalmars-d-learn mailing list