# Program about Farenheit/Celisus (off by 2)

Page 1 of 1

## 6 Replies - 467 Views - Last Post: 20 March 2013 - 03:19 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=316125&amp;s=c1d119811e460337b3598e78bc77cc46&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bytemyzetta

• New D.I.C Head

Reputation: 0
• Posts: 9
• Joined: 20-March 13

# Program about Farenheit/Celisus (off by 2)

Posted 20 March 2013 - 01:25 PM

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

int main()
{  int tempF;
int tempC = 100;

do
{
for (tempF = 0; tempF != tempC; tempC--)
{

tempF = (9 * tempC) / 5;
tempF = tempF + 32;
cout << tempF;
cout << endl;

}

cout << endl;
cout << endl;
cout << "Would you like to see the calculation again?\n";
}  while(answer == 'y' || answer == 'Y');
return 0;
}
```

Okay, I basically have to make a program that finds the Fahrenheit temp that equals the same in Celsius, so -40. My program stops at -43. We have to use type int and when I changed 32 to 30, it stopped at -40 (but that's not the correct formula. Can anyone see what I did wrong?

Is This A Good Question/Topic? 0

## Replies To: Program about Farenheit/Celisus (off by 2)

### #2 jimblumberg

Reputation: 4734
• Posts: 14,759
• Joined: 25-December 09

## Re: Program about Farenheit/Celisus (off by 2)

Posted 20 March 2013 - 01:54 PM

Part of your problem is that you are doing integer math. Remember that integers have no fractions. So things like 1/2 evaluate to zero. You really should be using floating point numbers for these calculations.

Jim

### #3 bytemyzetta

• New D.I.C Head

Reputation: 0
• Posts: 9
• Joined: 20-March 13

## Re: Program about Farenheit/Celisus (off by 2)

Posted 20 March 2013 - 01:58 PM

I getcha. My teacher made it very clear that anything after a decimal point or whatever basically vanishes/is ignored. The word problem insists that we use type int though and that the results might not be super accurate. Is there anything I did that prevents it from being -40 or is -43 technically the right output for this situation?

### #4 jimblumberg

Reputation: 4734
• Posts: 14,759
• Joined: 25-December 09

## Re: Program about Farenheit/Celisus (off by 2)

Posted 20 March 2013 - 02:19 PM

What exactly is the assignment? If you do the math with floating point you end up with -43.6.

Jim

### #5 bytemyzetta

• New D.I.C Head

Reputation: 0
• Posts: 9
• Joined: 20-March 13

## Re: Program about Farenheit/Celisus (off by 2)

Posted 20 March 2013 - 02:31 PM

"Write a program that finds the temperature that is both the same in Fahrenheit and Celsius. Use the formula: (9 * Celsius) / 5 + 32. Your program should use two integer variables for the temperatures Fahrenheit and Celsius. Initialize the value of Celsius to 100. In a loop, decrement the value of Celsius and compute the corresponding Fahrenheit value until the two values are the same. Note: Since you are using integer values, the formula may not give you an exact result. This will not affect the solution to this particular problem."

So might -43 be the correct answer for this problem? It outputs numbers from 212 all the way down to -43.

### #6 jimblumberg

Reputation: 4734
• Posts: 14,759
• Joined: 25-December 09

## Re: Program about Farenheit/Celisus (off by 2)

Posted 20 March 2013 - 03:00 PM

Well the answer should actually be -40. I found this to work properly:

```   do
{
tempF = (9 * tempC) / 5;
tempF = tempF + 32;
cout << tempF << endl;
}while(tempF != tempC--);

```

Jim

### #7 bytemyzetta

• New D.I.C Head

Reputation: 0
• Posts: 9
• Joined: 20-March 13

## Re: Program about Farenheit/Celisus (off by 2)

Posted 20 March 2013 - 03:19 PM

Thanks, that worked for me too! I just got rid of the extra tempC in my for loop and also go -40.