5 Replies - 2582 Views - Last Post: 05 June 2007 - 09:18 PM Rate Topic: -----

#1 Guydin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-June 07

Converision Between Celsius and Fahrenheit

Post icon  Posted 05 June 2007 - 05:47 PM

Ok, this isn't really related to the topic, but I didn't want to start a new thread.

Here is my code:

//celsius.cpp
//demonstrates cin, newline
//a personal experiment, not a part of a book
//converts from celsius to fahrenheit

#include <iostream>
#include <conio.h>
using namespace std;

int main ()
	{
		int ctemp;		//for temperature in celsius

		cout << "Enter temperature in celsius: ";
		cin >> ctemp;
		int ftemp = 32 + ((9 / 5) * ctemp);
		cout << "Equivalent in fahrenheit is: " << ftemp << '\n';
		getch();
		return 0;
	}


Ok, when I run this program and I input 100 I should get 212, but instead I get 132. I'm guessing that for some reason the program is just adding 32 to ctemp and ignoring the (9 / 5) * part for some reason. Would anyone mind explaining that reason to me?

Thanks!

Note: When I write the equation like this: ctemp * 9 / 5 + 32 it works perfectly. But I wrote it a bunch of ways, without any parentheses and with, and that is the only way that worked. I'm really curious as to why.

Is This A Good Question/Topic? 0
  • +

Replies To: Converision Between Celsius and Fahrenheit

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: Converision Between Celsius and Fahrenheit

Posted 05 June 2007 - 06:33 PM

Sorry I started a new topic for you anway. Cuz I am mean like that. (Actually because it really is a new topic and this makes it easier for other user to find -- and it is of interest by itself).

Ok. I belive your problem is that (9/5) == 1 (when dealing with integers). Integers can not be fractions of a whole. So they truncate the number to just the whole part. There are two solutions. First of all you can do your math in a floating point format (like double or float):

32 + (double)(9/5)*ctemp;

OR you can just rearrange how you do things:

32 + 9 * ctemp / 5; as 900/5 = 180 so 180 + 32 = 212
ctemp * 9 / 5 + 32;

C will do * before / so ctemp * 9 happens first, it will do / over + so 900/5 happens next.

When working with division of integers in C/C++ you really need to think about the order of operation as you don't want your program to truncate the wrong part.

Operator Precedence Chart - read left to right, top to bottom. I.e () has higher precedence than [] which is higher than * which is higher than & and + etc etc.
Was This Post Helpful? 0
  • +
  • -

#3 Guydin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-June 07

Re: Converision Between Celsius and Fahrenheit

Posted 05 June 2007 - 06:49 PM

Oh, now that is interesting.

I tried doing it like so: ctemp * (12/5) + 32 where ctemp equals 100 and got the answer 232. So apparently it completely ignores anything that isn't a whole number. It doesn't round it, it doesn't figure it into the equation in any way. But it does keep the part of the answer that is a whole number and uses that. Thank you for the response.
Was This Post Helpful? 0
  • +
  • -

#4 Guydin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-June 07

Re: Converision Between Celsius and Fahrenheit

Posted 05 June 2007 - 07:21 PM

Ok, I think I'm running into some kind of syntax problem or something. Whenever I try it with double or float and force it to divide 9 by 5 I get the wrong answer. Here is my code:

//celsius.cpp
//demonstrates cin, newline
//a personal experiment, not a part of a book
//converts from celsius to fahrenheit

#include <iostream>
#include <conio.h>
using namespace std;

int main ()
	{
		double ctemp;		//for temperature in celsius

		cout << "Enter temperature in celsius: ";
		cin >> ctemp;
		double ftemp = (9 / 5) * ctemp + 32;
		cout << "Equivalent in fahrenheit is: " << ftemp << '\n';
		getch();
		return 0;
	}


Maybe I should stop experimenting so much, hehe.
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: Converision Between Celsius and Fahrenheit

Posted 05 June 2007 - 08:29 PM

Maybe I should stop experimenting so much, hehe.
NO WAY. The best way to learn programing (and I swear by this) is to play. Experiment.


This one caught me off guard. the reason you get the wrong answer is the same as above: Integer division. In this case the compiler say 9 and 5 as integer literals. Therefore it did the 9/5 as integer division. To force it to do the right division write it as:
ftemp = 9.0 / 5.0 * ctemp + 32;

the decimal point lets the compiler know that you are using floating point values. ( you could just write 9. / 5. but the zeros help to avoid other errors where the compiler might try to connect the . with another symbol).

thats the kind of bug that will get you in trouble. I probably would have coded something like that and then spend forever trying to figure out why it didn't work. -- I would SWEAR that I was right.
Was This Post Helpful? 0
  • +
  • -

#6 Guydin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-June 07

Re: Converision Between Celsius and Fahrenheit

Posted 05 June 2007 - 09:18 PM

Wow...

That is somewhat strange and certainly good to know. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1