![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Feb 2005
Posts: 47
Rep Power: 0
![]() |
Converting Temperatures
I'm using the following code to convert from Celcius to Fahrenheit but for some reason the actual conversion wont work and I can't work out why. The key lines are lines 13 and 16.
Basically, the first line tries fahrenheit = ((9 / 5) * celcius) + 32; however it's not performing the multiplication, it just adds 32 to celcius. I've tried changing it to 1.8 * celcius too but it still doesn't perform the multiplication. If anyone can figure out why then I'd be grateful. Could it just be a problem with my machine? I'm using an Intel Mac running Leopard. Thanks. c Syntax (Toggle Plain Text)
|
|
|
|
|
|
#2 |
|
Hobby Coder
Join Date: May 2006
Posts: 58
Rep Power: 0
![]() |
Re: Converting Temperatures
Your program is working, but integer 9 divided by integer 5 = 1, (the remainder is truncated), so it's not what you want.
Try (9.0 / 5.0) and see if your compiler won't accept them as floats, that way. Otherwise you may need to explicitly cast them to floats first, and then divide. The second line of code works, because when 9 is multiplied by celcius, the 9 is being automatically re-cast to a float, since celcius is a float, already. |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Feb 2005
Posts: 47
Rep Power: 0
![]() |
Re: Converting Temperatures
Yes! Using 9.0 and 5.0 works!
Thanks a lot! |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,254
Rep Power: 5
![]() |
Re: Converting Temperatures
A conversion/cast is not required. Simply use 9.0f/5.0f (or 1.80f) to have literals that are of type float.
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 198
Rep Power: 1
![]() |
Re: Converting Temperatures
Cool
__________________
Be kinder than necessary because everyone you meet is fighting some kind of battle. |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 198
Rep Power: 1
![]() |
Re: Converting Temperatures
A bit off topic, but you could use a function to do the work
in the program if you want. Identify the formula, in this case it could be: Farenheit = Celcius X 1.8 + 32. (I think) Your function could look like this... float convert( float a )
{
float temp_farenheit = ( (temp_celcius * 1.8) + 32 );
return (0);
}Let the user enter the temp in Celcius, then pass that float value as an argument to the function above, then display the 'converted' result on - screen...( %.2f will display to two decimal places, %.3f to three, etc.)![]() printf("Temperature in Celcius = %.2f ", temp_celcius
printf("Temperature in Farenheit = %.2f ", convert( temp_celcius );>BstrucT
__________________
Be kinder than necessary because everyone you meet is fighting some kind of battle. |
|
|
|
|
|
#7 | |
|
Hobbyist Programmer
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 198
Rep Power: 1
![]() |
Re: Converting Temperatures
Quote:
, itshould obviously return temp_farenheit in above example.My apologies.
__________________
Be kinder than necessary because everyone you meet is fighting some kind of battle. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Excell and MySQL - converting data | MiKuS | Other Web Development Languages | 0 | May 4th, 2008 12:32 AM |
| Help in Converting C# to Java | bearvsgodzila | Java | 1 | Jan 13th, 2008 12:36 PM |
| converting double to string | dark_omen | Java | 3 | Dec 8th, 2005 4:11 PM |
| Converting javascript to Visual Basic.NET | emdiesse | Visual Basic .NET | 4 | Nov 21st, 2005 9:52 PM |
| Converting ASM to 86000 | Brom02 | Assembly | 0 | Apr 28th, 2005 1:48 AM |