Problem with functions

I'm creating a program that converts Fahrenheit to Celsius using multiple functions. This is a project for class so I do have to use the functions. I have it all set but for whatever the reason the output is #INF .

I'm not sure where I went wrong so some help would be great.

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
   /* declarations ***********************************************
      declare function prototypes & data
   */
   // declare local function prototype
   double enterTemperature();          // input function prototype
   double convertTemperature(double);  // processing function prototype
   void printCelsius(double);          // output function prototype

   // declare local constants (scope: local)
   const int CONVERTS = 4;  // number of conversions to be made

   // declare local variables (scope: local)
   double fahrenheit;
   double celsius;

   /* statements *************************************************
      code function instructions
   */

   for(int index = 0; index < CONVERTS; index++)
   {
      // call function to obtain utemperature from user
      fahrenheit = enterTemperature();

      // call function to convert temperature (f to c)
      celsius = convertTemperature(fahrenheit);

      // call function for format & display converted temperature
      printCelsius(celsius);
   }

   cin.get();
   return 0;
}   // end of main() function ----------------------------------------




//-------------------------------------------------------------------
// Input Function
//-------------------------------------------------------------------

double enterTemperature()
{
	int temperature;

	cout << "Enter the tempeture in fahrenheit : ";
	cin >> temperature;

	return temperature;
}


//-------------------------------------------------------------------
// Processing Function
//-------------------------------------------------------------------

double convertTemperature(double fahrenheit)
{
	double celsius;

	celsius = ((fahrenheit - 32) / (5/9));

	return celsius;
}

//-------------------------------------------------------------------
// Output Function
//-------------------------------------------------------------------



void printCelsius(double celsius)
{
	cout << celsius << endl;
}
line 69 - you've got integer division with 5/9 which gives 0 - which then gives a divide by zero error.

line 52 - shouldn't that be double, not int?


Edit: is that the right formula?
Enter the tempeture in fahrenheit : 75 
77.4
Last edited on
I had a typo in the formula there, its 9/5 not 5/9

And ah thanks for catching that int instead of double

it works now, thank you
Topic archived. No new replies allowed.