Temperature Conversion code problems

This is a discussion on Temperature Conversion code problems within the C Programming forums, part of the General Programming Boards category; I cannot seem to get the program to work correctly. I have tried to figure out whats wrong with the ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    Unhappy Temperature Conversion code problems

    I cannot seem to get the program to work correctly. I have tried to figure out whats wrong with the code but i got nothing, i am new to C programming any help will be much appreciated.

    thanks a bunch!
    Code:
    #include <stdio.h>
    
    void main()
    {
    	float (temp_kelvin);
    	float (temp_celcius);
    	float (temp_farhenheit);
    	printf("Enter a temperature in Celcius:");
    	scanf_s("2f",&temp_celcius);
    		temp_kelvin = (temp_celcius + 273.15);
    	    temp_farhenheit = (((1.8)*temp_celcius)+32);
    		
    	printf("The equivalent Farhenheit temperature is: .2%f\n", temp_farhenheit);
    	printf("The equivalent Kelvin temperature is: .2%f\n", temp_kelvin);
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Don't use void main() -- see the FAQ. What's scanf_s()? You have extra parentheses which are causing errors:
    Code:
    float (temp_kelvin);
    ->
    Code:
    float temp_kelvin;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Some splint:
    Owner@pavilion ~
    $ splint new.c
    Splint 3.1.1 --- 02 May 2003

    new.c:3:6: Function main declared to return void, should return int
    The function main does not match the expected type. (Use -maintype to inhibit
    warning)
    new.c: (in function main)
    new.c:9:2: Unrecognized identifier: scanf_s /* is this some function on your compiler? Use something standard to get a number. */
    Identifier used in code has not been declared. (Use -unrecog to inhibit
    warning)
    new.c:10:18: Variable temp_celcius used before definition
    An rvalue is used that may not be initialized to a value on some execution
    path. (Use -usedef to inhibit warning)
    new.c:10:3: Assignment of double to float:
    temp_kelvin = (temp_celcius + 273.15) /* Compilers tend to promote floating point literals to double type. Use 273.15f to force otherwise. */
    To allow all numeric types to match, use +relaxtypes.
    new.c:11:6: Assignment of double to float:
    temp_farhenheit = (((1.8) * temp_celcius) + 32)

    Finished checking --- 5 code warnings
    Fix those. And not that it's a problem, but I don't see a reason to put variable names in parentheses.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    still not working..new code

    Code:
    #include <stdio.h>
    
    
    void main()
    {
    	float temp_kelvin;
    	float temp_celcius;
    	float temp_farhenheit;
    	printf("Enter a temperature in Celcius: ");
    	scanf_s("2f",&temp_celcius);
    		temp_kelvin = (temp_celcius + 273.15f);
    	    temp_farhenheit = ( ( (1.8) * temp_celcius) + 32);
    		
    	printf("The equivalent Farhenheit temperature is: .2%f\n", temp_farhenheit);
    	printf("The equivalent Kelvin temperature is: .2%f\n", temp_kelvin);
    }
    thats what i got now and still not work. it gives the same incorrect temperatures no matter what starting celcius temperature i use. this is leading me to believe there is an error in the
    Code:
    scanf_s("2f",&temp_celcius);
    line possibly?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Yes; scanf_s isn't a function. (If it is, I don't know about it and neither does your compiler.) Try
    Code:
    scanf("%f", &temp_celcius);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    You also forgot about these:

    temp_farhenheit = (((1.8f) * temp_celcius) + 32.0f)
    new.c:3:6: Function main declared to return void, should return int

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    http://msdn.microsoft.com/msdnmag/is...C/default.aspx
    scanf_s is microsoft's "safe" scanf function. But as so ably demonstrated already, it's still possible to completely screw up when using it.
    No doubt the OP gets some "deprecated, use scanf_s" type message when trying to use scanf().
    The link tells you how to turn that non-portable nonsense off.

    Of course, reading the FAQ on not using scanf() at all, and using fgets() to read the input, then maybe using sscanf() to parse the buffer is a much preferred option.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  2. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 10:31 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C# Programming
    Replies: 0
    Last Post: 10-14-2002, 01:26 PM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM
  5. structs, array, code has problems, Im lost
    By rake in forum C++ Programming
    Replies: 4
    Last Post: 03-13-2002, 01:02 AM

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