如果声明成功后如何停止? C编程

时间:2017-02-02 00:15:59

标签: c if-statement

我试图让用户输入以找出工资的扣除额,因此他们将他们的ID和工资放在该ID号的范围内。例如,如果我输入ID 1然后输入500的工资,则应输出输入的工资,扣除的工资和净工资。它完成了所有这些工作,但是当它检查工资是否在正确的ID号范围内时,即使检查了输入的ID,它也会检查每个ID号。

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

int main()
{
    int ID;  // variable for ID
    float Base_Salary; // variable for base salary
    float Amount_Deducted; // variable for amount deducted from employee
    float Net_Salary; // variable for employees' net salary

    printf("%s", "Enter ID: \n"); // prompt for ID number
    scanf("%d", &ID );

    // Validate user's ID
    if (ID == 1)
        printf("You have entered the ID 1\n");
    else { if (ID == 2)
            printf("You have entered the ID 2\n");
            else { if (ID == 3)
                printf("You have entered the ID 3\n");
                    else { if (ID == 4)
                        printf("You have entered the ID 4\n");
                            else { if (ID == 5)
                                printf("You have entered the ID 5\n");
                                    else { if (ID < 1 && ID > 5)
                                        printf("You didn't enter a proper ID\n");
                                    }
                            }
                    }
            }
    }

    printf("%s", "Enter your salary in the appropriate range: \n");
    scanf("%f",&Base_Salary);

    // Validations

       if (ID == 1 && Base_Salary >= 100 && Base_Salary <= 1000) {
            printf("Base salary is in range for ID given.\n");
       }
       else {
            printf("Salary must be between 100-1000 for ID 1.\n");
       }

       if (ID == 2 && Base_Salary >= 1001 && Base_Salary <= 5000) {
            printf("Base salary is in range for ID given.\n");
       }
       else {
            printf("Salary must be between 1001-5000 for ID 2.\n");
       }

       if (ID == 3 && Base_Salary >= 5001 && Base_Salary <= 10,000) {
            printf("Base salary is in range for ID given.\n");
       }
       else {
            printf("Salary must be between 5001-10,000 for ID 3.\n");
       }

       if (ID == 4 && Base_Salary >= 10,001 && Base_Salary <= 15,000) {
            printf("Base salary is in range for ID given.\n");
       }
       else {
            printf("Salary must be between 10,001-15,000 for ID 4.\n");
       }

       if (ID == 5 && Base_Salary >= 15,001 && Base_Salary <= 20,000) {
            printf("Base salary is in range for ID given.\n");
       }
       else {
            printf("Salary must be between 15,001-20,000 for ID 5.\n");
       }

    // Calculations
    if (ID == 1) {
        Amount_Deducted = Base_Salary * 0.50;
        Net_Salary = Base_Salary - Amount_Deducted;
        printf("The Base Salary you have entered = %.2f.\n", Base_Salary);
        printf("The Amount of salary deducted is = %.2f.\n", Amount_Deducted);
        printf("The Net Salary is = %.2f.\n", Net_Salary);
    }
    if (ID == 2) {
        Amount_Deducted = Base_Salary * 1.50;
        Net_Salary = Base_Salary - Amount_Deducted;
        printf("The Base Salary you have entered = %.2f.\n", Base_Salary);
        printf("The Amount of salary deducted is = %.2f.\n", Amount_Deducted);
        printf("The Net Salary is = %.2f.\n", Net_Salary);
    }

    if (ID == 3) {
        Amount_Deducted = Base_Salary * 2.50;
        Net_Salary = Base_Salary - Amount_Deducted;
        printf("The Base Salary you have entered = %.2f.\n", Base_Salary);
        printf("The Amount of salary deducted is = %.2f.\n", Amount_Deducted);
        printf("The Net Salary is = %.2f.\n", Net_Salary);
    }

    if (ID == 4) {
        Amount_Deducted = Base_Salary * 3.50;
        Net_Salary = Base_Salary - Amount_Deducted;
        printf("The Base Salary you have entered = %.2f.\n", Base_Salary);
        printf("The Amount of salary deducted is = %.2f.\n", Amount_Deducted);
        printf("The Net Salary is = %.2f.\n", Net_Salary);
    }

    if (ID == 5) {
        Amount_Deducted = Base_Salary * 4.50;
        Net_Salary = Base_Salary - Amount_Deducted;
        printf("The Base Salary you have entered = %.2f.\n", Base_Salary);
        printf("The Amount of salary deducted is = %.2f.\n", Amount_Deducted);
        printf("The Net Salary is = %.2f.\n", Net_Salary);
    }

}

