Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The result of the algorithm always return zero – C

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…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • I’m using vscode..

>Solution :

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;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading