I'm trying to use scanf_s()
to read in multiple values but every time I run the program, I get
Unhandled exception at 0x592AD6AC (msvcr120d.dll) in lab 2.exe: 0xC0000005: Access violation writing location 0x00000000.
in a popup window. How do I fix this?
float inTemp;
char inUnit;
char outUnit;
printf("Please enter the starting temperature with its units and the units\nyou would like to convert to (i.e. 74.5 F C): ");
scanf_s("%f %c %c", &inTemp, &inUnit, &outUnit); //takes in all user input (NOT WORKING)
If you really want to use (less portable)
scanf_s
, from the C11 standard (n1570), Annex K 3.5.3.2 p.4:You need to give the lengths of your
char *
arguments:Alternatively, just use
scanf
:And, as always, check the return values.
In general, as
scanf
is somtimes useful for little quick-and-dirty programmes, it’s of less use for productive software, because handling errors is hard. Usefgets
andsscanf
/strtod
/strtol
/… instead.You have not provided a length indicator. From MSDN:
Here is a link to that page.