Flaoting point question

This is a discussion on Flaoting point question within the C Programming forums, part of the General Programming Boards category; I can't for the life of me figure out how I'm supposed to use float. Here's the simple peice of ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    68

    Flaoting point question

    I can't for the life of me figure out how I'm supposed to use float.
    Here's the simple peice of code:
    Code:
      	#include <stdio.h>
    			
    int main()
    {
    
    	int     centimeters;
    	float  inches;
    	
    	
    	printf("Enter the number of centimeters you would like converted to feet\n" );
    
    	scanf("%d" , &centimeters );
    
    	inches = centimeters / (float) 2.54;
    
    	printf("The total inches is %d\n", inches );
    
    	return 0;
    }
    Pretty simple, but I get a crazy number when I input centimeters (something like -1678682734). Any thoughts would be really appreciated, thanks.

    P.S. yes I'm a newbie programmer, but hey you gotta start somewhere

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    You need to use %f to print floats. Try changing the %d in your last printf() to a %f.
    If you understand what you're doing, you're not learning anything.

  3. #3
    FOX
    Join Date
    May 2005
    Posts
    188
    And why do you cast 2.54 to float, when it's already a float? If you wanted to divide centimeters with say 2 (int), then you would either have to cast 2 to float or use 2.0 instead of 2.

  4. #4
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    And why do you cast 2.54 to float, when it's already a float
    it's a double actually. For it to be a float you would have to have the "f" at the end. But I'm just being nit picky.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    68
    Quote Originally Posted by itsme86
    You need to use %f to print floats. Try changing the %d in your last printf() to a %f.
    Oh ya! D'uh I knew that too, really I did lol.
    Anyhow, thank you very much for the feedback. My teacher is a bit of a space case and so I'm sure I'll be back with something like

    "What the heck does he mean by that?" or something

    hehe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help realy needed
    By ZohebN in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2008, 09:37 AM
  2. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  3. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  4. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 11:13 AM
  5. trouble with overloaded operator
    By kkurz in forum C++ Programming
    Replies: 2
    Last Post: 10-31-2003, 11:59 AM

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