Thread: help! temp converter

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    36

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

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    we have never used the strcmp concept yet, is there another way to compare strings?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by cakestler View Post
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    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;
    }

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    sorry the indentation is a little messy

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    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; 
    }

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    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))
    Woop?

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    yay! thankyou!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  2. Linked List Part 2
    By Nish in forum C Programming
    Replies: 18
    Last Post: 03-09-2005, 05:05 PM
  3. Problem Putting INTs Into a CHAR Array
    By cram in forum C++ Programming
    Replies: 13
    Last Post: 10-13-2004, 07:53 AM
  4. Need help
    By duty11694 in forum C Programming
    Replies: 6
    Last Post: 11-17-2002, 03:23 PM
  5. Help with my c-program
    By duty11694 in forum C Programming
    Replies: 1
    Last Post: 11-15-2002, 02:13 PM