# Temperature Conversion Issues

Discussion in 'Mac Programming' started by SkippyThorson, Feb 4, 2010.

1. ### SkippyThorson macrumors 65816

Joined:
Jul 22, 2007
Location:
Utica, NY
#1
Ok, so I am literally brand new to writing code. -- Never wrote a line in my life. I'm now in a Computing Fundamentals class at my university - I decided to throw myself in to it. So anyhow, without my blubbering, I'll get to my point.

I have this program a buddy and I wrote last night. We had it working at a time, but changed some stuff here and there. I'm assuming the conversion is simply written wrong, but maybe I'm just totally wrong.

Any ideas on what's going on here? Anything at all is greatly appreciated. I'm so glad this community exists. I'm probably going to have weekly issues in this class, but it's something I know I wanna try.

Any temp. entered 40 or below returns a Celsius of 0. Anything above returns 1075052544 or some such number. I'm stumped. -- Thanks to anyone who at least read this!

2. ### robbieduncan Moderator emeritus

Joined:
Jul 24, 2002
Location:
London
#2
F is an int. As is 32 and 9. So (F-32) will be calculated as an int. As will ((F-32)/9). So this will be integer division: the remainder is discarded...

3. ### ritsard macrumors regular

Joined:
Jun 18, 2009
Location:
SF Bay Area, CA
#3
Also, your printf needs to correctly reflect the type you are presenting. You have declared C as a double but you are printing it like an int. You may want to consider using a float instead and use %f in the printf statement.

4. ### SkippyThorson thread starter macrumors 65816

Joined:
Jul 22, 2007
Location:
Utica, NY
#4
Alright, well I think I'm starting to get the hang of it. You have no idea how thankful I am for your help.

Seems like it works. Checked the outcomes of a few numbers on ConvertBot, and they line up. If I do say so myself, I think I understand "beautiful code", in another sense. It's like one of those movies where a ray of sun picks you out of a crowd. My correct answers produced the sound of angels, to put it lightly.

5. ### sfurlani macrumors newbie

Joined:
Feb 4, 2010
#5
You should be more explicit with your math:

Code:
```int main(void)
{
// state variables
float F;
double C;

// input - temperature F
printf("Welcome.\n");
printf("Enter a temperature in degrees Fahrenheit: ", F);
scanf("%f" ,&F);

// convert the temperature to celsius

// Be more explicit about what numbers you are using.
// 5 = no decimal, int
// 5.0 = defaults to double (usually)
// 5.0f = float
// And don't forget to then "cast" that math to (double) before saving into C.
C = (double)(((F - 32.0f) / 9.0f) * 5.0f);

// output - temperature F and C
printf("Below is the conversion from Fahrenheit to Celsius.\n");
printf("Degrees F: %f.\n", F);
printf("Degrees C: %f.", C);

return 0;
}```

-S!

6. ### SkippyThorson thread starter macrumors 65816

Joined:
Jul 22, 2007
Location:
Utica, NY
#6
This makes sense, but I'm still trying to wrap my head around the application of the rules. I learn best by doing, not reading - so my class book is of very little use. I think I just learned more from the responses here than I have in 2 weeks in my class.

I want to see if I have this straight:
int = 3: An int is only a number, without decimal, positive or negative. (I've got that one down.)
double = 3.0: A number with a decimal will default to double. (Can I ask why that is?)
float = 3.0f: A number with a decimal and f goes to float. (It is always f, correct?)

Like I said, I am brand new, I'm sorry if this is redundant and elementary.

7. ### lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#7
int, short, and char are all signed integer values when declared "bare" like this. You can use the "unsigned" keyword to get an unsigned value.

decimal literals default to double because double is almost always the right answer for storing floating point numbers. the precision of float is insufficient for nearly any sort of floating point computation.

adding the f will get you a float constant. You always use f if you want one of these, but the good news is you hardly ever do, so using the double version is easier and generally the right thing to do.

-Lee

8. ### Sander macrumors 6502

Joined:
Apr 24, 2008
#8
I'm not sure I agree with this. This is an example of what I call the "And suddenly... nothing happened" anti-pattern. To an experienced reader, this line sticks out and will raise all kinds of questions. "Why the extra zero at the end..? Is there something special with significant digits..? Hmm, no, they get thrown away anyway... Why the "float" suffix? Is he purposely trying to avoid calculations with doubles..? Perhaps he intends to run this on an embedded CPU which only has support for floats? Hmm, no, he's specifically casting to double. Why is he doing that, when the assignment to C would already do the conversion..?"