Access Violation
David Medlock
noone at nowhere.com
Thu May 11 10:44:00 PDT 2006
Robert Jones wrote:
> Why am i getting an access violation:
>
> C:\Documents and Settings\jareth\Desktop>dmd -v -w ly2au.d
> parse ly2au
> semantic ly2au
> semantic2 ly2au
> semantic3 ly2au
> code ly2au
> generating code for function 'main'
> c:\dmd\bin\..\..\dm\bin\link.exe ly2au,,,user32+kernel32/noi;
>
> C:\Documents and Settings\jareth\Desktop>ly2au
> Enter number of light years: 5
> Error: Access Violation
>
> Attached is the source code.
>
>
> ------------------------------------------------------------------------
>
> import std.c.stdio;
> import std.stdio;
>
> // number of kilometers in a astronomical unit
> const real au = 148800000.0;
> // number of kilometers in a light year based on light second being exactly 300,000km
> const real ly = 9467280000000.0;
> real n;
> real x;
> real y;
>
> int main()
> in
> {
> assert(au is 148800000.0); // ensure that au is exactly 1,448,800,000km
> assert(ly is 9467280000000.0); // ensure that ly is exactly 9,467,280,000,000km
> }
> body
> {
> writef("Enter number of light years: ");
> scanf("%f", x);
>
> // convert light years to kilometers
> y = x * ly;
>
> // convert kilometers to astonomical units
> n = y / au;
>
> writefln("\nThe number of astronomical units is %f", n );
> return 0;
> }
2 Problems I can see:
1) scanf requires the address of a float, so use &x
2) initialize those reals to some value, or you will get nan
-DavidM
More information about the Digitalmars-d-learn
mailing list