Home > Blockchain >  The result of the algorithm always return zero - C
The result of the algorithm always return zero - C

Time:11-26

I'm trying make an algorithm to calculate a readjusted salary from employee, but always the algorithm returns zero and still gets an error message:

lucas@lucas-pc:~/VSCodeProjects/C$ cd "/home/lucas/VSCodeProjects/C/" && gcc logic.c -o logic && "/home/lucas/VSCodeProjects/C/"logic
logic.c: In function ‘main’:
logic.c:14:32: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘float *’ [-Wformat=]
   14 |    printf("Readjusted salary: %f", &readjustedSalary);
      |                               ~^   ~~~~~~~~~~~~~~~~~
      |                                |   |
      |                                |   float *
      |                                double

Algorithm:

#include <stdio.h>

int main() {
   float salary, readjustedSalary;
   printf("Enter your salary: ");
   scanf("%f", &salary);

   if(salary >= 5000){
      readjustedSalary = salary   (salary * 0.2);
   } else {
      readjustedSalary = salary   (salary * 0.3);
   }

   printf("Readjusted salary: %f", &readjustedSalary);
   return 0;
}

What I doing wrong? I'm beginning on C...

  • I'm using vscode..

CodePudding user response:

Your problem is that you're using & in printf, when you use & you mean the memory position of a variable (summarizing). So to write a value to a variable (using scanf) you need to pass the memory position, but to print it, no.

#include <stdio.h>

int main() {
   float salary, readjustedSalary;
   printf("Enter your salary: ");
   scanf("%f", &salary);

   if(salary >= 5000){
      readjustedSalary = salary   (salary * 0.2);
   } else {
      readjustedSalary = salary   (salary * 0.3);
   }

   printf("Readjusted salary: %f", readjustedSalary);
   return 0;
}
  •  Tags:  
  • c
  • Related