Fahrenheit to Centigrade Formula Returning Zero

Hi all, I've got a problem with a formula that I am trying to use to convert from F to C. Seems that 5 / 9 always returns a zero. Any suggestions?? Here is the code...
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
#include <iostream>
#include <string.h>
using namespace std;

void convert_toc (double *p);
void convert_tof (double *p);
double a;
char b;

int main()
{
    while (1){
            cout << "Enter C to convert from Centigrade to Fahrenheit or F to do the opposite: ";
            cin  >> b;
            cout << endl;
        if (b == 'C' || b == 'c'){
            cout << "Please enter a Centigrade temperature to be converted to Fahrenheit (0 = EXIT): ";
            cin  >> a;
            if (a == 0)
                break;
            cout << "\n";
            cout << "\n";
            cout << a << " degrees in centigrade is equal to ";
            convert_tof(&a);
            cout << a << " degrees in Fahrenheit.\n";
            cout << "\n";
            }
        else {
            cout << "Please enter a Fahrenheit temperature to be converted to Centigrade (0 = EXIT): ";
            cin  >> a;
            if (a == 0)
                break;
            cout << "\n";
            cout << "\n";
            cout << a << " degrees in Fahrenheit is equal to ";
            convert_toc(&a);
            cout << a << " degrees in Centigrade.\n";
        }
    return 0;
    }
}
void convert_tof (double *p) {
    *p = (*p * 1.8) + 32;
}
void convert_toc (double *p) {
    *p = (*p - 32) * (5 / 9); /** Something is wrong with this code. For some reason (5 / 9) always causes a return of zero **/
}
5 / 9 is an integer division, the result gets truncated.
Make one of the operands a double
eg:
5.0 / 9
5. / 9
Last edited on
Awesome! Thanks!!
Topic archived. No new replies allowed.