### Functions will not do math

I can't get any output for celcius besides zero out of either function.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687`` ``````#include #include using namespace std; void celcius(int); void celciusloop(); int fatemp; int main() { int menu; cout<<"If you would like to convert a Farenheit temperature to celcius, press 1;" <> menu; while (menu >2|| menu<1) { cout<<"That entry is invalid, please re enter your selection: "; cin>>menu; } while (menu==1) { cout<<"This program will convert Farenheit to Celcius for you." <>fatemp; cout<<"Thankyou"; system ("pause"); celcius(fatemp); cout<<"If you would like to enter another number to convert press 1, otherwise press 2 " <>menu; } if (menu==2) {celciusloop(); system ("pause"); return 0; } else { system ("Pause"); return 0; } } void celcius(int fatemp) { double celcius; celcius=5/9*(fatemp-32); cout<
Dat formatting.

Anyways, try changing fatemp to a double as well.
You seem to be doing integer math in your calculations, 5/9 in integer math yields 0. There are no fractions with integers. You need to have at least one of your terms as a floating point type to force floating point math.
Change:
 ``12345678`` `````` void celcius(int fatemp) { double celcius; celcius=5/9*(fatemp-32); cout<

to:
 ``1234`` ``````double FtoC(double f) { return 5.0 / 9.0 * (f - 32.0); }``````

Then change line 35 from: `celcius(fatemp);`
to: `cout << "The celcius conversion of your temperature is: " << FtoC(fatemp) << endl;`
Last edited on
Except you can't return an item from a void function. You also need to change the function to return a double.
We haven't learned what "ftoc" is in class, so my teacher wouldn't like if I used it. What is its purpose? Also, my funtions only have to display, they don't have to return anything to the main function.
That's dumb design. A function shouldn't be calculating and outputting. If he knows anything, he'll be more pleased if you go with Stewbond's route.
geharbison wrote:
We haven't learned what "ftoc" is in class,
You won't find "FtoC" in a textbook, it's just the name of a user-defined function, it can be anything you like. I expect Stewbond chose it as a concise and meaningful way of saying "Fahrenheit to Celsius".

 so my teacher wouldn't like if I used it.
It's a function. I'm sure your teacher will be very pleased if you use functions.
Last edited on
Topic archived. No new replies allowed.