Doubt of if He is

Asked

Viewed 175 times

3

Good evening, I was testing this C code of an activity* of my course, works ok if (c<101||c>103), works ok if if (c=101), but if (c=102) if instead of returning me ns=s+(s*0.2), is returning me the previous one, ns=s+(s*0.1), the same is true if if (c=103) which should return ns=s+(s*0.3) but also returns ns=s+(s*0.1). Could you clear me of the question of what the problem is and how I should fix it? Thank you.

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

main()

{
float s,c,ns;
printf ("Cod. do Cargo:");
scanf ("%f",&c);
printf ("Salario:");
scanf ("%f",&s);
if (c<101||c>103)
{
 ns=s+(s*0.4);
}
else if (c=101)
{
 ns=s+(s*0.1);
}
else if (c=102)
{
 ns=s+(s*0.2);
}
else if (c=103)
{
 ns=s+(s*0.3);
}
printf ("Salario Antigo: R$ %.2f",s);
printf ("\nNovo Salario: R$ %.2f",ns);
printf ("\nDiferenca: R$ %.2f",ns-s);
}

*

*The activity was this: A company will grant a salary increase to its employees, variable according to the position, according to the table below. Make an algorithm that reads the salary and position of a employee and calculate the new salary. If the employee’s position does not is in the table, it should then receive a 40% increase. Show the old salary, new salary and difference.
Code Position Percentage 101 Manager 10% 102 Engineer 20% 103 Technician 30%

*

2 answers

4


The problem in your code is in the block below

else if (c=101)
{
  ns=s+(s*0.1);
}
else if (c=102)
{
  ns=s+(s*0.2);
}
else if (c=103)
{
  ns=s+(s*0.3);
}

The statements of the conditions imposed on us if.. Else do not result in a comparison of values, but rather an assignment of values. Therefore, in your case, regardless of the value that is assigned, if it does not meet the condition if (c<101||c>103) it will always meet the condition if(c=101) and never again the following since the value of c after if(c=101) will be 101.

Remember, in the C language the command to make a comparison is the ==

Below is your code with corrections

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

int main(void){
 float s,c,ns;

 printf ("Cod. do Cargo:");
 scanf ("%f",&c);

 printf ("Salario:");
 scanf ("%f",&s);

 if (c<101||c>103) {
   ns=s+(s*0.4);
 }
 else if (c==101) {
   ns=s+(s*0.1);
 }
 else if (c==102)
 {
   ns=s+(s*0.2);
 }
 else if (c==103){
   ns=s+(s*0.3);
 }

 printf ("Salario Antigo: R$ %.2f",s);
 printf ("\nNovo Salario: R$ %.2f",ns);
 printf ("\nDiferenca: R$ %.2f",ns-s);

 return 0;
}
  • Perfect, I missed the difference between = and ==, thank you very much!

  • @Newtonsandey if the answer solved the problem, you can accept it as correct!

3

From what I’ve observed in your code, the error is in condition. In C, like some other languages, the relational operator for testing the equality condition is "==". When you use only one "=" you are assigning a value to a variable, so it is always returning the previous value and not the expected answer.

So, your if would look something like this:

if(c == 101){
    //Código a ser executado.
}

  • 1

    Perfect, I missed the difference between = and ==, thank you very much!

Browser other questions tagged

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