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;
}
}
}
}
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: