Thread: Help with a basic scanf procedure.

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    44

    Help with a basic scanf procedure.

    Hey guys. I've been studying C for a few weeks now, and decided to have a crack at a program this evening. I would give up and watch a movie but I'm so close and want to finish. Basically i just did a simple conversion from miles to kilometers and vice versa, and was messing round with the scanf function.

    I was wondering how i could use the scanf function to let the 'user' choose whether they want to convert from m to km, or from km to m.

    Sorry if that makes no sense. Feel free to ask questions. Anyway here is the code, you can see where i get lost, i just need something which will define which conversion to do. Also if any of you guys have some tips on how to neaten up or help my coding all feedback is greatly appreciated.

    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 ;
    	float i ;
    	char m, km ;
    	printf( "Do you wish to convert m or km?\n") ;
    	i = scanf( "%c%c", &m, &km) ;  
    	
    	if( i = km )
    	{
    		printf( "X number of m converted to km:\n") ;
    		scanf( "%f", &a ) ; 
      		printf( "= %f km\n", kilometres((float) a)) ;
    	}
    
    	else
    	{
    		printf( "Y number of km converted to m:\n") ;
    		scanf( "%f", &b ) ; 
      		printf( "= %f km\n", kilometres((float) b)) ;
    	}
      	return 0 ;
    }


    Cheers.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    So what happens if the user wants the converse ie from km to m or doesn't want to do any conversion??

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    Thats what I'm trying to work out. I can't work out what to put in the 'if' statement, because what I've currently done is completely wrong most probably. And if they don't want to do a conversion well they can close the program! =] It's just the 'if' statement that is bugging me =[

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    43
    Quote Originally Posted by killpoppop View Post
    Hey guys. I've been studying C for a few weeks now, and decided to have a crack at a program this evening. I would give up and watch a movie but I'm so close and want to finish. Basically i just did a simple conversion from miles to kilometers and vice versa, and was messing round with the scanf function.

    I was wondering how i could use the scanf function to let the 'user' choose whether they want to convert from m to km, or from km to m.

    Sorry if that makes no sense. Feel free to ask questions. Anyway here is the code, you can see where i get lost, i just need something which will define which conversion to do. Also if any of you guys have some tips on how to neaten up or help my coding all feedback is greatly appreciated.

    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 ;
    	float i ;
    	char m, km ;
    	printf( "Do you wish to convert m or km?\n") ;
    	i = scanf( "%c%c", &m, &km) ;  
    	
    	if( i = km )
    	{
    		printf( "X number of m converted to km:\n") ;
    		scanf( "%f", &a ) ; 
      		printf( "= %f km\n", kilometres((float) a)) ;
    	}
    
    	else
    	{
    		printf( "Y number of km converted to m:\n") ;
    		scanf( "%f", &b ) ; 
      		printf( "= %f km\n", kilometres((float) b)) ;
    	}
      	return 0 ;
    }


    Cheers.
    The scanf function returns the number of characters it read correctly. So what you are doing there is setting i to 2 if scanf reads in two characters correctly.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    I see so if i = 2, then it should convert KM?? but how does that work that M is only one digit?

  6. #6
    Registered User
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    5
    Code:
    i = scanf( "&#37;c%c", &m, &km) ;
    Here scanf will read two characters from the standard input (keyboard) and put one character in m and one in km. And as posted above, this declaration of i isn't what u desire.

    Mabey you should try do a menu where a user can choose between the number 1 and 2. 1 is for meter, 2 is for kilometers.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    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?

  8. #8
    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.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    right well hopefully I'll get it now. I had a massively stupidly moment when realizing i should be "==" for equals -.-

    The joys of late night programming. Thanks a lot people. If I'm still stuck in an hour or so you may see a quick return =]

  10. #10
    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