Thread: Temperature conversion

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    14

    Program to Convert Temperature

    I'm new to this -- I couldn't find my original thread. so here goes:
    This is the code I've written. I need the user to be able to make a choice of Celsius or Fahrenheit. Only one temperature at a time.
    The ouput should be something like: 100 degrees Celsius is 212 degrees Fahrenheit

    #include <stdio.h>

    main()


    {
    float Fahrenheit, Celsius;


    printf( "Enter a temperature in degrees\n" );
    printf( "if in Fahrenheit enter F\n" );
    printf( "if in Celsius enter C\n" );

    scanf("%f%f", Celsius);


    scanf( "%f%f", Fahrenheit);


    return 0;

    }
    Anna

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    14
    Please help me - I found my old message re temperature conversion but was unable to use it or move it. Can you help me with the previously submitted code?
    Anna

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    I had to do a similar exercise in a CPP class a while back. Though this version has potential for disaster (namely a char entered as choice) it *might* offer something useful.

    Code:
    #include <stdio.h> 
    
    void fTOc(float);
    void cTOf(float);
    
    int main(void)
    {
    	float userTemp;
    	int choice;
    	int control;
    	
    	control = 0;
    		
    	do
    	{
    	
    	    printf("1) Convert a temperature to Celsius\n");
    	    printf("2) Convert a temperature to Fahrenheit\n");
    	    printf("3) Quit\n\n"); 
    	
    	    printf("Enter your choice: ");
    	    scanf("%d", &choice); 
    	    	
    	    switch(choice)
    	    {
    	        case 1: printf("\nEnter the temp to convert: ");
    	                scanf("%f", &userTemp);
    	                fTOc(userTemp);
    	                break;
    		        
                    case 2: printf("\nEnter the temp to convert: ");
                            scanf("%f", &userTemp);
                            cTOf(userTemp);
                            break;
    		        
                   case 3: control = 1;
                           break;
    		
                   default:  printf("\n%d is not a valid option.\n", choice);
                             break;
                }; 
    		
                printf("\n");
    
            }while(control == 0);
            printf("\n\nUser ended program.");
            return 0;
    	
    } 
    
    void cTOf(float temp)
    {
        printf("\n%.2f degrees Celsius is %.2f degrees Fahrenheit.\n", temp, (32 + temp * 1.8));
    }
    
    void fTOc(float temp)
    {
        printf("\n%.2f degrees Fahrenheit is %.2f degrees Celsius.\n", temp, (temp - 32) / 1.8);
    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Temperature conversion...
    By Onslaught in forum C Programming
    Replies: 3
    Last Post: 10-21-2005, 01:15 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  5. Help with temperature conversion Chart
    By sanmaximo in forum C++ Programming
    Replies: 1
    Last Post: 11-13-2003, 03:29 PM