mercredi 1 février 2017

How to stop after successful if statement? C programming

I'm trying to get user input to find out deductions from salary and so they put their ID and a salary that is in range with that ID number. For example if I input ID 1 and then a salary of 500 and it should output the salary entered, salary deducted, and net salary. It does all of that, but where it checks if the salary is in range for the correct ID number it checks it with every ID number even after the ID entered has been checked.

#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);
}

}

Aucun commentaire:

Enregistrer un commentaire