Beginner program error in C

Asked

Viewed 65 times

0

I am starting to program now and I have a problem in the example below. The question asks to calculate the income tax, according to the salary received by the person. The mistake is that, regardless of the amount of salary I put as input, the program prints "exempt", even for values that surpass the intevalo relative to the exemption.

include

int main (){
    double salario, aux1, aux2, aux3;

    printf("digite seu salario: ");
    scanf("%lf", &salario);

    if(salario>=0.00 || salario<=2000.00){
        printf("Isento");}
    else {if(salario>=2000.01 || salario<3000.00){
        aux1=salario*0.08;
        printf("O valor a ser pago e %.2lf", &aux1);}

    else {if(salario>=3000.01 || salario<=4500.00){
        aux2=salario*0.18;
        printf("o valor a ser pago e %.2lf", &aux2);}

    else {if(salario>=4500.01){
        aux3=salario*0.28;
        printf("O valor a ser pago e %.2lf", &aux3);}
            }
          }
        }


return 0;

}

4 answers

3

Your problem is here:

if(salario>=0.00 || salario<=2000.00){

Any value will meet such condition (all positive numbers will be greater than or equal to zero and all negative numbers will be less than or equal to 2000)

I believe the correct test is:

if(salario>=0.00 && salario<=2000.00){

The same applies to all other if commands.

  • Wow, really. I had not attempted it. Thank you so much!

2

Besides this problem with operators && and ||, there are other problems as well. By calling the printf you are passing the address operator &, so it shows the address of the variable in memory and not its value.

Errado - printf("O valor a ser pago e %.2lf", &aux1);}
Correto - printf("O valor a ser pago e %.2lf", aux1);}

And a beginner’s tip. Train your code a lot. Bons Estudo!

1


The problem is in the logical identifier || in which case you should use the &&.

In your logic you’re saying that wages can be greater than 0 or less than 2000, only 2001 is already greater than 0 so the return is true.

you would use || if for example you needed such an expression x < 1000 || x > 2000

0

This answer is a reformatted version because the program as it was posted is badly formatted.

int main ()
{
  double salario, aux;

  printf("digite seu salario: ");
  scanf("%lf", &salario);

  if (salario >= 0.00 && salario <= 2000.00)
  {
    printf("Isento");
  }

  else if (salario >= 2000.01 && salario < 3000.00)
  {
    aux = salario * 0.08;
    printf("O valor a ser pago e %.2lf", aux);
  }

  else if (salario >= 3000.01 && salario <= 4500.00)
  {
    aux = salario * 0.18;
    printf("o valor a ser pago e %.2lf", aux);
  }

  else if (salario >= 4500.01)
  {
    aux = salario * 0.28;
    printf("O valor a ser pago e %.2lf", aux);
  }

  else
  {
    printf("salario digitado negativo!");
  }

  return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.