退出代码为0(0x0)

时间:2017-03-19 23:08:15

标签: c visual-studio visual-studio-2015 exit exit-code

好的,所以我第一次在Visual Studio 2015中从事一些学校工作,我的程序不会进入if语句,只是退出代码0,我尝试删除if语句,但它有效,但我需要拥有它。

    #include <stdio.h>

void main() {
    char c1, c2;
    int controlPoint;
    int counter, counterTwo;
    float intSingle;
    int isTrue = 0;
    float* x;
    float* y;
    float* fx;
    float sum = 0;
    float temp;
    int k;



    printf(" \n Please select an option");
    printf(" \n Press i to do an interlopation");
    printf(" \n Press q to quit");

    scanf("%c", &c1);


    if (c1 == 'q' || c1 == 'Q') {
        printf("Now Quiting");
        exit(0);
    }
    else if (c1 == 'i' || c1 == 'I') {
        printf(" \n Do a Lagrange Interlopation");
        printf(" \n How many control points are there?");

        scanf("%d", &controlPoint);
        x = malloc(controlPoint * sizeof(float));
        y = malloc(controlPoint * sizeof(float));

        for (counter = 0; counter < controlPoint; counter++) {
            printf("Please Enter the x coordinate for control point #%d: ", counter);
            scanf("%f", &x[counter]);
            printf("Please Enter the y coordinate for control point #%d: ", counter);
            scanf("%f", &y[counter]);
        }
    }
    else {
        printf("Invalid Option Quitting");

    }









    printf("\nPlease select an option");
    printf("\n Press s to do single interlopation");
    printf("\n Press r to interlope in increments over the entire range");
    printf("\n Press q to quit");

    scanf("%c", &c2);

    if (c2 == 's' || c2 == 'S') {
        printf(" \n Interlope Single");
        printf(" \n Please enter the value of x you wish to interlope for: ");
        scanf("%f", &intSingle);
        fx = malloc(controlPoint * sizeof(float));

        for (counter = 0; counter < controlPoint; counter++)
        {
            temp = 1;
            int k = counter;

            for (counterTwo = 0; counterTwo < controlPoint; counterTwo++)
            {
                if (k == counterTwo)
                {

                }
                else
                {
                    temp = temp * ((intSingle - x[counterTwo]) / (x[k] - x[counterTwo]));
                }
            }
            fx[counter] = y[counter] * temp;

        }

        for (counter = 0; counter < controlPoint; counter++)
        {
            sum = sum + fx[counter];
        }
        printf("\n Interpolated pair is (%f, %f) ", intSingle, sum);
    }

    else if (c2 == 'q' || c2 == 'Q'){
        printf("Now Quiting");
        exit(0);

    }
    int holder;

    scanf("%d", &holder);



}

问题是当用户输入第二个选项时,如果我按下代码0的s我就会关闭我也删除了if语句并且代码工作正常但我需要它们

1 个答案:

答案 0 :(得分:3)

首先,你的代码看起来不是很好,你没有释放动态分配,有些缺少\n等等。

对于你的问题, scanf %c只读一个字符,当您输入一个字母并按Enter键时,您只需输入i\n

第一个scanf读取了字母i,但\n保留在缓冲区中,直到下一个scanf读取它而不是s

您可以使用类似的内容清除stdin缓冲区:

while ((temp = getchar()) != '\n' && temp != EOF);

或者,如果您确定输入只有1个字符,则可以在%c之前添加空格以忽略\nscanf("%c",...scanf(" %c",...