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.
There are multiple issues in the code:
fgets()
stores the trailing newline at the end of the target array, so all comparisons with unit strings will fail.Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct unit {
double factor;
const char *abbrev, *singular, *plural;
} const units[] = {
{ 1, "mm", "millimeter", "millimeters" },
{ 10, "cm", "centimeter", "centimeters" },
{ 100, "dm", "decimeter", "decimeters" },
{ 1000, "m", "meter", "meters" },
{ 10000, "dam", "decameter", "decameters" },
{ 100000, "hm", "hectometer", "hectometers" },
{ 1000000, "km", "kilometer", "kilometers" },
{ 25.4, "in", "inches", "inches" },
{ 304.8, "ft", "foot", "feet" },
{ 914.4, "yd", "yards", "yards" },
{ 201168, "fur", "furlong", "furlongs" },
{ 1609344, "mi", "mile", "miles" },
};
int get_unit(void) {
char buf[40];
while (fgets(buf, sizeof buf, stdin)) {
buf[strcspn(buf, "\n")] = '\0'; // remove the newline if any
for (int i = 0, n = sizeof(units) / sizeof(*units); i < n; i++) {
if (!strcmp(buf, units[i].abbrev)
|| !strcmp(buf, units[i].singular)
|| !strcmp(buf, units[i].plural)) {
return i;
}
}
printf("unknown unit\n");
}
printf("unexpected end of file, aborting\n");
exit(1);
}
int main(void) {
int from, to;
double val;
printf("Enter what you want to convert from: ");
from = get_unit();
printf("Enter what you want to convert to: ");
to = get_unit();
printf("Enter the numerical value of conversion: ");
if (scanf("%lf", &val) == 1) {
double result = val * units[from].factor / units[to].factor;
printf("Answer is %g %s\n", result, result == 1 ? units[to].singular : units[to].plural);
return 0;
}
return 1;
}