c - 输入某些值时如果出错则嵌套

标签 c

当输入值 60000.01、70000.01、75000.01 和 88000.01 时,嵌套 if 语句出现问题,RaisePrcnt 值不会打印到控制台。我没有发现嵌套 if 结构的缺陷,除了那些特定值之外的任何其他值都可以正常工作。

#include<stdio.h>
#include<stdlib.h>

void main()
{
    // Employee and Department name variable declarations.
    char Name1[25], Dept1[25];  

    /* Variable declarations for current yearly income, raise percentage, 
    new raise amount and the new yearly pay amount.*/
    float Income1, NewPay1;
    float RaisePrcnt1 = 9.7, RaisePrcnt2 = 8.5, RaisePrcnt3 = 7.2;
    float RaisePrcnt4 = 6.3, RaisePrcnt5 = 5.9, Percentage = 0;

    /*Initialize and declare variables to calculate the total current
    yearly incomes, total raise amounts and total new pay amounts for
    all 4 employees.*/
    float IncomeTotal = 0;
    float RaiseTotal = 0; 
    float PayTotal = 0;

    // Display program title.
    printf("\t\t\t\tINCOME CALCULATOR\n\n"); 

    // User directive.
    printf("\t\t************************************************\n");
    printf("\t\tPLEASE ENTER THE FOLLOWING EMPLOYEE INFORMATION: "); 
    printf("\n\t\t************************************************\n\n");

    /************************************************************
    BEGIN COLLECTING INFORMATION ON EMPLOYEE NUMBER 1.          *
    ************************************************************/
    printf("\t\tEmployee Number 1: \n");
    printf("\t\t________________________________________________\n");

    printf("\n\t\tName: \t\t\t"); // Prompt user for Name.
    gets(Name1); // Reads input for Name to the enter key.

    printf("\n\t\tDepartment: \t\t"); // Prompt user for Department Name.
    gets(Dept1); // Reads Department Name input to the enter key.

    // Prompt user for Current Yearly Income input.
    printf("\n\t\tCurrent Yearly Income: \t"); 
    scanf("%f", &Income1); // Reads Current Income input.

    // Prompt user for the Raise Percentage.
    //printf("\n\t\tRaise Percentage: \t");
    //scanf("%f", &RaisePercent1); // Reads Raise Percentage input.


    if(Income1 < 0 && Income1 != 0){
       printf("%0.1f The Income Amount entered is INVALID. \t");
    }else
        if(Income1 >= 0 && Income1 <= 60000){
           printf("%0.1f", RaisePrcnt1);
        }else
           if(Income1 >= 60000.01 && Income1 <= 70000){
              printf("%0.1f", RaisePrcnt2);
           }else
              if(Income1 >= 70000.01 && Income1 <= 75000){
                 printf("%0.1f", RaisePrcnt3);
              }else
                 if(Income1 >= 75000.01 && Income1 <= 88000){
                    printf("%0.1f", RaisePrcnt4);
                 }else
                    if(Income1 >= 88000.01){
                    printf("%0.1f", RaisePrcnt5);
                    }



 //Percentage = (Income1 * RaisePrcnt1);
 //Percentage = (Income1 * RaisePrcnt2);
 //Percentage = (Income1 * RaisePrcnt3);
// Percentage = (Income1 * RaisePrcnt4);
//Percentage = (Income1 * RaisePrcnt5);





    // Calculate and print the new Raise Amount for Employee Number 1.
    //RaiseAmt1 = (Income1 * RaisePercent1) / 100;
 //printf("\n\tBased on the information you have entered for Employee Number: 1\n"); 
//  printf("\t________________________________________________________________\n");
    //printf("\n\tThe New Raise Amount is: \t$ %0.2f", RaiseAmt1); 

    // Calculate and print the New Income Amount for Employee Number 1.
    //NewPay1 = Income1 + RaiseAmt1;
    //printf("\n\tThe New Pay Amount is: \t\t$%0.2f", NewPay1); 
    //printf("\n\n");

    // Calculate and incorporate Employee 1 figures into final totals.
    //IncomeTotal = IncomeTotal + Income1;
    //RaiseTotal = RaiseTotal + RaiseAmt1;
    //PayTotal = PayTotal + NewPay1;
    /*END EMPLOYEE 1.*******************************************/

    //fflush(stdin);


    // Title for totals.
    //printf("\n\n\t\t************************************************\n");
    //printf("\t\t\tINCOME TOTALS FOR ALL EMPLOYEES"); 
    //printf("\n\t\t************************************************\n\n");

    /*Calculate and print all totals for the 4 employees.*/
    //printf("\tCurrent Yearly Incomes \tTotal: $%10.2f", IncomeTotal);
    //printf("\n\tRaise Amounts \t\tTotal: $%10.2f", RaiseTotal);
    //printf("\n\tNew Yearly Incomes \tTotal: $%10.2f", PayTotal);
    //printf("\n\n");


    system("PAUSE");
    //return (0);

} // End main.

最佳答案

这是一个 floating point precision error (也涵盖得很好 here )。仅仅因为您将 Income1 输入为 60000.01 并不意味着 Income1 >= 60000.01 为真。

事实上,由于您目前已经构建了 if 语句,因此无需进行比较 - 一旦达到该点,您就已经知道 Income1 不小于 60000,这要归功于其他。只是做:

if(Income1 < 0){
   printf("%0.1f The Income Amount entered is INVALID. \t");
} else if (Income1 <= 60000){
       printf("%0.1f", RaisePrcnt1);
}else if (Income1 <= 70000){
          printf("%0.1f", RaisePrcnt2);
}else

等等。 (另请注意,您的深缩进是不必要的,因为它们实际上并不是嵌套的 ifs)。

关于c - 输入某些值时如果出错则嵌套,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12521357/

相关文章:

c - 对于 (*ptr)[],为什么 printf ("%p",(void*)ptr+1) 有效但 printf ("%p",ptr+1) 无效?

c - C中指针和数组的区别

c++ - C 的简单文本分析库

python - Cython:如何将 python 对象作为 cython 类的属性

c - 子进程如何从管道读取stdout,而父进程如何将stdin写入管道?

c - 在 main() 之外访问进程的参数

c - 已分配动态数组,但不能使用它

arrays - 为什么 const char *arr 的大小大于 const char arr[]?

c - 谁检测拼写错误的函数名称?编译器还是链接器?

c - 从 DTD 解析 C 语言的 XML 并相应地构建链表