# Help with a basic scanf procedure.

• 11-03-2008
killpoppop
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.
• 11-03-2008
itCbitC
So what happens if the user wants the converse ie from km to m or doesn't want to do any conversion??
• 11-03-2008
killpoppop
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 =[
• 11-03-2008
mesmer
Quote:

Originally Posted by killpoppop
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.
• 11-03-2008
killpoppop
I see so if i = 2, then it should convert KM?? but how does that work that M is only one digit?
• 11-03-2008
Oester
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.
• 11-03-2008
killpoppop
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?
• 11-03-2008
tabstop
Quote:

Originally Posted by killpoppop
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.
• 11-03-2008
killpoppop
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 =]
• 11-03-2008
killpoppop
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! =]