【问题标题】:Exit with code 0 (0x0)使用代码 0 (0x0) 退出
【发布时间】:2017-03-19 23:08:15
【问题描述】:

好的,所以我第一次在 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 语句,代码工作正常,但我需要它们

【问题讨论】:

  • 您是否考虑过在调试器中单步执行您的代码?不确定您指的是哪个 if 语句或您的输入是什么。也许你可以澄清这些事情。
  • 当代码到达这里时 if (c2 == 's' || c2 == 'S') {
  • 在调用任何scanf() 系列函数时,始终检查返回值(而不是参数值)以确保操作成功。
  • 在调用任何堆分配函数时:(malloc, calloc, realloc),始终检查 (!=NULL) 返回值以确保操作成功。

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


【解决方案1】:

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

对于您的问题, scanf %c 只读取一个字符,当您输入一个字母并按 Enter 时,您实际上输入了 i\n

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

您可以使用类似的方法来清除stdin 缓冲区:

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

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

【讨论】:

  • 在格式字符串中的%c之前放置一个空格也会跳过空格。
  • 同意,将添加
猜你喜欢
  • 2018-07-25
  • 1970-01-01
  • 2018-05-28
  • 1970-01-01
  • 1970-01-01
  • 2014-03-26
  • 2012-09-06
  • 1970-01-01
  • 2014-01-05
相关资源
最近更新 更多