# Fahrenheit to Celsius Conversion Code Help!

• (2 Pages)
• 1
• 2

## 15 Replies - 7925 Views - Last Post: 29 June 2009 - 05:03 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=112328&amp;s=487e5578fb6bfdf7d5d6326bf90ea6f7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mralchamp

Reputation: 0
• Posts: 5
• Joined: 29-June 09

# Fahrenheit to Celsius Conversion Code Help!

Posted 29 June 2009 - 08:19 AM

Hello
I need to say that I am new to c++. i bought the "C++ for Dummies" book, and it came with a CodeBlock CD inside. Once i finished my first program (a celsius to fahrenheit converter) i thought that i could do a fahrenheit to celsius converter by just changing the equations and factors, and all the fahrenheit words to celsius, and vise-versa. After correcting a few errors, i was finally able to run it. But, once i entered a temperature in Fahrenheit, the Celsius was said to be 0 every time. Please help me.
This is the code Ive got so far:
```//
// Conversion - Program to convert temperature from
//			Fahrenheit degrees into Celsius:
//			Celsius = 5.0/9.0 * (Fahrenheit - 32.0)
//
#include <cstdio>

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

int main (int nNumberofArgs, char* pszArgs[])
{
// enter the temperature in Fahrenheit
int fahrenheit;
cout << "Enter the temperature in Fahrenheit:";
cin >> fahrenheit;

// calculate conversion factor for Fahrenheit
// to Celsius
int factor;
factor = 5.0 / 9.0;

// use conversion factor to convert Fahrenheit
// into Celsius values
int celsius;
celsius = factor * (fahrenheit - 32.0);

// output the results (followed by a NewLine)
cout << "Celsius value is:";
cout << celsius << endl;

// wait until user is ready before terminating program
// to allow the user to see the program results
system("PAUSE");
return 0;
}

```

You will find it to be easier to contact me through there.

This post has been edited by mralchamp: 29 June 2009 - 10:07 AM

Is This A Good Question/Topic? 0

## Replies To: Fahrenheit to Celsius Conversion Code Help!

### #2 Smok3dSalmon

Reputation: 0
• Posts: 3
• Joined: 29-June 09

## Re: Fahrenheit to Celsius Conversion Code Help!

Posted 29 June 2009 - 08:35 AM

Your factor is of datatype "integer," it is whole numbers only with no decimals. The 5/9 is being truncated and is stored as a 0 in the variable factor.

Factor should be a double, as well as all the other temperatures, most celsius to fahrenheit result in a decimal.
int fahrenheit; should be double fahrenheit;
int celsius; should be double celsius;
int factor; should be int factor;

Any variable that holds decimals should be a double... or a float... but I don't like floats.

I would also use the setprecision command so that the degrees would be read out to two decimal places.
cout << setprecision(3) << celsuis << endl;

Suggestion:
You can define variables as you declare them.
double factor = 5.0/9.0;

This post has been edited by Smok3dSalmon: 29 June 2009 - 08:39 AM

### #3 computerfox

• straight vegetarian kid

Reputation: 49
• Posts: 3,772
• Joined: 29-January 09

## Re: Fahrenheit to Celsius Conversion Code Help!

Posted 29 June 2009 - 09:16 AM

first off, edit your post using [code ] and [/code ]

second off, i hope this helps

### #4 mralchamp

Reputation: 0
• Posts: 5
• Joined: 29-June 09

## Re: Fahrenheit to Celsius Conversion Code Help!

Posted 29 June 2009 - 09:30 AM

Smok3dSalmon, on 29 Jun, 2009 - 07:35 AM, said:

Your factor is of datatype "integer," it is whole numbers only with no decimals. The 5/9 is being truncated and is stored as a 0 in the variable factor.

Factor should be a double, as well as all the other temperatures, most celsius to fahrenheit result in a decimal.
int fahrenheit; should be double fahrenheit;
int celsius; should be double celsius;
int factor; should be int factor;

Any variable that holds decimals should be a double... or a float... but I don't like floats.

I would also use the setprecision command so that the degrees would be read out to two decimal places.
cout << setprecision(3) << celsuis << endl;

Suggestion:
You can define variables as you declare them.
double factor = 5.0/9.0;

Where should i put the setprecision command? (sorry, obviously im a newcommer to this, but thanks for the help.)

