fahrenheit to celsius conversion problem

May 7, 2010 at 12:52am
So I am trying to write a program with a function that prompts the user to enter degrees Fahrenheit, and then convert it to Celsius in main(). The program compiles fine and all but when I run it and type in 93 as Fahrenheit it comes out -18 celsius, when the answer should be 34. What have I done wrong here?

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

int getTemp()

{
float fahr , celsius;
cout << "Please enter Fahrenheit degrees: ";
cin >> fahr;

return(fahr, celsius);
}

int main()
{
cout.setf(ios::fixed);
cout.precision(0);

getTemp();

float fahr , celsius;

celsius = (5.0 / 9.0) * (fahr - 32.0);
cout << "Celsius: " << celsius << endl;

return (0);

}
May 7, 2010 at 1:04am
You may want to split the getting of the variable up from the actual procedure. So your function would look more like this:
1
2
3
4
float ConvTemp(float tempf) //5.0/9.0*(measuref.to_f-32.0)
{
      return (5.0/9.0)*(tempf-32.0);
}


Let main() handle the actual getting of the variable. If you would like the source for the program I made just ask.

EDIT:
To make sure there is no confusion, the comment was just so I would know the proper conversion equation.
Last edited on May 7, 2010 at 1:29am
May 7, 2010 at 1:25am
Your getTemp() function does not do what you think it does. Look here:

http://www.cplusplus.com/doc/tutorial/functions/
May 7, 2010 at 2:26am
MottMan from what it looks like there I would be doing the conversion in the function instead of main and the assignment calls for me to let main do the conversion but to write a function that prompts the user to input degrees Fahrenheit.

Also firedraco I've read that and haven't really understood it that's why I came here. I've only been programming in C++ for about a week and this is my first programming language all around.
May 7, 2010 at 2:31am
Then firedraco is right. Just read up on them and you'll see it. But since it's not what your looking for anyway, here is what I created:

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
#include <iostream>

float ConvTemp(float tempf);

int main()
{
    using namespace std;
    
    float tempf, tempc;
    
    cout << "Please enter a fahrenheit temperature to be converted to celsius\n";
    cin >> tempf;
    
    tempc = ConvTemp(tempf);
    
    cout << "F: " << tempf << endl;
    cout << "C: " << tempc << endl;
    
    cout << "\nPress ENTER to exit\n";
    cin.get();
    cin.get();
    return 0;
}

float ConvTemp(float tempf) //5.0/9.0*(measuref.to_f-32.0)
{
      return (5.0/9.0)*(tempf-32.0);
}


This may or may not help.
Last edited on May 7, 2010 at 1:31pm
Topic archived. No new replies allowed.