4 个答案:

答案 0 :(得分:1)

看起来switch-caseif-else更合适:

// Validate user's ID
switch (ID)
{
case 1: 
    printf("You have entered the ID 1\n");
    if (Base_Salary >= 100 && Base_Salary <= 1000)
    {
        printf("Base salary is in range for ID given.\n");
    }
    Amount_Deducted = Base_Salary * 0.50;
    Net_Salary = Base_Salary - Amount_Deducted;
    break;

case 2:
    printf("You have entered the ID 2\n");
    /* Base Salary & Amount Deducted, code here. */
    break;

case 3:
    printf("You have entered the ID 3\n");
    /* Base Salary & Amount Deducted, code here. */
    break;

case 4:
    printf("You have entered the ID 4\n");
    /* Base Salary & Amount Deducted, code here. */
    break;

case 5:
    printf("You have entered the ID 5\n");
    /* Base Salary & Amount Deducted, code here. */
    break;

default:
    printf("You didn't enter a proper ID\n");
    break;
}

答案 1 :(得分:1)

要解决您的确切问题,请使用elseif。

如果IF语句为真,则跳过其下的所有其他elseif。

if (ID == 1) {
    Amount_Deducted = Base_Salary * 0.50;
    Net_Salary = Base_Salary - Amount_Deducted;
    printf("The Base Salary you have entered = %.2f.\n", Base_Salary);
    printf("The Amount of salary deducted is = %.2f.\n", Amount_Deducted);
    printf("The Net Salary is = %.2f.\n", Net_Salary);
}

else if (ID == 2) {
        Amount_Deducted = Base_Salary * 1.50;
        Net_Salary = Base_Salary - Amount_Deducted;
        printf("The Base Salary you have entered = %.2f.\n", Base_Salary);
        printf("The Amount of salary deducted is = %.2f.\n", Amount_Deducted);
        printf("The Net Salary is = %.2f.\n", Net_Salary);
    }

else if (ID == 3) {
    Amount_Deducted = Base_Salary * 2.50;
    Net_Salary = Base_Salary - Amount_Deducted;
    printf("The Base Salary you have entered = %.2f.\n", Base_Salary);
    printf("The Amount of salary deducted is = %.2f.\n", Amount_Deducted);
    printf("The Net Salary is = %.2f.\n", Net_Salary);
}

else if (ID == 4) {
    Amount_Deducted = Base_Salary * 3.50;
    Net_Salary = Base_Salary - Amount_Deducted;
    printf("The Base Salary you have entered = %.2f.\n", Base_Salary);
    printf("The Amount of salary deducted is = %.2f.\n", Amount_Deducted);
    printf("The Net Salary is = %.2f.\n", Net_Salary);
}

else if (ID == 5) {
    Amount_Deducted = Base_Salary * 4.50;
    Net_Salary = Base_Salary - Amount_Deducted;
    printf("The Base Salary you have entered = %.2f.\n", Base_Salary);
    printf("The Amount of salary deducted is = %.2f.\n", Amount_Deducted);
    printf("The Net Salary is = %.2f.\n", Net_Salary);
}

更简洁的方法是使用数组并使用索引来循环它们,因为您的代码在每个IF块内是非常冗余的。您可以轻松地将其重构为方法,或者在FOR循环中编写它以最大化代码重用并减少代码重复。

答案 2 :(得分:0)

逻辑说&#34;如果ID == 1&amp;&amp; ..检查工资范围...否则打印错误&#34;,这意味着对于每个不是1的ID,您将打印错误。

我想你想改变这个:

if (ID == 1 && Base_Salary >= 100 && Base_Salary <= 1000) {
    printf("Base salary is in range for ID given.\n");
}
else {
    printf("Salary must be between 100-1000 for ID 1.\n");
}

到此:

if (ID == 1) {
    if (Base_Salary >= 100 && Base_Salary <= 1000) {
        printf("Base salary is in range for ID given.\n");
    }
    else {
        printf("Salary must be between 100-1000 for ID 1.\n");
    }
}

你也必须改变其他每个块。

为了回应其他一些答案,如果您使用数组来保存薪资信息和循环来迭代工资,那么这段代码会更好,你可以摆脱所有难以判断的if / else语句阅读并难以维护。

答案 3 :(得分:0)

