Reputation: 1547
I am passing a value into a function to convert Celsius to Fahrenheit, but the console prints an error code which brings up nothing when I Google it.
// Function Declarations
float fToC(float degreesF = 32.0);
int main() {
float fahrenheit = 10;
float centigrade = 10;
centigrade = fToC(fahrenheit);
cout << "Freezing Point: " << fToC << "C" << endl;
return 0;
}
// Function Defintions
float fToC(float degreesF) {
float degreesC = ((5.0 / 9.0)*(degreesF - 32));
return degreesC;
}
This prints into the console:
Freezing Point: 00A31627C
What did I do wrong?
Upvotes: 6
Views: 191
Reputation: 1130
cout << "Freezing Point: " << fToC << "C" << endl;
is not correct. You cannot output a function name in a cout
like this and hope it will give you the result you want. It will instead print out the address the function is stored at. You can however call the function as
cout << "Freezing Point: " << fToC(fahrenheit) << "C" << endl;
because fToC(fahrenheit) is actually a float
, with the parameter you gave to it, so you should get the answer you want instead of the function address. You could also just use:
cout << "Freezing Point: " << centigrade << "C" << endl;
which you have defined in the previous line.
Upvotes: 6
Reputation: 1
but the console prints an error code which brings up nothing when I Google it.
That's not an error code.
You're missing the parenthesis to call a function:
cout << "Freezing Point: " << fToC(farenheit) << "C" << endl;
// ^^^^^^^^^^^
the function address is printed instead.
You probably meant to print
cout << "Freezing Point: " << centigrade << "C" << endl;
as you already calculated the value by calling the function.
Upvotes: 8
Reputation: 2559
It is because you are printing out the function and not centigrade.
cout << "Freezing Point: " << fToC << "C" << endl;
in the console it is showing the memory address of the function.
You should do something like:
cout << "Freezing Point: " << centigrade << "C" << endl;
Upvotes: 9
Reputation: 297
This is wrong:
cout << "Freezing Point: " << fToC << "C" << endl;
You pass fToC to the console, and so it prints the address of the function. Remove fToC and write centigrade.
Upvotes: 7
Reputation: 118425
Well,
cout << "Freezing Point: " << fToC << "C" << endl;
Pop quiz for you: what is "fToC"?. Why, it's your function, and what you see, as the result, is the meaningless internal memory address where your function's code lives.
You probably meant to write:
cout << "Freezing Point: " << centigrade << "C" << endl;
Unfortunately, a computer does only what you tell it to do, and not what you want it to do.
Upvotes: 7