samedi 7 novembre 2020

How do I turn the if else statements in this program to switch statements? Visual Studio 2019 C++

I've looked up examples of doing this but when I try to format the code like it, it doesn't work. Here is the program I want to convert.

#include <stdio.h> 

double comp_tax(double salary);

int
main(void)
{
    double salary, tax;

    printf("Input your annual salary >$ ");
    scanf_s("%lf", &salary);

    tax = comp_tax(salary);

    if (tax != -1.0)
        printf("your tax is $%.2f", tax);
    else
        printf("your salary is outside the table ranged");

    return 0;
}

double
comp_tax(double salary)
{
    double tax;
    
    if (salary < 0.0)
        tax = -1.0;
    else if (salary < 15000.00)
        tax = 0.15 * salary;
    else if (salary < 30000.00)
        tax = (salary - 15000.00) * 0.18 + 2250.00;
    else if (salary < 50000.00) 
        tax = (salary - 30000.00) * 0.22 + 5400.00;
    else if (salary < 80000.00)
        tax = (salary - 50000.00) * 0.27 + 11000.00;
    else if (salary <= 150000.00)   
        tax = (salary - 80000.00) * 0.33 + 21600.00;
    else
        tax = -1.0;

    return (tax);
}

Aucun commentaire:

Enregistrer un commentaire