# Fahrenheit to Celsius conversion program

Page 1 of 1

## 12 Replies - 14887 Views - Last Post: 23 January 2010 - 11:01 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=74639&amp;s=6c716cd53309c669b33076a084da700a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 scimmy55

Reputation: 1
• Posts: 5
• Joined: 02-December 08

# Fahrenheit to Celsius conversion program

Posted 02 December 2008 - 12:07 PM

I am trying to create a conversion program that converts Fahrenheit to Celsius.
This is what I came up with so far:

```/*Conversion Program*/
#include <iostream>
#include <string>
using namespace std;
int main()
{
double ftemp;
double ctemp;
cout<<"-Converting Fahrenheit to Celsius-"<<endl;
cout<<"Enter temperature in Fahrenheit to convert to Celcius: ";
cin>>ftemp;
ctemp=(ftemp-32)*5/9;
cout<<"The temperature in Celsius is: "<<ctemp<<endl;
return (0);
}
```

Is This A Good Question/Topic? 1

## Replies To: Fahrenheit to Celsius conversion program

### #2 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Fahrenheit to Celsius conversion program

Posted 02 December 2008 - 12:12 PM

Problems, concerns, errors...?

If it isn't correct its probably because of order of operations:

Chane
ctemp=(ftemp-32)*5/9;

to

ctemp=(ftemp-32)*(5/9);

### #3 scimmy55

Reputation: 1
• Posts: 5
• Joined: 02-December 08

## Re: Fahrenheit to Celsius conversion program

Posted 02 December 2008 - 03:26 PM

Okay thank you, I can't start the program on the computer because i am not at home where my visual studio is. So I'm running all of this down on paper.

*The only question that I had: Must I include #include <string>, or am I way off?

I appreciate you time! Thank you much!

This post has been edited by scimmy55: 02 December 2008 - 03:27 PM

### #4 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Fahrenheit to Celsius conversion program

Posted 02 December 2008 - 03:55 PM

No, unless you plan on using strings somehow. all you need is <iostream> for cin, cout, etc... and using namespace std;

### #5 scimmy55

Reputation: 1
• Posts: 5
• Joined: 02-December 08

## Re: Fahrenheit to Celsius conversion program

Posted 02 December 2008 - 04:00 PM

Okay, I never really understood how to use strings anyway! Haha.
Thanks!

### #6 elcarn_23

Reputation: 0
• Posts: 6
• Joined: 21-January 10

## Re: Fahrenheit to Celsius conversion program

Posted 23 January 2010 - 03:48 PM

I've been working on this Converter program for a class and this is what I was able to come up with after 2 hours. I'm just learning about operators and it took me a while to figure out how to order the equation. My teacher wanted it to output an int so that is why I did the static_cast to the final output. It passed all but the 10th test and that was to convert 0 degrees fahrenheit to celsius. My answer came up -17 because the actual math returns -17.7777 but his answer says it should be - 18. I was wondering if anyone could point me in the right direction to figuring this out.

```#include <iostream>
using namespace std;

int main()
{

int fahr;

cout << "Please enter Fahrenheit degrees: ";
cin >> fahr;

double celsius = (5.0 * (fahr - 32.0)) / 9.0;

cout << "Celsius: " << static_cast<int>(celsius) << endl;

return 0;

}

```

### #7 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

## Re: Fahrenheit to Celsius conversion program

Posted 23 January 2010 - 04:27 PM

did you just hijack this thread? lol i would start a new one for better answers. But it seems you have a rounding problem.

try to implement a function like this
```int round(double number)
{
if(number<0)
number-=0.5;
else
number+=0.5;
return (int)number;
}

```

### #8 Bartuc

Reputation: 1
• Posts: 20
• Joined: 19-April 09

## Re: Fahrenheit to Celsius conversion program

Posted 23 January 2010 - 04:29 PM

It will automatically truncate the answer, not rounding it correctly. Usually you can just add .5 to round it (5.6 + .5 = 6 truncated, and 5.1 + .5 = 5 truncated). This may not be the easiest way so correct me if I am wrong, but you could try
```#include <iostream>
using namespace std;

int main()
{

int fahr;

cout << "Please enter Fahrenheit degrees: ";
cin >> fahr;

double celsius = (5.0 * (fahr - 32.0)) / 9.0;
if(celsius<0)
celsius-=.5;
else
celsius+=.5;
cout << "Celsius: " << static_cast<int>(celsius) << endl;

return 0;

}

```

I got an output of -18c for 0f. I tested it with 32f and got 0 c so I think it is working correctly.

### #9 diedev

Reputation: 0
• Posts: 8
• Joined: 22-January 10

## Re: Fahrenheit to Celsius conversion program

Posted 23 January 2010 - 04:46 PM

Try with <cmath> using floor procedure.

```floor( 7.5 ) --> 7
floor( -7.5 ) --> -8
```

### #10 cgseif323

Reputation: 0
• Posts: 27
• Joined: 01-April 09

## Re: Fahrenheit to Celsius conversion program

Posted 23 January 2010 - 04:47 PM

I made a program like this a long long time ago but it does the opposite: Celsius to Fahrenheit
Here it is:

```// SeifConvert.cpp : main project file.
// Celsius to Fahrenheit

#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main()
{
int nFactor;
int nFahrenheit;
{
loop:
cout<<("Enter the temperature in Celsius:");
cin>>nFactor;
{
nFahrenheit = nFactor * 1.8 + 32;
}
cout << "Fahrenheit temperature is:";
cout <<	nFahrenheit << endl;
goto loop;
}

}

```

It doesn't do decimal values, though.

This post has been edited by cgseif323: 23 January 2010 - 04:48 PM

### #11 Bartuc

Reputation: 1
• Posts: 20
• Joined: 19-April 09

## Re: Fahrenheit to Celsius conversion program

Posted 23 January 2010 - 04:58 PM

diedev, on 23 Jan, 2010 - 03:46 PM, said:

Try with <cmath> using floor procedure.

```floor( 7.5 ) --> 7
floor( -7.5 ) --> -8
```

The only bad thing about that is if the number is 7.999 it will round to 7 rather than 8. Floor rounds down and ceil rounds up every time.

### #12 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

## Re: Fahrenheit to Celsius conversion program

Posted 23 January 2010 - 05:49 PM

@diedev ciel and floor do not round right liek Bartuc mentioned

@cgseif323, avoid using goto statments, its bad programming practice

### #13 elcarn_23

Reputation: 0
• Posts: 6
• Joined: 21-January 10

## Re: Fahrenheit to Celsius conversion program

Posted 23 January 2010 - 11:01 PM

Thanks to everyone and their comments. I went and studied up on the rounding functions and I've now learned a bit about if else statements. Sorry I didn't mean to hijack the thread lol I meant to start one but thanks again for contributing my program is now running great and accurately converting.