我该如何解决if语句不被识别为true的问题

时间:2019-10-10 22:17:51

标签: c if-statement boolean scanf

我写了我的程序,用来检查某人是单身,已婚(分居还是共同),以及他们是否是户主,以计算他们必须缴纳的税款。由于某种原因,当我在第一个scanf中键入y时,下一个if语句将被跳过。请帮忙!这是我的整个代码。我知道它不是最优化的,我可以清理它,但我只是想先弄清楚问题出在哪里。

    void question3 (void)
    {
    char single, head, joint;
    int income = 0;
    double tax = 0;

    printf("What is your total income?");
    scanf("%d", &income);

    printf ("Are you single? (y/n) \n");
    scanf (" %c", &single);

    if (single == 'y')
    {
        if (income == 17850)
        {
            tax = income * .15;
        }
        else if (income >= 17850)
        {
            tax = ((.15 * 17850) + (.28 * (income - 17850)));
        }
    }
    else
    {
        if (single != 'y')
        {
        printf("Are you the head of the house hold? y or n\n");
        scanf(" %c", &head);
        }
    }

    if (head == 'y')
    {
        if (income == 23900)
        {
         tax = income * .15;
        }
        else if (income >= 23900)
        {
         tax = ((.15 * 23900) + (.28 * (income - 23900)));
        }
    }
    else if (head != 'y')
    {
        printf("Do you have a joint marriage\n");
        scanf(" %c", &joint);
    }

    if (joint == 'y')
    {
        if (income == 29750)
        {
         tax = income * .15;
        }
        else if (income >= 29750)
        {
        tax = ((.15 * 29750) + (.28 * (income - 29750)));
        }
    }
    else if (joint != 'y')
    {
        if(income == 14875)
        {
         tax = income * .15;
        }
        else if (income >= 14875)
        {
         tax = ((.15 * 14875) + (.28 * (income - 14875)));
        }

    }

    printf("You owe %f dollars in taxes.", tax);
    }

2 个答案:

答案 0 :(得分:0)

您可以使用函数来分解它,以便对于每个真实表达式,它都调用一个函数。在每个功能中,您都要求收入,然后得出结果。打印结果后,它将调用问题3重复该循环。

对于每个错误表达式,您都给出了另一个选择,这也调用了一个函数或给出了另一个选择。

char single, head, joint;
int income = 0;
double tax = 0;

void Single();
void Head();
void Joint();

void question3(void)
{
    printf("Are you single? (y/n) \n");
    scanf(" %c", &single);

    if (single == 'y')
    {
        Single();
    }
    else if (single == 'n')
    {
        printf("Are you the head of the house hold? y or n\n");
        scanf(" %c", &head);

        if (head == 'y')
        {
            Head();
        }
        else if (head == 'n')
        {
            printf("Do you have a joint marriage\n");
            scanf(" %c", &joint);

            if (joint == 'y')
            {
                Joint();
            }
            else if(joint == 'n')
            {
               question3();
            }
        }
    }
}

void Single()
{
    printf("What is your total income?");
    scanf("%d", &income);

    if (income == 17850)
    {
        tax = income * .15;
    }
    else if (income >= 17850)
    {
        tax = ((.15 * 17850) + (.28 * (income - 17850)));
    }

    printf("You owe %f dollars in taxes.\n\n", tax);

    question3();

    return 0;
}

void Head()
{
    printf("What is your total income?");
    scanf("%d", &income);

    if (income == 23900)
    {
        tax = income * .15;
    }
    else if (income >= 23900)
    {
        tax = ((.15 * 23900) + (.28 * (income - 23900)));
    }

    printf("You owe %f dollars in taxes.\n\n", tax);

    question3();

    return 0;
}

void Joint()
{
    printf("What is your total income?");
    scanf("%d", &income);

    if (income == 29750)
    {
        tax = income * .15;
    }
    else if (income >= 29750)
    {
        tax = ((.15 * 29750) + (.28 * (income - 29750)));
    }

    printf("You owe %f dollars in taxes.\n", tax);

    question3();

    return 0;
}

void main()
{
   question3();

return 0;
}

答案 1 :(得分:-1)

对于单个字符,应使用getchar()而不是scanf

single = getchar();

如果您坚持使用scanf

  • 您应该删除空格(因此scanf("%c", &single))。
  • 您可能应该测试scanf的返回值:它返回可以解析的arg的数量(在您的情况下,如果可以,则应为1,否则为0)。

另一方面,这没用:

if (single == 'y') {
  ...
} else if (single != 'y') {
  ...
}

如果您不在乎单个绝对等于y或n,那么您应该简单地写成这样:

if (single == 'y') {
  ...
} else {
  ...
}