Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,114 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,766 people online right now. Registration is fast and FREE... Join Now!




Converision Between Celsius and Fahrenheit

 
Reply to this topicStart new topic

Converision Between Celsius and Fahrenheit, Why do I get the wrong answer

Guydin
5 Jun, 2007 - 04:47 PM
Post #1

New D.I.C Head
*

Joined: 5 Jun, 2007
Posts: 9


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

Here is my code:

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.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Converision Between Celsius And Fahrenheit
5 Jun, 2007 - 05:33 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Guydin
RE: Converision Between Celsius And Fahrenheit
5 Jun, 2007 - 05:49 PM
Post #3

New D.I.C Head
*

Joined: 5 Jun, 2007
Posts: 9


My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Guydin
RE: Converision Between Celsius And Fahrenheit
5 Jun, 2007 - 06:21 PM
Post #4

New D.I.C Head
*

Joined: 5 Jun, 2007
Posts: 9


My Contributions
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:

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.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Converision Between Celsius And Fahrenheit
5 Jun, 2007 - 07:29 PM
Post #5

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
CODE
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.
User is offlineProfile CardPM
+Quote Post

Guydin
RE: Converision Between Celsius And Fahrenheit
5 Jun, 2007 - 08:18 PM
Post #6

New D.I.C Head
*

Joined: 5 Jun, 2007
Posts: 9


My Contributions
Wow...

That is somewhat strange and certainly good to know. Thanks!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:46PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month