Fahrenheit to Celsius Conversion Code Help!

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 7925 Views - Last Post: 29 June 2009 - 05:03 PM Rate Topic: -----

#1 mralchamp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;
 }


Thanks for the help in advance!! Also, e-mail me at adl-94@hotmail.com
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  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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. :P

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

Was This Post Helpful? 0
  • +
  • -

#3 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 49
  • View blog
  • 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 :)
Was This Post Helpful? 0
  • +
  • -

#4 mralchamp  Icon User is offline

  • New D.I.C Head

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

Re: Fahrenheit to Celsius Conversion Code Help!

Posted 29 June 2009 - 09:30 AM

View PostSmok3dSalmon, 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. :P

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

Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 862
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 mralchamp  Icon User is offline

  • New D.I.C Head

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

Re: Fahrenheit to Celsius Conversion Code Help!

Posted 29 June 2009 - 10:00 AM

View PostImaSexy, 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..
Was This Post Helpful? 0
  • +
  • -

#7 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 49
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

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

Re: Fahrenheit to Celsius Conversion Code Help!

Posted 29 June 2009 - 10:13 AM

my bad
your factor variable should be this

double factor = 5.0/9.0;


should work now
Was This Post Helpful? 0
  • +
  • -

#9 mralchamp  Icon User is offline

  • New D.I.C Head

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

Re: Fahrenheit to Celsius Conversion Code Help!

Posted 29 June 2009 - 10:25 AM

View Postcomputerfox, 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 :)

View PostImaSexy, on 29 Jun, 2009 - 09:13 AM, said:

my bad
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 :D :^:
Was This Post Helpful? 0
  • +
  • -

#10 flabarm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#11 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 862
  • View blog
  • 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 ?
Was This Post Helpful? 0
  • +
  • -

#12 flabarm  Icon User is offline

  • New D.I.C Head

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

Re: Fahrenheit to Celsius Conversion Code Help!

Posted 29 June 2009 - 01:17 PM

View PostImaSexy, 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 :D
Was This Post Helpful? 0
  • +
  • -

#13 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 49
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#14 alm0614  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#15 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 49
  • View blog
  • 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.

hope that answers your question :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2