# help! temp converter

• 03-05-2009
cakestler
help! temp converter
hello, i am working on a tempconverter program, i have tried a number of things to get this program to work and everytime i try something it seems to either mess something else up, or only do half of what i need it to do. could anyone give me some hints or insight as to what i am doing wrong? thanks

Code:

``` double fTOc(double); //Fahrenheit to Celsius double cTOf(double); //Celsius to Fahrenheit int main() {     double temp;//temperature entered     char scale[1];//identify scale used (C or F)     double absZeroC = -273.15;//variable to easier identify absolute zero in Celsius     double absZeroF = -459.67;//variable to easier identify absolute zero in Fahrenheit         printf("Enter the temperature followed by F or C( Ex: \"75 F\"):");     scanf("%lf%s", &temp, scale);             if (scale == "C")         if (temp > absZeroC)             printf("\nTemperature in Fahrenheit is %f.\n", cTOf(temp));         else if ( temp < absZeroC)             printf("\nTemperature %f is less than absolute zero %f", temp, absZeroC);     if (scale == "F")         if (temp > absZeroF)             printf("\nTemperature in Celsius is %f.\n", fTOc(temp));         else if (temp < absZeroF)             printf("\nTemperature %f is less than absolute zero %f", temp, absZeroF);     else         printf("\nInvalid scale entry! Use C for Celsius or F for Fahrenheit!\n\n");         system("pause");     return 0;  } double fTOc(double f)//fah to celsius {     return (5.0 / 9.0) * (f - 32); } double cTOf(double c)//celsius to fah {     return ((9.0 / 5.0) * c) + 32; }```
• 03-05-2009
tabstop
scale == "C" and scale == "F" can never be true. If you want to compare strings, you need to use the string comparison function strcmp.

Also, this isn't Python; your indentation is good but you do actually have to use braces.
• 03-05-2009
cakestler
we have never used the strcmp concept yet, is there another way to compare strings?
• 03-05-2009
vart
Quote:

Originally Posted by cakestler
we have never used the strcmp concept yet, is there another way to compare strings?

using loop - compare each byte

since in your case string is 1 byte long you need to compare first and second byte of the strings
• 03-05-2009
hk_mp5kpdw
You are entering a character (C or F) so use a single character instead using "strings":
Code:

```    char scale;//identify scale used (C or F)         ...     printf("Enter the temperature followed by F or C( Ex: \"75 F\"):");     scanf("%lf %c", &temp, &scale);             if (scale == 'C')     ...     if (scale == 'F')```
... if you really don't want to use strcmp.
• 03-05-2009
cakestler
thanks!! that helps, but now it just prints both values, here is my revised code
Code:

``` double fTOc(double); //Fahrenheit to Celsius double cTOf(double); //Celsius to Fahrenheit int main() {     double temp;//temperature entered     char scale;//identify scale used (C or F)     double absZeroC = -273.15;//variable to easier identify absolute zero in Celsius     double absZeroF = -459.67;//variable to easier identify absolute zero in Fahrenheit         printf("Enter the temperature followed by F or C( Ex: \"75 F\"):");     scanf("%lf%c", &temp, &scale);           if ((scale == 'C' || 'c') && (temp > absZeroC))             printf("\nTemperature in Fahrenheit is %f.\n", cTOf(temp));         else if (temp < -273.15)             printf("\nTemperature %f is less than absolute zero %f", temp, absZeroC);   if ((scale == 'F' || 'f') && (temp > absZeroF))             printf("\nTemperature in Celsius is %f.\n", fTOc(temp));//Print and Call Function           else if (temp < -459.67)             printf("\nTemperature %f is less than absolute zero %f", temp, absZeroF);     else if (scale != 'C' || 'F')         printf("\nInvalid scale entry! Use C for Celsius or F for Fahrenheit!\n\n");     system("pause");     return 0;  } double fTOc(double f)//fah to celsius {     return (5.0 / 9.0) * (f - 32); } double cTOf(double c)//celsius to fah {     return ((9.0 / 5.0) * c) + 32; }```
• 03-05-2009
cakestler
sorry the indentation is a little messy
• 03-05-2009
cakestler
revised loop again sorry
Code:

```  printf("Enter the temperature followed by F or C( Ex: \"75 F\"):");     scanf("%lf%c", &temp, &scale);           if ((scale == 'C' || 'c') && (temp > absZeroC))             printf("\nTemperature in Fahrenheit is %f.\n", cTOf(temp));         else if (temp < -273.15)             printf("\nTemperature %f is less than absolute zero %f", temp, absZeroC);   else if ((scale == 'F' || 'f') && (temp > absZeroF))             printf("\nTemperature in Celsius is %f.\n", fTOc(temp));//Print and Call Function         else if (temp < -459.67)             printf("\nTemperature %f is less than absolute zero %f", temp, absZeroF);         else if (scale != 'C' || 'F')             printf("\nInvalid scale entry! Use C for Celsius or F for Fahrenheit!\n\n");     system("pause");     return 0; }```
• 03-05-2009
prog-bman
You need to do the comparison of the variable twice in the if statements
Code:

`if ((scale == 'C' || 'c') && (temp > absZeroC))`
Should be:
Code:

`if ((scale == 'C' || scale == 'c') && (temp > absZeroC))`
• 03-05-2009
cakestler
yay! thankyou!