Microsoft Visual Studio 2013 C 程序异常

Microsoft Visual Studio 2013 C Program Exception

我正在使用 Microsoft Visual Stusio 2013 Community Edition Update 4 使用 C 编程语言(而非 C++)编写程序。每当我尝试构建和 运行 我的程序时,在执行过程中它会告诉我 "Unhandled exception at 0x52873FD4 (msvcr120d.dll) in program.exe: 0xC0000005: Access violation writing location 0x00C40000." 当我将 .c 文件的内容复制并粘贴到另一个 IDE (代码块)时,它会编译运行s 没有问题。 (虽然我确实必须将代码中的所有 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, *");
}

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

scanf_s(" %s", oper);

正确的使用方法:

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

您还应该更正代码中对 scanf_s 的其他调用。 (或者禁用 scanf#include <stdio.h> 之前使用 #define _CRT_SECURE_NO_WARNINGS 调用的错误)