This post has been edited by mralchamp: 29 June 2009 - 09:32 AM

### #5 jjl

• Engineer

Reputation: 862
• Posts: 3,982
• Joined: 09-June 09

## Re: Fahrenheit to Celsius Conversion Code Help!

Posted 29 June 2009 - 09:36 AM

Welcome to DiC, make sure to use code tags /,

first of all you dont do all of those headers for this simple program,
look at the differences:
```
#include <iostream>
using namespace std;

int main()
{
int fahr; // try to use short camel-case variable names
cout<<"Enter Fahrenheit :";
cin>>fahr;

//You need a double here because an int does not hold decimal values
double factor = 5/9;

double cel = factor * (fahr - 32.0); // double the value for decimal points

//if you want to round up to a hole number you can use this equation
// ceil() & floor() have problem rounding when less than 1;
double x = .5;
if(cel<0)
x = -.5;
int temp = static_cast<int>(cel+x);

cout<<"Temperature : "<<temp<<endl;
cin.get(); // dont use system pause() its very resource heavy
return 0;
}

```

hope that helps

****EDIT*****

@Smok3dSalmon
if your going to tell someone to use a function you should at least tell them what header to include,

include
```#include <iomanip>

```

like this
```
double temperature = 30.55454; // just to show you how it works

cout<<setprecision(4)<<temperature<<endl;

```

this sets the value to 4 digits which will work as long as
the celcius is under 100; otherwise were probably all dead

This post has been edited by ImaSexy: 29 June 2009 - 09:43 AM

### #6 mralchamp

Reputation: 0
• Posts: 5
• Joined: 29-June 09

## Re: Fahrenheit to Celsius Conversion Code Help!

Posted 29 June 2009 - 10:00 AM

ImaSexy, on 29 Jun, 2009 - 08:36 AM, said:

Welcome to DiC, make sure to use code tags /,

first of all you dont do all of those headers for this simple program,
look at the differences:
```
#include <iostream>
using namespace std;

int main()
{
int fahr; // try to use short camel-case variable names
cout<<"Enter Fahrenheit :";
cin>>fahr;

//You need a double here because an int does not hold decimal values
double factor = 5/9;

double cel = factor * (fahr - 32.0); // double the value for decimal points

//if you want to round up to a hole number you can use this equation
// ceil() & floor() have problem rounding when less than 1;
double x = .5;
if(cel<0)
x = -.5;
int temp = static_cast<int>(cel+x);

cout<<"Temperature : "<<temp<<endl;
cin.get(); // dont use system pause() its very resource heavy
return 0;
}

```

hope that helps

****EDIT*****

@Smok3dSalmon
if your going to tell someone to use a function you should at least tell them what header to include,

include
```#include <iomanip>

```

like this
```
double temperature = 30.55454; // just to show you how it works

cout<<setprecision(4)<<temperature<<endl;

```

this sets the value to 4 digits which will work as long as
the celcius is under 100; otherwise were probably all dead

after what you both added, i got this:

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

int main()

{
int fahr;
cout << "Enter Fahrenheit:";
cin >> fahr;

double factor = 5/9;

double cel = factor * (fahr - 32.0);

cout<<"Celsius : "<<cel<<endl;
cin.get();
return 0;
}

```

and yet, i still get the answer as 0. after it says "Celsius: 0", it has an equation of "process returned as 0 <0x0>"
continue helping!!!i think we are getting there..

### #7 computerfox

• straight vegetarian kid

Reputation: 49
• Posts: 3,772
• Joined: 29-January 09

## Re: Fahrenheit to Celsius Conversion Code Help!

Posted 29 June 2009 - 10:12 AM

did you try my code by any chance?

### #8 jjl

• Engineer

Reputation: 862
• Posts: 3,982
• Joined: 09-June 09

## Re: Fahrenheit to Celsius Conversion Code Help!

Posted 29 June 2009 - 10:13 AM

your factor variable should be this

```double factor = 5.0/9.0;

