程序无法正确打印

时间:2011-04-23 00:09:35

标签: c++

我相信我的程序在声明无效之后仍然会通过if语句。它应该在id的同一行打印无效的填充状态或无效的免除状态。但是它打印了id上方的有效语句,并且仍然显示taxableIncome和taxRate以及TaxAmount。     while(fscanf(tax,“\ n%d%c%d%d”,& taxpayerId,& fillingStatus,& grossIncome,& taxExemptions)!= EOF)     {

                    fillingStatus = toupper(fillingStatus);

                    if ( fillingStatus == 'S')
                    {
                        taxableIncome = grossIncome - 3000 - 1000 * taxExemptions;

                    }
                    else if ( fillingStatus == 'M')
                    {
                        taxableIncome = grossIncome - 3000 - 1000 * taxExemptions;

                    }   
                    else if ( fillingStatus == 'J')
                    {
                        taxableIncome = grossIncome - 6000 - 1000 * taxExemptions;

                    }

                    if (taxableIncome < 0)
                    {
                        taxableIncome = 0;
                    }


                    if ( fillingStatus == 'S')
                    {
                            if (taxableIncome < 5000)
                            {
                                taxRate = 0.15;
                            }
                            if (taxableIncome  > 20000)
                            {
                                taxRate = 0.31;
                            }
                            else
                            {
                                taxRate = 0.22;
                            }
                    }     
                    else if ( fillingStatus == 'M')
                    {
                            if (taxableIncome < 10000)
                            {
                                taxRate = 0.15;

                            }   
                            if (taxableIncome > 40000)
                            {
                                taxRate = 0.31;
                            }
                            else
                            {
                                taxRate = 0.22;
                            }
                    }
                    else if ( fillingStatus == 'J')
                    {
                            if (taxableIncome < 7000)
                            {
                                taxRate = 0.17;
                            }
                            if (taxableIncome > 25000)
                            {    
                                taxRate = 0.33;
                            }
                            else
                            {
                                taxRate = 0.24;
                            } 
                    }
                    else
                    {   
                            printf("\n %d     **** Invalid filling status ****",taxpayerId);
                            continue;
                    }

                    if (taxExemptions > 12  || taxExemptions < 0) 
                    {
                        printf("\n %d     **** Invalid number of exemptions ****", taxpayerId);
                    } 
                    else 
                    {
                        taxAmount = taxableIncome * taxRate;
                        printf("\n %d %15.2f  %15.2f  %18.2f",taxpayerId,taxableIncome,taxRate,taxAmount);
                    }  

输出假设如下:

Taxpayer ID    Taxable Income    Tax Rate        Tax Amount
-----------    --------------    --------        ----------

111111111           4000.00        0.15            600.00
222222222          36500.00        0.22           8030.00
333333333          19152.00        0.24           4596.48
444444444    **** Invalid filing status ****
555555555          67000.00        0.31          20770.00
666666666          53197.00        0.33          17555.01
777777777          19000.00        0.22           4180.00
888888888    **** Invalid number of exemptions ****
999999999          46308.00        0.33          15281.64
101010101          91602.00        0.31          28396.62
121212121           9525.00        0.15           1428.75
131313131              0.00        0.17              0.00

我的出局看起来像这样:

  

纳税人ID应税收入
  税率税额       ----------- -------------- -------- ----------

111111111           4000.00        0.22            880.00
222222222          36500.00        0.31          11315.00
333333333          19152.00        0.24           4596.48
444444444          **** Invalid filing status ****           
555555555          67000.00        0.31          20770.00
666666666          53197.00        0.24          12767.28
777777777          **** Invalid number of exemptions ****        
888888888           3543.00        0.22
999999999          46308.00        0.24            779.46
101010101          91602.00        0.22          11113.92
121212121           9525.00        0.31           2952.75
131313131              0.00        0.24              0.00

5 个答案:

答案 0 :(得分:4)

fillingStatus == 's' || 'S'

上述情况并不符合你的想象。尝试:

fillingStatus == 's' || fillingStatus == 'S'

原因是==运算符一次只比较一对值。所以你的表达式如下:

  

if(fillingStatus是's')或('S'非零)

始终为真(因为'S'始终为非零)。更正的表达式如下:

  

if(fillingStatus是's')或(fillingStatus是'S')

值得注意的是,与上述C ++代码相比,其他一些语言表达此条件的方式较为冗长。例如,在Python中,您可以编写:

if fillingStatus in ('s', 'S'):

fillingStatus's'测试'S'

答案 1 :(得分:2)

您可以考虑在scanf之后执行以下操作:

fillingStatus = toupper(fillingStatus);

然后只测试大写字符:

fillingStatus == 'S'

您需要按照建议修正'ifs'并打印输出,如下所示。但是,在你修复'ifs'之前,这将是不对的。

if (taxExemptions > 12  || taxExemptions < 0) {
    printf("\n %d \t**** Invalid exemptions status ****", taxpayerId);
} else {
    taxAmount = taxableIncome * taxRate;
    printf("\n %d \t %.2f \t\t %.2f \t %15.2f",taxpayerId,taxableIncome,taxRate,taxAmount);
}

我越看这段代码,我就越困惑。对于taxableIncome为1且fillStatus =='S'的人,税率应该是多少?我认为这将是0.31,因为它不到20000.可能不是你想要的。我认为你错过了一些'else if'的陈述。

答案 2 :(得分:0)

更改此

 else
    {       
        printf("\n**** Invalid filling status ****");
    }

    if (taxExemptions > 12  || taxExemptions < 0)
    {       
        printf("\n**** Invalid exemptions status ****");
    } 

else
    {       
        printf("\n**** Invalid filling status ****");  
        continue;
    }

    if (taxExemptions > 12  || taxExemptions < 0)
    {       
        printf("\n**** Invalid exemptions status ****");
        continue;
    } 

答案 3 :(得分:0)

编辑:
基于更新的问题,很明显问题不在于可怕的if语句,而在于输入数据 - 您选择的错误记录是豁免数量无效。这意味着两件事:

  • 你应该早点发布这个并节省了很多时间在错误的地方找人

  • 如果您想要了解标记错误记录的原因,则需要显示您正在使用的数据。

我删除了原来的答案。下次请发布所有相关详细信息。

答案 4 :(得分:0)

您不能在C ++中使用数字内的逗号。逗号是comma operator

所以代替7,000写7000。

if(taxable_income>7,000)
{
   x;
}

永远不会执行x;