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.