2

I'm trying to write a program where the user gets to convert from Fahrenheit to Celsius or Celsius to Fahrenheit. When I run the program I enter 68 and it returns with -17.78 instead of 20 like it should.

I looked through lots of different forums and the only solution I could find was changing the data type from integer to double but I already did that.

double temp;

printf("Input temperature in degrees Fahrenheit:");
scanf("%.2f", &temp);
temp = (5.0f/9.0f)*(temp-32.0f);
printf("The temperature in Celsius is %.2f.", temp);
return 0;

On paper it seems to me like everything is correct is there something I am missing?

  • Yeah I've tried that already. It just changes the return value to -17.777779. – Ian Rueschhoff Apr 06 '19 at 19:18
  • ohhh god you were right. Lmao thank you so much. – Ian Rueschhoff Apr 06 '19 at 19:19
  • 3
    The format specifier for `double` is `%lf`, not `%f` – Mark Benningfield Apr 06 '19 at 19:23
  • 2
    A good way to debug this would have been to print out the input value before doing anything to it, to make sure you were in fact operating on the number you thought you were. I.e., narrow down the point of failure. Then you probably wouldn't have needed to post here. – Tom Karzes Apr 06 '19 at 19:25
  • Try converting -17.68°C back to °F and you'll get a value close to 0. That should tell you to look at the value of `temp` before the conversion. – Caleb Apr 07 '19 at 05:29

2 Answers2

5

Why isn't my equation working for converting Fahrenheit to Celsius?

Compiler warnings not fully enabled.


scanf("%f", ...); expects a float *, not the double * provided.

"%.2f" --> Precision in scanf() is undefined behavior. Simple drop that. scanf() does not provide limiting input on precision.

double temp;
printf("Input temperature in degrees Fahrenheit:");
// scanf("%.2f", &temp);

scanf("%lf", &temp);

I suggest a trailing '\n' in your printf().

// printf("The temperature in Celsius is %.2f.", temp);
printf("The temperature in Celsius is %.2f.\n", temp);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

I changed your program like this and everything looks good:

int main()
{
    float temp;

    printf("Input temperature in degrees Fahrenheit:");
    scanf("%f", &temp);
    temp = (5.0f/9.0f)*(temp-32.0f);
    printf("The temperature in Celsius is %.2f.", temp);
    return 0;
}

Pay attention to compiler warnings too. For example for your code compiler says warning: C4476: 'scanf' : unknown type field character '.' in format specifier so I removed . from scanf parameter.

s4eed
  • 7,173
  • 9
  • 67
  • 104
  • If you required such precision, you can also scan into the double with `%lf`. The data type and scan specifiers have to be in sync. – Rafael Apr 06 '19 at 19:27
  • 3
    Why `%2f` and not `%f`? The restriction will damage any following input. For example, `scanf("%2f%2f", &a, &b);` with input `1.2 3.4 \n` will result in `a=1.0` and `b=2.0` which is quite wrong. – Weather Vane Apr 06 '19 at 19:35
  • @WeatherVane You're right. I copied SO code. I fixed it. – s4eed Apr 07 '19 at 05:35