```

should work now

### #9 mralchamp

Reputation: 0
• Posts: 5
• Joined: 29-June 09

## Re: Fahrenheit to Celsius Conversion Code Help!

Posted 29 June 2009 - 10:25 AM

computerfox, on 29 Jun, 2009 - 09:12 AM, said:

did you try my code by any chance?

yes i did...i had 7 errors, and it was completely diferent from what i did. i didnt want to copy/paste, because i wanted to try to make my own. but thanks for trying

ImaSexy, on 29 Jun, 2009 - 09:13 AM, said:

your factor variable should be this

```double factor = 5.0/9.0;

```

should work now

ahhahahah yes!!!! it works perfectly. im so happy now...this has made my day...an accomplishment...thank you so much

### #10 flabarm

Reputation: 0
• Posts: 19
• Joined: 29-June 09

## Re: Fahrenheit to Celsius Conversion Code Help!

Posted 29 June 2009 - 11:52 AM

I noticed you already figured out the answer to your problem, but I figured I would post a temp converter I recently did in class just in case anyone else has any questions.
```#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int number;
int celcius;
int fahrenheit;
cout<<"Enter 1 if you would like to convert Celcius to Farenheit"<<endl;
cout<<"Enter 2 if you would like to convert Farenheit to Celcius"<<endl;
cin>>number;

switch (number)
{

case 1:
cout<<"You have selected Celcius to Fahrenheit"<<endl;
cout<<"Enter Temperature in Celcius"<<endl;
cin>>celcius;
fahrenheit = 1.8 * celcius + 32;
cout << "Temperature in Fahrenheit is   " << fahrenheit<<" Degrees" << endl;
break;
case 2:
cout<<"You have selected Farenheit to Celcius"<<endl;
cout<<"Enter Temperature in Fahrenheit"<<endl;
cin>>fahrenheit;
celcius = .56 * (fahrenheit - 32.0);
cout<<"Temperature in Celcius is  "<<celcius<<" Degrees"<<endl;

break;
default:
cout<<"Invalid Input"<<endl;
break;
}

system("PAUSE");
return EXIT_SUCCESS;
}

```

### #11 jjl

• Engineer

Reputation: 862
• Posts: 3,982
• Joined: 09-June 09

## Re: Fahrenheit to Celsius Conversion Code Help!

Posted 29 June 2009 - 12:48 PM

pretty good, only suggestions would be double your variables then set precision so you could have a more exact answer ( but who reads temperature to a decimal value anyways) lol , i would try and stay away from system () commands as much as possible, cin.get() works like a champ, but what is the reason for return EXIT_SUCCESS ?

### #12 flabarm

Reputation: 0
• Posts: 19
• Joined: 29-June 09

## Re: Fahrenheit to Celsius Conversion Code Help!

Posted 29 June 2009 - 01:17 PM

ImaSexy, on 29 Jun, 2009 - 11:48 AM, said:

pretty good, only suggestions would be double your variables then set precision so you could have a more exact answer ( but who reads temperature to a decimal value anyways) lol , i would try and stay away from system () commands as much as possible, cin.get() works like a champ, but what is the reason for return EXIT_SUCCESS ?

I'm not sure about EXIT_SUCCESS. It is always there when I start dev-c++ so I don't mess with it. Our instructor asked for no decimals so i got off easy

### #13 computerfox

• straight vegetarian kid

Reputation: 49
• Posts: 3,772
• Joined: 29-January 09

## Re: Fahrenheit to Celsius Conversion Code Help!

Posted 29 June 2009 - 01:21 PM

you should change that to cin.get(). it would be more efficient since it doesn't really have to go into the System. with cin.get(), it will hold the window open, then when you press enter, that's when it will close.

### #14 alm0614

Reputation: 1
• Posts: 85
• Joined: 23-June 09

## Re: Fahrenheit to Celsius Conversion Code Help!

Posted 29 June 2009 - 01:24 PM

what is the purpose of the system("PAUSE") and why woud cin.get help??

what does cin.get do that just
``` cin >>
```
doesnt do

### #15 computerfox

• straight vegetarian kid

Reputation: 49
• Posts: 3,772
• Joined: 29-January 09

## Re: Fahrenheit to Celsius Conversion Code Help!

Posted 29 June 2009 - 01:35 PM

first off using just cin>> is invalid. it will not compile
second system("PAUSE") allows dev c++ to stay open because if you take that out, it will execute, run through, then close the window and you don't want that.
third, cin.get() takes the place of system("PAUSE"), but since it uses iostream, it doesn't have to go into the system, which makes it more efficient.