WHAT'S WRONG WITH THIS?

Hello Everyone!!
I just want to know the error in this piece of code (temperature function) that I suppose to call in my unit converter program.
For instance, I want to convert 1 C to F and I got 33.8..
but the problem is when I want to convert 33.8 F to C, the result is not 1..

I need help...I know my formula is right but how did it happen that the result is wrong?

Hope you can help me...
________________________________________________________________________________

double temperature()
{
string u[3]={"C","F","K"};
double inp,ans;
int i,giv,wan;
string other="~ UNIT NOT FOUND!";

cout<<endl<<"TEMPERATURE UNIT CONVERTER"<<endl;
cout<<"__________________________________________________________"<<endl;
cout<<" 0.C 1.F 2.K"<<endl;
cout<<"----------------------------------------------------------"<<endl;
//INPUTS
cout<<"Enter no. corresponding to the GIVEN UNIT: ";cin>>giv;
if (giv<0||giv>2)
cout<<other<<endl;
else
{
for (i=0;i<3;i++)
{
if(giv==i)
cout<<" ~ Enter a value in "<<u[i]<<": ";
}
cin>>inp;
cout<<"Enter no. corresponding to the WANTED UNIT:";cin>>wan;
if(wan<0||wan>2)
cout<<other<<endl;
else if (wan==giv)
cout<<" ~ NOTHING TO CONVERT!"<<endl;
else
{
//PROCESS
if(giv==0)
{
if(wan==1)
ans=1.8*inp+32;
else
ans=inp+273.15;
}
else if(giv==1)
{
if(wan==0)
ans=inp*(inp-32);//Error???
else
ans=0.5555556*(inp-32);
ans=ans+273.15;
}
else
{
if(wan==0)
ans=inp-273.15;//Error???
else
ans=inp-273.15;
ans=1.8*ans+32;
}
//OUTPUT
for (i=0;i<3;i++)
{
if (wan==i)
cout<<" ~ That is equal to "<<ans<<" "<<u[i]<<"."<<endl;
}
}
}
}

________________________________________________________________________________
Hey there,

So when I compile this code, I am getting an error saying that your function needs to return a double, which it doesn't... Is this function just suppose to return the statement at the end or the answer?

If just printing to cout, you're function prototype should be: void temperature(void)
If returns double, then you need a "return ans;" somewhere at the end of your function.

Anyways, I tried your code with it just writing to cout to see what your conversion problem is, and what I am getting is in here:

if(wan==0)
ans=inp*(inp-32);//Error???
else
ans=0.5555556*(inp-32);
ans=ans+273.15;

So I looked it up, and the easiest way to convert Fahrenheit to Celsius is to
1. Subtract 32
2. Divide by 9
3. Multiply by 5

and here it is:

if(wan==0)
{
ans = inp - 32;
ans = ans / 9;
ans = ans * 5;
}
else
{
ans=0.5555556*(inp-32);
ans=ans+273.15;
}


not sure what the else stuff is for, but the top stuff works for converting F to C.

Are you also having problems with converting to Kelvins at all?
hi,
i've checked this and i get a value for 33.8F to C equals to 333.99.
you forget about brackets. please try this:

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdlib.h>
#include <iostream>
using namespace std;

/*
 * 
 */
void temperature();


int main(int argc, char** argv) {
    temperature();
    return (EXIT_SUCCESS);
}

void temperature() {
    string u[3] = {"C", "F", "K"};
    double inp, ans;
    int i, giv, wan;
    string other = "~ UNIT NOT FOUND!";

    cout << endl << "TEMPERATURE UNIT CONVERTER" << endl;
    cout << "__________________________________________________________" << endl;
    cout << " 0.C 1.F 2.K" << endl;
    cout << "----------------------------------------------------------" << endl;
    //INPUTS
    cout << "Enter no. corresponding to the GIVEN UNIT: ";
    cin >> giv;
    if (giv < 0 || giv > 2)
        cout << other << endl;
    else {
        for (i = 0; i < 3; i++) {
            if (giv == i)
                cout << " ~ Enter a value in " << u[i] << ": ";
        }
        cin >> inp;
        cout << "Enter no. corresponding to the WANTED UNIT:";
        cin >> wan;
        if (wan < 0 || wan > 2)
        {cout<<"\n1:::::"<<inp<<":::::::"<<ans<<endl;
            cout << other << endl;}
        else if (wan == giv)
            cout << " ~ NOTHING TO CONVERT!" << endl;
        else {
            //PROCESS
            if (giv == 0) {
                if (wan == 1)
                {cout<<"\n2:::::"<<inp<<":::::::"<<ans<<endl;
                    ans = 1.8 * inp + 32;}
                else
                {cout<<"\n3:::::"<<inp<<":::::::"<<ans<<endl;
                    ans = inp + 273.15;}
            } else if (giv == 1) {
                if (wan == 0)
                {cout<<"\n4:::::"<<inp<<":::::::"<<ans<<endl;
                    ans = inp * (inp - 32);} //Error???
                else{
                    ans = 0.5555556 * (inp - 32);
                ans = ans + 273.15;
                cout<<"\n5:::::"<<inp<<":::::::"<<ans<<endl;}
            } else {
                if (wan == 0)
                    ans = inp - 273.15; //Error???
                else
                {ans = inp - 273.15;
                ans = 1.8 * ans + 32;}
            }
            //OUTPUT
            for (i = 0; i < 3; i++) {
                if (wan == i)
                    cout << " ~ That is equal to " << ans << " " << u[i] << "." << endl;
            }
        }
    }
}


Topic archived. No new replies allowed.