Convert Fahrenheit to Celsius with a Floating-Point Expression

Sorry for asking a new question so soon. I'm new to programming...

Directions:

Need to create a program that will convert Fahrenheit to Celsius because a European friend doesn't understand Fahrenheit temperatures.

You only need two variables in this program: Fahrenheit and Celsius both of which should be the integer data type. When you convert the Fahrenheit to Celsius you will need to use a floating-point expression doing floating-point calculations for precision. Additionally we want to round up or down the Celsius answer by adding 0.5 to the calculation expression.

I don't get the part in bold. What does he mean by "floating-point expression" for precision?

My code so far ( I know it doesn't work):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

// Headers and Other Technical Items

#include <iostream>  
using namespace std;

// Function Prototypes

void pause(void);

// Variables

int     Fahrenheit;
int     Celsius;
double  answer;

//******************************************************
// main
//******************************************************

int main(void)
  {
  // Input	
  cout << "\nEnter the Fahrenheit temperature --->: ";
  cin >> Fahrenheit;


  // Process
  answer = (Fahrenheit - 32)  * (5/9) + 0.5;

  // Output
  cout << "\nThe temperature in Celsius is -------->: ";
  cout << answer;

  pause();
  return 0;
  }

//******************************************************
// pause
//******************************************************

void pause(void)
  {
  cout << "\n\n";
  system("PAUSE");
  cout << "\n\n";
  return;
  }

//******************************************************
// End of Program
//****************************************************** 
Last edited on
In line 29, 5/9 evaluates to 0 because you're performing integer division. Change it to 5.0/9.
Changed it.


But when I run the program, I get..

Lets say Fahrenheit is 98.7, I get 37.1667. Other online conversions claim 37.0555556.

Anyway, I'm suppose to get 37 degrees, not decimals...
closed account (28poGNh0)
Use floor functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Headers and Other Technical Items

# include <iostream>
# include <cstdlib>
# include <cmath>
using namespace std;

// Function Prototypes

void pause(void);

// Variables

int     Fahrenheit;
int     Celsius;
double  answer;

//******************************************************
// main
//******************************************************

int main(void)
  {
  // Input
  cout << "\nEnter the Fahrenheit temperature --->: ";
  cin >> Fahrenheit;


  // Process
  answer = (Fahrenheit - 32)  * (5.0/9) + 0.5;

  // Output
  cout << "\nThe temperature is Celsius is -------->: ";
  cout << floor(answer);

  pause();
  return 0;
  }

//******************************************************
// pause
//******************************************************

void pause(void)
  {
  cout << "\n\n";
  system("PAUSE");
  cout << "\n\n";
  return;
  }

//******************************************************
// End of Program
//****************************************************** 
I haven't read anything on floor functions...I did change my code to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

// Headers and Other Technical Items

#include <iostream>  
using namespace std;

// Function Prototypes

void pause(void);

// Variables

int     Fahrenheit;
int     Celsius;

//******************************************************
// main
//******************************************************

int main(void)
  {
  // Input	
  cout << "\nEnter the Fahrenheit temperature --->: ";
  cin >> Fahrenheit;


  // Process
   Celsius = (Fahrenheit - 32)  * (5.0/9) + 0.5;

  // Output
  cout << "\nThe temperature in Celsius is -------->: ";
  cout << Celsius;

  pause();
  return 0;
  }

//******************************************************
// pause
//******************************************************

void pause(void)
  {
  cout << "\n\n";
  system("PAUSE");
  cout << "\n\n";
  return;
  }

//******************************************************
// End of Program
//******************************************************


It rounds down or up...Works just fine...
Topic archived. No new replies allowed.