Microsoft Visual Studio 2013 C程序异常

时间:2015-01-30 22:14:32

标签: c visual-studio-2013

我正在使用Microsoft Visual Stusio 2013 Community Edition Update 4以C编程语言(而不是C ++)编写程序。每当我尝试构建并运行我的程序时,在执行过程中它会告诉我" program.exe中的0x52873FD4(msvcr120d.dll)处的未处理异常:0xC0000005:访问冲突写入位置0x00C40000。"当我将.c文件的内容复制并粘贴到另一个IDE(代码块)时,它编译并运行没有问题。 (虽然我必须将代码中的所有scanf_s语句更改为scanf

我程序的.c文件中的源代码

#include <math.h>
#include <stdio.h>
#include <string.h>

#define PI 3.14159265358979323846;

double add();
double cos_d_r();
double div();
double mult();
void list();

int main()
{
    //Declaration of variables
    char oper[20];
    double ans;
    int loop = NULL;

    //While loop allows for multiple executions without restarting program
    while (!loop)
    {
        printf("\n\nEnter a mathematical operation.\n\nFor a list of options, enter 'list'\n\n");
        scanf_s(" %s", oper);

        //If statement to determine which operation to do

        //Addition
        if (strcmp(oper, "add") == 0 || strcmp(oper, "addition") == 0 || oper[0] == '+')
        {
            ans = add();
            printf("The sum is %.2f\n", ans);
        }

        //Cosine
        else if (strcmp(oper, "cos") == 0 || strcmp(oper, "cosine") == 0)
        {
            ans = cos_d_r();
            printf("The cosine of the angle is %lf\n", ans);
        }

        //Division
        else if (strcmp(oper, "divide") == 0 || strcmp(oper, "division") == 0 || oper[0] == '/')
        {
            ans = div();
            printf("The quotient is %.2f\n", ans);
        }

        //List of possible operations
        else if (strcmp(oper, "list") == 0)
        {
            list();
        }

        //Multiplication
        else if (strcmp(oper, "multiply") == 0 || strcmp(oper, "multiplication" == 0) || oper[0] == '*')
        {
            ans = mult();
            printf("The product is %.2f", ans);
        }
    }

    return 0;
}

//Declaration of functions

//Addition
double add()
{
    double ans;
    double num_1;
    double num_2;

    puts("Enter the first number");
    scanf_s(" %lf", &num_1);
    puts("Enter the second number");
    scanf_s(" %lf", &num_2);
    ans = num_1 + num_2;
    return ans;
}

//Cosine
/*Uses cos() function from math.h
this function adds option for degrees or radians*/
double cos_d_r()
{
    char deg_rad;
    double angle;
    double ans;
    int loop = NULL;

    while (!loop)
    {
        puts("Degrees or radians? Enter 'd' or 'r'");
        scanf_s(" %c", &deg_rad);

        //Degrees
        if (deg_rad == 'd')
        {
            puts("Enter an angle in degrees");
            scanf_s(" %lf", &angle);
            angle = angle / 180 * PI;
            ans = cos(angle);
            loop = 1;
            return ans;
        }

        //Radians
        else if (deg_rad == 'r')
        {
            puts("Enter an angle in radians");
            scanf_s(" %lf", &angle);
            ans = cos(angle);
            loop = 1;
            return ans;
        }

        //Else statement repeats loop if user enters text other than 'd' or 'r'
        else
        {
            puts("ERROR. Enter either 'd' or 'r'");

        }
    }
}

//Division
double div()
{
    double ans;
    double num_1;
    double num_2;

    puts("Enter the dividend");
    scanf_s(" %lf", &num_1);
    puts("Enter the divisor");
    scanf_s(" %lf", &num_2);
    ans = num_1 / num_2;
    return ans;
}

//Multiplication
double mult()
{
    double ans;
    double num_1;
    double num_2;

    puts("Enter the first number");
    scanf_s(" %lf", &num_1);
    puts("Enter the second number");
    scanf_s(" %lf", &num_2);
    ans = num_1 * num_2;
    return ans;
}

//List of possible operations
void list()
{
    printf("The possible operations are:\n\n");
    printf("Operation\tDescription\tCommand");
    printf("Addition\tAdds two numbers together\tAdd, Addition, +\n");
    printf("Cosine\tFinds the cosine of the angle entered\tCos, Cosine");
    printf("Division\tDivides the first number by the second\tDivide, Division, /");
    printf("Multiplication\tMultiplies the two numbers\tMultiply, Multiplication, *");
}

1 个答案:

答案 0 :(得分:0)

您应该首先阅读您使用的功能说明!请查看MSDN上的scanf_s功能说明。你第一次调用这个函数时程序崩溃了

scanf_s(" %s", oper);

使用它的正确方法:

scanf_s("%s", oper, _countof(oper)); // or sizeof(oper), but only with char array

此外,您应该更正代码中scanf_s的其他来电。 (或scanf之前使用#define _CRT_SECURE_NO_WARNINGS #include <stdio.h>来禁用{{1}}来电的错误