-1
#include <stdio.h>

int main(){

    // 2) Write a program to convert ferhenhite to celcius.

    double temp;
    char unit1,unit2,Ferhenhite[] = "Ferhenhite",Celcius[] = "Celcius",Kelvin[] = "Kelvin";
        printf("Enter the degree temperature you want to convert: ");
        scanf("%lf",&temp);
        printf("Select the degree of temperature for the entered temperature. (Ferhenhite/Celcius/Kelvin): ");
        scanf("%c", unit1);
        printf("Select the degree of temperature to be converted for the entered temperature. (Ferhenhite/Celcius/Kelvin): ");
        scanf("%c", unit2);
            if (unit1==Ferhenhite)
            {
                if (unit2==Celcius)
                {
                    printf("The entred temperature in celcius is: %lf°",5/9*(temp-32));

                }else printf("The entred temperature in kelvin is: %lf°",(temp + 459.67) * 5/9);
            }
            else if (unit1==Celcius)
            {
                if (unit2==Ferhenhite)
                {
                    printf("The entred temperature in ferhenhite is: %lf°",(9/5)*temp+32);

                }else printf("The entred temperature in kelvin is: %lf°",temp + 273.15);
                
            }
            else if (unit1==Kelvin)
            {
                if (unit2==Celcius)
                {
                    printf("The entred temperature in ferhenhite is: %lf°",temp - 273.15);

                }else printf("The entred temperature in celcius is: %lf°",(temp - 273.15) * 9/5 + 32);
            }else printf("You have entered wrong unit");
            
    return 0;
}

Error : Comperision between pointer and intergers

C program that can be used to convert temperatures from one unit to another. The program will ask the user for the temperature and the units of the temperature (Ferhenhite, Celsius, or Kelvin). Depending on the units the user enters, the program will calculate and display the temperature in the corresponding unit.

  • 1
    `unit1==Ferhenhite` is trying to compare a `char` to a `char *`. What do you want the user to enter? A letter or a word? – 001 Mar 01 '23 at 14:11
  • 1
    @Jisnu Kalita You are trying to compare a single character with a character array as for example if (unit1==Ferhenhite) Also calls of scanf like that scanf("%c", unit1); are also invalid. – Vlad from Moscow Mar 01 '23 at 14:11
  • 1
    There are earlier errors such as `scanf("%c", unit1);`. Also please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). – Weather Vane Mar 01 '23 at 14:13

1 Answers1

0

In reviewing your code and testing it out, if you truly want to require the user to enter in a string value such as "Celcius" and compare it against a string constant, you would want to first define the input variables as strings (character arrays) as well, and also utilize the string comparison function. With that in mind I did a bit of code refactoring, commenting out the original character test for which you received compiler messages and utilized the "strcmp" function instead.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    // 2) Write a program to convert ferhenhite to celcius.

    double temp;
    char unit1[40],unit2[40],Ferhenhite[] = "Ferhenhite",Celcius[] = "Celcius",Kelvin[] = "Kelvin";
    printf("Enter the degree temperature you want to convert: ");
    scanf("%lf",&temp);
    printf("Select the degree of temperature for the entered temperature. (Ferhenhite/Celcius/Kelvin): ");
    scanf("%s", unit1);
    printf("Select the degree of temperature to be converted for the entered temperature. (Ferhenhite/Celcius/Kelvin): ");
    scanf("%s", unit2);
    //if (unit1==Ferhenhite)
    if (strcmp(unit1, Ferhenhite) == 0)
    {
        //if (unit2==Celcius)
        if (strcmp(unit2, Celcius) == 0)
        {
            printf("The entred temperature in celcius is: %lf°\n", 5.0/9.0*(temp-32.0));

        }
        else printf("The entered temperature in kelvin is: %lf°\n", (temp + 459.67) * 5.0/9.0);
    }
    //else if (unit1==Celcius)
    else if (strcmp(unit1, Celcius) == 0)
    {
        //if (unit2==Ferhenhite)
        if (strcmp(unit2, Ferhenhite) == 0)
        {
            printf("The entred temperature in ferhenhite is: %lf\n°", (9.0/5.0)*temp+32.0);

        }
        else printf("The entred temperature in kelvin is: %lf°\n", temp + 273.15);

    }
    //else if (unit1==Kelvin)
    else if (strcmp(unit1, Kelvin) == 0)
    {
        //if (unit2==Celcius)
        if (strcmp(unit2, Celcius) == 0)
        {
            printf("The entred temperature in ferhenhite is: %lf\n°", temp - 273.15);

        }
        else printf("The entred temperature in celcius is: %lf°\n", (temp - 273.15) * 9.0/5.0 + 32.0);
    }
    else printf("You have entered wrong unit\n");

    printf("FYI, 5/9 will result in a value of: %d\n", 5/9);    /* Just added to denote that division of two integers will result in an integer */

    return 0;
}

Following are some of the highlights of the refactoring.

  • Since string comparison would be used, the "string.h" file was included at the top of the code.
  • The various "if" tests were revised to check for the outcome of the "strcmp" comparison of the user's entered units of measure versus the string constants "Ferhenhite", "Celcius", and "Kelvin".
  • In the formulas where the division of "5" by "9" or "9" by "5" were occurring, the constant values were redefined as floating point constants; otherwise, integer division was occurring resulting in an integer result - a sample "printf" statement was added just to illustrate this point towards the end of the code.

With those bits refactored, following was a sample test run at the terminal.

@Vera:~/C_Programs/Console/Temparature/bin/Release$ ./Temparature 
Enter the degree temperature you want to convert: 98.6
Select the degree of temperature for the entered temperature. (Ferhenhite/Celcius/Kelvin): Ferhenhite
Select the degree of temperature to be converted for the entered temperature. (Ferhenhite/Celcius/Kelvin): Celcius
The entred temperature in celcius is: 37.000000°
FYI, 5/9 will result in a value of: 0

If you did want to use the character comparison route, the program could have asked for a single character input (e.g. "C", "F", or "K") and then an "if" statement such as "if (unit1 == 'F')" could have been used.

Anyway, try out these refactored bits and see if it meets the spirit of your project.

NoDakker
  • 3,390
  • 1
  • 10
  • 11