int Distance() {
char from[40], to[40];
double val;
double result;
printf("Enter what you want to convert from: ");
fflush(stdin);
fgets(from, 40, stdin);
printf("Enter what you want to convert to: \n");
fflush(stdin);
fgets(to, 40, stdin);
printf("Enter the numerical value of conversion: \n");
scanf("%lf", &val);
(strcmp(from, "cm") == 0 && strcmp(to, "mm") == 0) ? printf("Answer is %lf MilliMeters", val / 10) :
(strcmp(from, "mm") == 0 && strcmp(to, "cm") == 0) ? printf("Answer is %lf CentiMeters", val * 10) :
(strcmp(from, "cm") == 0 && strcmp(to, "km") == 0) ? printf("Answer if %lf KiloMeters", val * 100000) :
(strcmp(from, "km") == 0 && strcmp(to, "cm") == 0) ? printf("Answer is %lf CentiMeters", val / 100000) :
(strcmp(from, "mm") == 0 && strcmp(to, "km") == 0) ? printf("Anser is %lf KiloMeters", val * 1000000) :
(strcmp(from, "km") == 0 && strcmp(to, "mm") == 0) ? printf("Answer is %lf MilliMeters", val / 1000000) :
printf("Please enter valid conversion units");
}
I am trying to make a unit converter. So I have made different functions for different conversions - like int distance()
,
int time()
and so on.
In my function to calculate distances, I used fgets()
to get multi-char inputs like 'cm', 'mm, etc.
What i am trying to achieve is
If value of fgets
in char from
is "any string" and fgets
in char to
is "any other string": print result whose formula is -----(something)
It doesn't compute the result and directly jumps to the final printf("Please enter valid conversion units")
I am still learning at a beginner level, it could be a minor mistake, Please help.