Thread: Help with a basic scanf procedure.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by killpoppop View Post
    Ah i get it. It's all about memory so if i enter 12. It will store 1 as m and 2 as km?

    So what i've been doing is enetering say 'km'. That will store 'k' as m and 'm' as km?
    That's right. And if you had typed "m", you would have gotten 'm' in the variable m and '\n' (=enter key) in the variable km.

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    So chuffed guys i got it made a bad mistake in the print functions both were using the kilometers function to convert. Here's the finished code.

    Code:
    #include<stdio.h>
    
    double kilometres( double m ) 
    {
    	return m * 1.609344 ;
    }
    
    double miles( double km )
    {
    	return km * 0.621371192 ; 
    }
    
    int main( void ) 
    {
    	float a ;
    	float b ;
    	char c, d ;
    	printf( "Do you wish to convert m or km?\n") ;
    	scanf( "&#37;c%c", &c, &d) ;  
    	
    	if( c == 'k' && d == 'm' )
    	{
    		printf( "Y number of km converted to m:\n") ;
    		scanf( "%f", &b ) ; 
      		printf( "= %f km\n", miles((float) b)) ;
    	}
    	
    	else
    	{
    		printf( "X number of m converted to km:\n") ;
    		scanf( "%f", &a ) ; 
      		printf( "= %f km\n", kilometres((float) a)) ;
    	}
      	return 0 ;
    }

    Thanks again! =]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-20-2005, 01:48 PM
  2. scanf to struct pointer
    By ronenk in forum C Programming
    Replies: 11
    Last Post: 12-20-2004, 10:22 AM
  3. First scanf() skips next scanf() !
    By grahampatten in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:47 AM
  4. Replies: 2
    Last Post: 11-10-2003, 09:12 PM
  5. Collision with quads?
    By SyntaxBubble in forum Game Programming
    Replies: 6
    Last Post: 01-18-2002, 06:17 PM