继续原始评论,您的编译器应该告诉您最初的错误在哪里。如果您没有启用警告,那么养成至少使用-Wall -Wextra进行编译的习惯,然后读取不接受编译的代码有任何警告。 (您不太可能遇到无法消除警告的任何合法情况)如果您愿意,还可以添加-pedantic以获取其他警告。

你的编译器应该告诉你,例如

salary.c:55:60: warning: left-hand operand of comma expression has no effect
[-Wunused-value]
     if (ID == 3 && Base_Salary >= 5001 && Base_Salary <= 10, 000) {
                                                            ^

甚至可以为您提供指向问题的抑扬症'^')(例如,您不能在数字中加入comma

其次,当您开始使用C编程时,始终验证所有用户输入。如果您在使用scanf(通过检查返回)时未能验证是否进行了正确的转化,那么您实际上无法确信从该点处理实际值转发你的代码。没有什么困难,只需验证预期转换的数量确实发生了。您还可以验证当时输入的值的范围,例如(使用简单常量定义#define IDMAX 5):

printf ("Enter ID: ");  /* prompt for ID number & validate */
if (scanf ("%d", &ID) != 1 || ID < 1 || ID > IDMAX) {
    fprintf (stderr, "error: invalid ID\n");
    return 1;
}

注意 高度不太可能通过乘以大于Amount_Deducted的值来实际计算1,否则您的{ {1}} 否定

最后,虽然有很多方法可以验证范围和扣除率,但在类似的情况下,一个简单的查找表由数组创建,提供了非常有效的解决方案,而不是无数的Net_Salaryif ... else ...语句。例如:

switch

示例使用/输出

#include <stdio.h>

#define IDMAX 5

int main (void)   /* affirmatively indicate no arguments expected */
{
    int ID = 0,   /* initialize variables */
        range [][2] = {{   100,  1000}, /* salary range lookup */
                       {  1001,  5000},
                       {  5001, 10000},
                       { 10001, 15000},
                       { 15001, 20000}};
    float Base_Salary = 0.0,
        Amount_Deducted = 0.0,
        Net_Salary = 0.0,
        rate[] = { .05, .15, .25, .35, .45 };  /* rate lookup */

    printf ("Enter ID: ");  /* prompt for ID number & validate */
    if (scanf ("%d", &ID) != 1 || ID < 1 || ID > IDMAX) {
        fprintf (stderr, "error: invalid ID\n");
        return 1;
    }

    printf ("Enter your salary in the appropriate range: ");
    if (scanf ("%f", &Base_Salary) != 1) { /* validate entry */
        fprintf (stderr, "error: invalid Base_Salary\n");
        return 1;
    }

    /* validate Base_Salary in range */
    if (Base_Salary < range[ID-1][0] || Base_Salary > range[ID-1][1]) {
        fprintf (stderr, "error: Salary must be between %d-%d for ID %d.\n",
                range[ID-1][0], range[ID-1][1], ID);
        return 1;
    }
    else
        printf ("Base salary is in range for ID given.\n");

    Amount_Deducted = Base_Salary * rate[ID-1]; /* calculations */
    Net_Salary = Base_Salary - Amount_Deducted;

    printf ("The Base Salary you have entered = %.2f.\n", Base_Salary);
    printf ("The Amount of salary deducted is = %.2f.\n",
            Amount_Deducted);
    printf ("The Net Salary is = %.2f.\n", Net_Salary);

    return 0;   /* main is type 'int' and returns a value */
}

始终在启用警告的情况下进行编译,除了在没有警告的情况下进行编译之前不要使用任何代码,并且您的调试时间将大大减少。代码注释中还包含一些额外的指针。如果您有任何问题,请告诉我。

最后注释虽然不是错误,但C的标准样式通常会避免使用$ ./bin/salary Enter ID: 1 Enter your salary in the appropriate range: 99 error: Salary must be between 100-1000 for ID 1. $ ./bin/salary Enter ID: 1 Enter your salary in the appropriate range: 1000 Base salary is in range for ID given. The Base Salary you have entered = 1000.00. The Amount of salary deducted is = 50.00. The Net Salary is = 950.00. $ ./bin/salary Enter ID: 2 Enter your salary in the appropriate range: 1000 error: Salary must be between 1001-5000 for ID 2. Mixed_Case变量名来支持所有小写并为宏和预处理器定义保留大写。您的变量名称保留原样,以帮助您跟进。这是一个不错的选择,但值得注意。