I am trying to write this code, but I am having problem with using floating point in the while. The code works, but when I input the number, I get some strange result. What's wrong?

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

int main(int argc, char *argv[])
{
    int hourWorked;
    double hourlyRate;
    double total;

    total = 0;

    double salary;


   printf( "Enter hour Worked: ");
   scanf("%d", &hourWorked);

   while (hourWorked != -1) {
   total = total + hourWorked;

   printf( "hourly Rate: " );
   scanf("%d", &hourlyRate);

   // Initialize gallon.
   printf( "Enter hour Worked: ");
   scanf("%d", &hourWorked);
   
   salary = total * hourlyRate;
   printf( "Miles gallons  was %d\n\n", salary );

}

 

  system( "PAUSE");
  return 0;
}

Edited 4 Years Ago by arold10: n/a

scanf("%d", &hourlyRate);

%d is for integer input. For double input use %lf.

printf( "Miles gallons  was %d\n\n", salary );

Just like scanf(), printf() expects you to tell the truth about what type of variable is being printed. salary is a double, but %d says that it's an int. That technically causes undefined behavior, and you want to use %f instead.

On a side note, scanf() and printf() format specifiers are not the same. %f works for float *and* double when it comes to printf(), but scanf() differentiates them with %f for float and %lf for double. So be careful. :)

I also noticed that you ask for the new hours worked too soon inside the loop. That should be moved to be the very last step:

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

int main(void)
{
    int hourWorked;
    double hourlyRate;
    double total = 0;
    double salary;

    printf( "Enter hour Worked: ");
    scanf("%d", &hourWorked);

    while (hourWorked != -1) {
        total = total + hourWorked;

        printf( "hourly Rate: " );
        scanf("%lf", &hourlyRate);
        
        salary = total * hourlyRate;
        printf( "Miles gallons was %f\n\n", salary );

        printf( "Enter hour Worked: ");
        scanf("%d", &hourWorked);
    }
    
    system("PAUSE");
    return 0;
}

Edited 4 Years Ago by deceptikon: n/a

%d is for integer input. For double input use %lf.

I also noticed that you ask for the new hours worked too soon inside the loop. That should be moved to be the very last step:

Thanks for response. I did make some adjustment to my code and I keep getting a weird number. You are talking about that I ask for the new hours worked too soon inside the loop. I ask it at the very last step as you said, I still get the same weird number.

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

int main(int argc, char *argv[])
{
    int hourWorked;
    double hourlyRate;
    double total;

    total = 0;

    double salary;


   printf( "Enter hour Worked: ");
   scanf("%lf", &hourWorked);

   while (hourWorked != -1) {
   total = total + hourWorked;

   printf( "hourly Rate: " );
   scanf("%f", &hourlyRate);

   salary = total * hourlyRate;
   printf( "The salary is %f\n\n", salary );

   // Initialize hour worked.
   printf( "Enter hour Worked: ");
   scanf("%lf", &hourWorked);
}
scanf("%lf", &hourWorked);

Now you have the opposite problem. hourWorked is an int, so you should use %d in scanf().

scanf("%f", &hourlyRate);

And here it should be %lf because hourlyRate is a double and not a float.

I posted working code before, and I'll do it again. Please read it carefully:

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

int main(int argc, char *argv[])
{
    int hourWorked;
    double hourlyRate;
    double total;

    total = 0;

    double salary;


    printf( "Enter hour Worked: ");
    scanf("%d", &hourWorked);

    while (hourWorked != -1) {
        total = total + hourWorked;

        printf( "hourly Rate: " );
        scanf("%lf", &hourlyRate);

        salary = total * hourlyRate;
        printf( "The salary is %f\n\n", salary );

        // Initialize hour worked.
        printf( "Enter hour Worked: ");
        scanf("%d", &hourWorked);
    }

    system("PAUSE");
    return 0;
}

Now you have the opposite problem. hourWorked is an int, so you should use %d in scanf().


And here it should be %lf because hourlyRate is a double and not a float.

I posted working code before, and I'll do it again. Please read it carefully:

Thank you very much, it works perfect now. It was just a minor mistake that was messing up my code. I am practicing for a test that I'll take tomorrow.

Edited 4 Years Ago by arold10: fixing something

This article has been dead for over six months. Start a new discussion instead.