Functions (Converting Farenheight to Celsius)

Hello, CPlusPlus community. Thank you in advance for you assistance. I'm a computer science novice and am struggling heavily with the concept of arguments, return values, function prototypes, and function calls. I took 190 last year and am taking my second course 226. This function stuff is pretty heavy!

I am having major trouble understanding where I went wrong in this? I apologize for lack of the //comments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

double celsius ( double f);

int main()
{
  double f;
  
  cout<<"Please input a value for faren: ";
  cin>>f;
  cin.ignore();
  cout<<"This is "<< f << " in celsius: " << celsius (f) <<"\n";
  cin.get();
}

double celsius ( double f)
{
  return ((5/9) * (f-32));
}


This keeps spitting out -0 for the users when he gives a number for f.
Last edited on
5 / 9 will always return 0 because it's an integer division. Do this instead: 5.0 / 9.
As josue mentioned any integer divided by an integer is an integer.
You must either do a floating point divided floating point , floating point divided by integer or integer divided by floating point to get a return of a floating point.
ex:
1 / 2.0
1.0 / 2
1.0/2.0
1 / 2f
1f / 2
1f / 2f
Ohhhhhh. That's so messed up. :<
I totally probably got a F for that assignment for lack of a decimal.

Why is this stuff so haaaarrrrrd?

Thank you, Josue and Giblit!
Actually, it's because of this stuff that organized software development is possible. :-P You're welcome!
thesagaof wrote:
Why is this stuff so haaaarrrrrd?

Well think about it are integers rational or irrational numbers?

Here is a better explanation.
1
2
int operator/( int , int )
float operator/( float , int ) // not sure if it is float or double by default 


That's how the operator/ is overloaded AFAIK.
Just rememeber int + int = int
int * int = int
int / int = int
int - int = int

Since irrational anything irratonal = irrational
but if you put a rational anywhere then it has to be rational since 1 + 1.2 will be equal to 2.2 and not 2

*edit spelled quote wrong o.O
Last edited on
Well, I do admit that the plethora of ways of skinning the cat can become overwhelming; in addition to what giblit said, you can also cast one integer and let the compiler coerce the other (or cast both!) this way:

1
2
(float)5 / 9;
float(5) / 9;

Messed up indeed.
That's the c-style cast there is also static , dynamic , and other casts out there.
Topic archived. No new replies allowed.