-4

Question from book:

Write a program that does temperature conversion from Fahrenheit to Celsius, your program should :

  1. prompt the user for which type of conversion they want to do.
  2. prompt the user for the temperature they want to convert.

I am getting incorrect output.I'm not sure where i'm going wrong.I'm new to c language. Any help is greatly appreciated. Here is my code:

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


 int main()
 {int f, c, f_or_c;

 printf("Would you like to convert Fahrenheit (1) or Celsius (2)?\n");
 scanf("%d", &f_or_c);

 if(f_or_c==1)
 {
    printf("Enter the temperature in Fahrenheit to convert?\n");
    scanf("%d", &c);
    f = 1.8*c + 32.0;
    printf("Celsius of %d is %d degrees.\n");

 }
 if(f_or_c==2)
 {
    printf("Enter the temperature in Celsius to convert?\n");
    scanf("%d", &f);
    c = (f-32)*5/9;
    printf("Fahrenheit of %d is %d degrees.\n");
 }
 return 0;
 }
Arnestig
  • 2,285
  • 18
  • 30
user3723557
  • 11
  • 2
  • 6

1 Answers1

1

My guess is you just aren't printing the values out, but everything else looks pretty good.

printf("Fahrenheit of %d is %d degrees.\n");

You're not printing any variables.

This might work for you

printf("Fahrenheit of %d is %d degrees.\n", f, c); 

You can take a look at general usage of printf here http://www.cplusplus.com/reference/cstdio/printf/

Kyle Gobel
  • 5,530
  • 9
  • 45
  • 68
  • It does lead you to wonder what "incorrect output" was observed, though, doesn't it? When you're expecting 32 and it comes out 2037481959 (or whatever), that's often a clue that you're passing the wrong thing (or nothing at all) to printf. – David K Jul 01 '14 at 02:21
  • Thanks that did it! new at this, can't believe it was that simple. – user3723557 Jul 01 '14 at 02:25
  • Now I see the comment where the example of the output was given. Indeed it _was_ a pretty good clue. – David K Jul 01 '14 at 02:29