Thread: Temperature conversion program bug.

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    18

    Question Temperature conversion program bug.

    This program is supposed to prompt the user to either convert form celsius to degree or vice versa. Then ask for a temperature to be entered and ouput the result. When I enter the temperature, the conversion result always comes out to 0. It does this with both celsius to fahrenheit and fahrenheit to celsius. I cant seem to find the problem!

    Code:
    /*exercise 3.1 converting temperatures*/
    #include <stdio.h>
    #include <ctype.h>
    int main()
    {
        
    char type = 0;
        
    double degree = 0.0;
        
    double result = 0.0;
        printf("\nEnter A to convert celsius to fahrenheit or B to convert fahrenheit to celsius: ");
        scanf("%c", &type);
        printf("\nEnter degree: ");
        scanf("%d", &degree);
        
    switch (toupper(type))
        {
        
    case'A':
            result = degree*1.8+32;
            printf("\n%d degrees celsius is %d degrees fahrenheit", degree, result);
            
    break;
        
    case'B':
            result = ((32 - degree)*5)/9;
            printf("\n%d degrees fahrenheit is %d degrees celsius", degree, result);
            
    break;
        
    default:
            printf("\nYou did not enter A or B, try again");
            
    break;
        }
    }
    

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    use "%lf" as printf-format string to print a double.
    EDIT: actually "%f" should do. there is no difference between float and double for variadic functions.
    Last edited by ZuK; 08-12-2012 at 03:20 PM.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    18
    Thanks! problem solved

  4. #4
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by ZuK View Post
    use "%lf" as printf-format string to print a double.
    EDIT: actually "%f" should do. there is no difference between float and double for variadic functions.
    It's been sometime since I coded in C but assuming he does exactly as u say, he would be leaving scanf to expect a int and then store it in space reserved for a double...this would still not produce the output the OP is looking for. Assuming your way:
    Code:
    double degree = 0.0;
        
    double result = 0.0;
        printf("\nEnter A to convert celsius to fahrenheit or B to convert fahrenheit to celsius: ");
        scanf("%c", &type);
        printf("\nEnter degree: ");
        scanf("%d", &degree);
        
    switch (toupper(type))
        {
        
    case'A':
            result = degree*1.8+32;
            printf("\n%f degrees celsius is %d degrees fahrenheit", degree, result);
            
    break;
    I'm curious to see what the outcome on your system would be assuming this.


    Apparently the OP solved the issue. However solving and not knowing how it was done, especially when learning C isn't much help. Thus if you're declaring and expecting a certain data type, then make sure scanf's format string is set to accept that type. In some cases when calling printf you may have a little flexibility and want to change how the variable will be outputted to the end user ex:
    Code:
    #include <stdio.h>
    
    int main(void){
    	
    	char c;
    	
    	scanf("%c", &c);
    	
    	printf("you put: %c which is %d in decimal", c, c);
    	
    	getchar();
    	return 0;
    }
    However you are not doing anything like this.

    Thus to the correct way would be to give scanf the proper string it is expecting and follow through with your printf statements
    Code:
    /*exercise 3.1 converting temperatures*/
            #include <stdio.h>
            #include <ctype.h>
            int main()
            {
                
            char type = 0;
                
            double degree = 0.0;
                
            double result = 0.0;
                printf("\nEnter A to convert celsius to fahrenheit or B to convert fahrenheit to celsius: ");
                scanf("%c", &type);
                printf("\nEnter degree: ");
                scanf("%lf", &degree);
                
            switch (toupper(type))
                {
                
            case'A':
                    result = degree*1.8+32;
                    printf("\n%lf degrees celsius is %lf degrees fahrenheit", degree, result);
    As a slight aside if there are no arguments given to main then it should be set as
    Code:
    int main(void)
    compiling with at least -Wall should have given you at least some warnings about this.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I see now that the op used the wrong format for the scanf() call too. Had not noticed that before.

    All I meant to say in my edit is that "%lf" is actually not correct because a "long floating point" value will never be passed to a variadic function like printf() because the compiler promotes even a float value to a double to such a function. Therefore there is no need to make a difference in the format string between float and double.
    Kurt

  6. #6
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    Might double check your math on case 'B'.

    Also, you should return an int from main().

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    18
    Oh, alright, its strange that it compiled and the program did what I intended, but I'll change it to the correct value types. Thanks

    Actually I had changed scanf call too along with the printf. I didnt notice that Zuk only told me to change the printf() call.
    Last edited by prafiate; 08-13-2012 at 09:44 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Temperature Conversion Program
    By llind212 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2011, 11:37 PM
  2. Need help with temperature conversion program
    By Aequitas in forum C Programming
    Replies: 4
    Last Post: 03-06-2011, 08:28 PM
  3. Temperature conversion
    By darknasio in forum C Programming
    Replies: 4
    Last Post: 09-21-2010, 03:04 AM
  4. Temperature conversion...
    By Onslaught in forum C Programming
    Replies: 3
    Last Post: 10-21-2005, 01:15 PM
  5. Temperature conversion
    By Anna Lane in forum C Programming
    Replies: 2
    Last Post: 10-17-2002, 09:25 PM