C:只有if语句中的第一个条件有效

时间:2015-12-16 18:16:08

标签: c if-statement

我目前正在PuTTY编写代码,似乎在我的if语句中遇到技术问题。我的程序目前尚未完成,但我想在完成代码之前修复此错误。该程序将编译好并打印;然而,只做第一种分化工作并试图进行整合或其他类型的差异化退出该计划:

int main()
{
    int probtype, diftype, intype,  vartype, vartest;
    probtype = 0;
    diftype = 0;
    intype = 0;
    vartype = 0;
    vartest = 0;
    char variable, variable2;
    float  fpower, fconstant, fpower2, fconstant2, fdivisor;
    int ipower, iconstant, ipower2, iconstant2, idivisor;
    printf("Alright, starting off would you like to do differentiation or integration? Type '1' for differentiation or '2' for integration. ");
    scanf("%d", &probtype);
    if (probtype == 1)
    {
        printf("Okay, what kind of differentiation problem are you interested in? Type 1 for 'Power Rule with a Constant', Type 2 for 'Product Rule', Type 3 for 'Quotient Rule, Type 4 for 'Chain Rule', Type 5 for 'Trigonometric Problems', Type 6 for 'Exponential Problems' or Type 7 for 'Natural Log Problems. ");
        scanf("%d", &diftype);
        if (diftype == 1)
        {
            printf("Before we begin. Type 1, if the constant and power are both integers, type 2 if the constant is a decimal and the power is an integer, and type 3 for all other cases. " );
            scanf("%d", &vartype);
            if (vartype == 1)
            {
                printf("First off enter the function's constant. If it has no visible constant enter 1. ");
                scanf("%d", &iconstant);
                printf("Alright, next enter the variable you are using. ");
                scanf("%s", &variable);
                printf("Finally, enter the power of the variable. ");
                scanf("%d", &ipower);
                iconstant = iconstant * ipower;
                ipower = ipower - 1;
                if (ipower == 0)
                {
                    printf("The derivative of the function would be: %d \n", iconstant);
                }
                else
                {
                    printf("The derivative of the function would be: %d%s^%d \n", iconstant, &variable, ipower);
                }
            }
            if (vartype == 2)
            {
                printf("First off enter the function's constant. Enter it in DECIMAL form! ");
                scanf("%f", &fconstant);
                printf("Next, enter the variable you are using. ");
                scanf("%s", &variable);
                printf("Finally, enter the power of the variable. ");
                scanf("%d", &ipower);
                fconstant = fconstant * ipower;
                ipower = ipower - 1;
                if (ipower == 0)
                {
                    printf("The derivative of the function would be %f \n", fconstant);
                }
                else
                {
                    printf("The derivative of the function would be %f%s^%d \n", fconstant, &variable, ipower);
                }
            }
            if (vartype == 3)
            {
                printf("First off, enter the function's constant. Enter it in DECIMAL form! ");
                scanf("%f", &fconstant);
                printf("Okay, now enter which variable you are using. ");
                scanf("%s", &variable);
                printf("Finally, enter the power of the variable. Enter this in DECIMAL form also! ");
                scanf("%f", &fpower);
                fconstant = fconstant * fpower;
                fpower = fpower - 1;
                printf("The derivative of the function would be: %f%s^%f \n", fconstant, &variable, fpower);
            }
            else if (diftype == 2)
            {
                printf("Enter the first function's constant. If it doesn't have one, type a 1 ");
            }
            else if (diftype == 3)
            {
                printf("Quotient Rule");
            }
            else if (diftype == 4)
            {
                printf("Chain Rule");
            }
            else if (diftype == 5)
            {
                printf("Trigoometric Problems");
            }
            else if (diftype == 6)
            {
                printf("Exponential Problems");
            }
            else if (diftype == 7)
            {
                printf("Natural Log Problems");
            }
        }
        else if (probtype == 2)
        {
            printf("Okay, now what kind of integration problem are you interested in? Type 1 for 'Indefinite Integrals', Type 2 for 'Definite Integrals', Type 3 for 'Substitution', Type 4 for 'Trignometric Integrals', Type 5 for 'Integrations by Part', Type 6 for 'Exponential Problems', Type 7 for 'Natural Log Problems. ");
            scanf("%d", &intype);
            if (intype == 1)
            {
                printf("Before we get started: type 1 if both the constant and power are integers, type 2 if the constant is an integer and the power is a decimal, type 3 if the constant is a decimal and the power is an integer, and type 4 if both are decimals." );
                scanf("%d", &vartype);
                if (vartype == 1)
                {
                    printf("Alright first off, enter the constant. If there is no constant, type a 1. " );
                    scanf("%d", &iconstant);
                    printf("Is there a variable in this equation? Type 1 if there is, Type 2 if there isn't. ");
                    if (vartest == 2)
                    {
                        printf("The indefinite integral of the function is: %dx + C", iconstant);
                    }
                    else
                    {
                        printf("Next, enter the variable that will be used for the problem. ");
                        scanf("%s", &variable);
                        printf("Finally enter the power of the function. ");
                        scanf("%d", &ipower);
                        ipower = ipower + 1;
                        idivisor = ipower + 1;
                        printf("The indefinte integral of the function is:  %d%s^%d / %d \n", iconstant, &variable, ipower, idivisor);
                    }
                }
            }
            else if (intype == 2)
            {
                printf("Definite Integrals");
            }
            else if (intype == 3)
            {
                printf("Substitution");
            }
            else if (intype == 4)
            {
                printf("Trignometric Integrals");
            }
            else if (intype == 5)
            {
                printf("Integrations by Part");
            }
            else if (intype == 6)
            {
                printf("Exponential Problems");
            }
            else if (intype == 7)
            {
                printf("Natural Log Problems");
            }
        }
    }
}

我似乎无法找到导致错误的原因,如果我的信息不够具体,我道歉。我知道有更有效的方法来执行此程序,所以请不要更改我的代码,而只是告诉我我做错了什么以及如何解决它。谢谢。

2 个答案:

答案 0 :(得分:1)

这是因为条件else if (probtype == 2)位于条件if (probtype == 1)内并与其他if相关联。 而是在之前添加另一个支撑:

} // <--- Add an extra brace here in addition to what you have.
else if (probtype == 2)
{

并在外部if声明的末尾删除一个。

你有太多嵌套的 if else 语句。我建议您重新编写代码以使用switch - case和/或为子任务使用单独的函数,以便您的代码更易读,并且您可以轻松调试。

答案 1 :(得分:0)

我看到的一个问题是:

scanf("%s", &variable);

这在语法上是正确的但在语义上是不正确的。由于variable的类型为char,因此您无法将字符串读入其中。您最终访问了不应该访问的内存,并导致未定义的行为。

您需要以下内容:

char variable[100]; // Make the array as large you need to
scanf("%s", variable);

PS 修复此问题可能无法解决任何其他问题。