Help with this code? (Complete Beginner)

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;

​C = ftoc(C, F);
​cout<<C;

​system("pause");

}
Integer division; 5/9 is 0.

Also, why does ftoc take celcius as a parameter? It should be a local variable in the function, not a parameter.
Last edited on
You are using integer division.
Floating point:
5.0 / 9.0 = 0.55555556

Integer
5/9 = 0

Hence this statement
​C = 5/9*(F-32);
is the same as
​C = 0*(F-32);
Thanks for the help!

One more question, I made tweaks to the code, but the answer is still displayed as an interger. How can I fix this?

//This program will convert degrees Farenheit to Degrees Celcius
#include <iostream>
using namespace std;

double ConvertToCelcius(double, double);

double ConvertToCelcius(double C, double F)

{


C = 5.0 / 9.0*(F-32.0);
return (C);

}

int main()

{

double C, F;

C = 0.00;

cout<<" Welcome \n";
cout<<" Enter the temperature in degrees farenheit: ";
cin>>F;

C = ConvertToCelcius(C, F);

cout<<"********************************************************************************\n";
cout<<"The temperature, "<<F<<" degrees Farenheit, is "<<C<<" degrees Celcius\n";
cout<<"\n";
cout<<"********************************************************************************\n";

system("pause");

}
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.

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
29
30
31
32
//This program will convert degrees Fahrenheit to Degrees Celsius
#include <iostream>
#include <iomanip>

using namespace 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

********************************************************************************
Last edited on
Topic archived. No new replies allowed.