# newb question: probs with this program

• 04-18-2006
ajguerrero
newb question: probs with this program
my assignment is to write a program that asks for a temperature in fahrenheit, then spits out a value in celsius and kelvin. and ask again for a value in fahrenheit to convert. heres what ive got so far. by the way im using kernhigan and ritchie, with no experience in programming whatsoever. im finding that kernhigan and ritchie is not very good for me.

Code:

``` #iclude < stdio.h > #include <math.h > main() { float fahr, celsius, kelvin; int temp, cels, kelv temp = ' ' printf("Please enter (in fahr) a temp\n(q to quit)\n"); while (temp != 'q') { temp = getc ( stdin ); putchar (temp); cels = (temp-32.0) + (5.0/9.0); kelv = 273.5 + ((temp - 32.0) + (5.0/9.0)); printf("The temperature in Celsius is %6.1f\n" , cels); printf("The temperature in Kelvin is %6.1f\n" , kelv); } return = 0 }```

also, my next assignment is to use the program above (when written correctly) and rewrite it so that it uses one function to conver from fahrenheit to celsius and another to convert from celsius to kelivin. any help would be appreceated
• 04-18-2006
spydoor
If that's your actual code you need to take your time and look through it.
You have simple mistakes like misspelling include and forgetting semi colons.
Other than that it's close, you may run into a problem trying to use temp as both your loop control and fahr input.

a cleaned up version without the loop
Code:

```#include <stdio.h> int main() {     float fahr, celsius, kelvin;     printf("Please enter (in fahr) a temp\n");     scanf("%f", &fahr);     celsius = (fahr-32.0) + (5.0/9.0);     kelvin = 273.5 + ((fahr - 32.0) + (5.0/9.0));     printf("The temperature in Celsius is %6.1f\n" , celsius);     printf("The temperature in Kelvin is %6.1f\n" , kelvin);     return 0; }```
• 04-18-2006
00Sven
Code:

```#iclude < stdio.h >    /*Include spelled wrong*/ #include <math.h > main()    /*main returns an int  int main()*/ { float fahr, celsius, kelvin;    /*These aren't used*/ int temp, cels, kelv      /*Make these floats  Integers can't have decimals*/ /*Missing ;*/ temp = ' '    /*This should be a number not a character*/ /*missing ;*/ printf("Please enter (in fahr) a temp\n(q to quit)\n"); while (temp != 'q') {  /*Temp is a float and will never equal q so just do while(1)*/ temp = getc ( stdin );    /*You can use scanf(" %d",&temp);*/ putchar (temp);    /*I'm assuming that this is for checking and can be removed*/ cels = (temp-32.0) + (5.0/9.0);    /*You do not need the .0 at the end of all numbers*/ kelv = 273.5 + ((temp - 32.0) + (5.0/9.0));    /*same thing as previous*/ printf("The temperature in Celsius is %6.1f\n" , cels); printf("The temperature in Kelvin is %6.1f\n" , kelv); } return = 0 /*return 0;*/ }```
• 04-18-2006
TCB
Nice post 00Sven...

The other thing I didn't understand in the original post is why the <math.h> file was needed. I didn't see any calculations or math functions that were out of the ordinary.

And I took the use of decimals in the calculations as an attempt to cast the values (which were declared as 'int') to a decimal type?

TB
• 04-18-2006
dude543
[QUOTE]
im finding that kernhigan and ritchie is not very good for me
[\QUOTE]
I must agree with you. Try to find some beginer book.
• 04-19-2006
00Sven
I thought that that was what it was for too but that does not make a variable declared as an integer become a float. And if they are declared as floats then there is no need to have all of the ".0"s on the end of numbers.
On math.h, I have seen a ton of beginners include this whenever doing any kind of mathematical calulation, which you don't.
~Sven