Lord Wolfram Lord Wolfram - 9 months ago 71
C Question

Unhandled exception at 0x0f65f508 (msvcr100d.dll)

So I've been working on a project which is simply a command line program.
The problem I'm running into is when I wanted to create a calculator. The code looks like this:

int calculator()
{
char oper[1];
double x, y;
x = 1;
y = 1;
printf("Starting calculator...\n\n");
printf("Input an operator you want to execute: ");
gets(oper);

if (strncmp(oper, "+", 1) == 0)
{
printf("\ncalculator works like this x + y =\n");
printf("input x value:\n");
scanf("%lf",x);
printf("input y value:\n");
scanf("%lf",y);
printf("%.1lf + %.1lf = %.1lf\n",x, y, x+y);

}
else if (strncmp(oper, "-", 1) == 0)
{
printf("\ncalculator works like this x - y =\n");
printf("input x value:\n");
scanf("%lf",x);
printf("input y value:\n");
scanf("%lf",y);
printf("%.1lf + %.1lf = %.1lf\n",x, y, x+y);


}
else if (strncmp(oper, "*", 1) == 0)
{
printf("\ncalculator works like this x * y =\n");
printf("input x value:\n");
scanf("%lf",x);
printf("input y value:\n");
scanf("%lf",y);
printf("%.1lf + %.1lf = %.1lf\n",x, y, x+y);

}
else if (strncmp(oper, "/", 1) == 0)
{
printf("\ncalculator works like this x / y =\n");
printf("input x value:\n");
scanf("%lf",x);
printf("input y value:\n");
scanf("%lf",y);
printf("%.1lf + %.1lf = %.1lf\n",x, y, x+y);
}
else
{
printf("Incorrect operator, please try again!\n");
}
}


According to few examples and tutorials it should work fine, and it does.
But the problems start after running the code. It works fine until the x value is inserted. Right after it the error shows up:

Unhandled exception at 0x0f74f508 (msvcr100d.dll) in program.exe: 0xC0000005: Access violation writing location 0x00000000.

I've seen similar questions like this one but none of them was really giving the fix. Thank you for your help!

Answer Source

try replace scanf("%lf",x); to scanf("%lf",&x);