Fahrenheit to Celsius conversion program

Simple conversion program that I could use some advice.

Page 1 of 1

12 Replies - 13452 Views - Last Post: 23 January 2010 - 11:01 PM Rate Topic: -----

#1 scimmy55  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • 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);
Was This Post Helpful? 1
  • +
  • -

#3 scimmy55  Icon User is offline

  • New D.I.C Head

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

Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • 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;
Was This Post Helpful? 0
  • +
  • -

#5 scimmy55  Icon User is offline

  • New D.I.C Head

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

#6 elcarn_23  Icon User is offline

  • New D.I.C Head

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

}



Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1076
  • View blog
  • Posts: 4,536
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#8 Bartuc  Icon User is offline

  • New D.I.C Head

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

#9 diedev  Icon User is offline

  • New D.I.C Head

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

Was This Post Helpful? 0
  • +
  • -

#10 cgseif323  Icon User is offline

  • New D.I.C Head

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

Was This Post Helpful? 0
  • +
  • -

#11 Bartuc  Icon User is offline

  • New D.I.C Head

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

Re: Fahrenheit to Celsius conversion program

Posted 23 January 2010 - 04:58 PM

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

#12 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1076
  • View blog
  • Posts: 4,536
  • 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
Was This Post Helpful? 0
  • +
  • -

#13 elcarn_23  Icon User is offline

  • New D.I.C Head

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

Page 1 of 1