On 10/18/2018 07:04 PM, Samir wrote:

>      myArray.length = noValues; // I get a run-time error if I comment
> this out

It's because the expression that reads the elements below is readf, which reads on top of an existing element.

>      while (i < noValues) {
>          write("enter value #", i+1, " ");
>          readf(" %s", &myArray[i]);

Aside: It may be possible to read like the following without taking the address, as readf has been improved to take references since that code was written:

         readf(" %s", myArray[i]);  // (not tested)

So, the answer to your question is, we need to set the length of the array so that there are elements to read on top of. If D had function templates like getResponse() that I use in the Templates chapter,


then the best thing to do would be to append the elements directly to the array:

    // No need to set .length beforehand
    myArray ~= getResponse!int(/* ... */);


Reply via email to