Hello all. I am new to programming (just started a class in college), and I am having a problem getting this program to function correctly. Our task was to convert Fahrenheit to Celsius using a called function in the main. However, when I go to debug the program in visual studio 2012, I receive the output, 0. What is wrong with my program?
//This program will convert degrees Farenheit to Degrees Celcius
#include <iostream>
using namespace std;
int ftoc(int, int);
int ftoc(int C, int F)
{
C = 5/9*(F-32);
return (C);
}
int main()
{
int C, F;
C = 0;
cout<<"Enter the temperature in degrees farenheit\n";
cin>>F;
As L B pointed out, the function doesn't need C as a parameter.
The way the result is displayed by default will depend on the actual value. For example 68 F converts to exactly 20 C while 69 F converts to 20.5556 C (with a recurring figure).
To control how the values are displayed, you can use various formatting options such as fixed, setprecision() and maybe showpoint.
//This program will convert degrees Fahrenheit to Degrees Celsius
#include <iostream>
#include <iomanip>
usingnamespace std;
double ConvertToCelcius(double F)
{
return 5.0 / 9.0*(F-32.0);
}
int main()
{
double F;
cout<<" Welcome \n";
cout<<" Enter the temperature in degrees Fahrenheit: ";
cin>>F;
double C = ConvertToCelcius(F);
//cout << showpoint;
cout << fixed;
cout << setprecision(1);
cout<<"********************************************************************************\n";
cout<<"The temperature, "<<F<<" degrees Fahrenheit, is "<<C<<" degrees Celsius\n";
cout<<"\n";
cout<<"********************************************************************************\n";
//system("pause");
}
Welcome
Enter the temperature in degrees Fahrenheit: 68
********************************************************************************
The temperature, 68.0 degrees Fahrenheit, is 20.0 degrees Celsius
********************************************************************************