Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

SkippyThorson

macrumors 68000
Original poster
Jul 22, 2007
1,705
1,010
Utica, NY
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.

int main(void)
{
// state variables
int F;
double C;

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

// convert the temperature to celsius
C = ((F - 32) /9) * 5;

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

return 0;
}

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!
 
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...
 
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.
 
Alright, well I think I'm starting to get the hang of it. You have no idea how thankful I am for your help.

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
C = ((F - 32) / 9) * 5;

// 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;
}

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. :D

[Session started at 2010-02-04 14:45:04 -0500.]
Welcome.
Enter a temperature in degrees Fahrenheit: 54
Below is the conversion from Fahrenheit to Celsius.
Degrees F: 54.000000.
Degrees C: 12.222222.
The Debugger has exited with status 0.
 
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;
}

This will help you avoid major headaches in the future!

-S!
 
You should be more explicit with your math:

Code:
// 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);

This will help you avoid major headaches in the future!

-S!

This makes sense, but I'm still trying to wrap my head around the application of the rules. :eek: 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.
 
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.

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
 
You should be more explicit with your math:

Code:
// And don't forget to then "cast" that math to (double) before saving into C.
C = (double)(((F - 32.0f) / 9.0f) * 5.0f);

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..?"

I see lee1210 (always very fast in here :) ) has already answered your other questions...
 
Every user in this thread has been a great help. :) I can unfortunately guess that my woes don't stop here, but at least I learned a lot today. Thank you all very much.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.