Need help with temperature conversion program

This is a discussion on Need help with temperature conversion program within the C Programming forums, part of the General Programming Boards category; this is what i have so far later i will add a step but 'farn' returns a large number like ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    7

    Need help with temperature conversion program

    this is what i have so far later i will add a step but 'farn' returns a large number like 2 million or something
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    
    	int index = 0;
    	double tempc;
    	double farn = 0;
    	
    /*C = (F - 32) / 1.8 
    F = C  1.8 + 32*/
    
    	printf("Enter temperature in celsius:");
    	scanf("%d",&tempc);
    	printf("------------------------------\n");
    	while(index<=5)
    	{
    		farn= tempc * 1.8 +32;
    		printf("|  %d  |   %d   |\n",tempc, farn);
    		index++ ;
    		
    	}
    		printf("------------------------------\n");
    	
    	return(0);
    }
    can anyone see why it isnt working?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Farn is a double value... but in your printf you are printing an integer...
    Look up the formatting codes for printf() and adjust accordingly.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    ok i tried that and replaced the second %d in printf("| %d | %d |\n",tempc, farn);
    with %g, %lg, %lf, i tried everything and none of them worked, most gave me an incredibly small negative number

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    ah nvm, I changed tempc to an integar and it worked with %g to print the farn temp

    ty for that i didnt realize the print depended on that

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,522
    The scanf call is also wrong: You're required to pass the correct arguments and use the correct conversions in the format string. I'm sure "%lf" will work and then you can printf it with "%f".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug Conversion program
    By rupesh.rk in forum C++ Programming
    Replies: 2
    Last Post: 04-26-2009, 02:47 AM
  2. Minute to hour/minute conversion program
    By Remius in forum C Programming
    Replies: 7
    Last Post: 12-29-2007, 07:39 AM
  3. Temperature Conversion code problems
    By eroth88 in forum C Programming
    Replies: 6
    Last Post: 10-22-2006, 01:24 AM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 10:31 PM
  5. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 05:18 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21