I'm getting NAN out of nowhere

Binarydepth via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Apr 10 11:13:41 PDT 2017


On Monday, 13 July 2015 at 14:29:57 UTC, Steven Schveighoffer 
wrote:
> On 7/11/15 12:57 PM, flamencofantasy wrote:
>> On Thursday, 9 July 2015 at 15:14:43 UTC, Binarydepth wrote:
>>> This is my code :
>>> import std.stdio : writeln, readf;
>>> void main()    {
>>>     int[3] nums;
>>>     float prom;
>>>     foreach(nem; 0..2)    {
>>>         writeln("input a number : ");
>>>         readf(" %d", &nums[nem]);
>>>         prom+=nums[nem];
>>>     }
>>>     writeln(prom/3.0);
>>> }
>>>
>>> I get prompted two times for a number and I then get NAN out 
>>> of nowhere.
>>
>> foreach(nem; 0..3)
>
> that is a good catch, if the purpose is to fill in all 3 nums 
> elements.
>
> Note, a future-proof version would say:
>
> foreach(nem; 0..nums.length)
>
> A more d-idiomatic way is to say:
>
> foreach(ref nem; nums)
>
> And then use nem anywhere you see nums[nem]
>
> -Steve

You mean with readf ?


More information about the Digitalmars-d-learn mailing list