1

Hey im struggling with my code, i wanted to make a converter in C, but (im new to C), after using scanf to get input, it wont use it correctly. Here's the code:

double num1;
char unit[10];
char unitTwo[10];
double result;

printf("Enter a number : ");
scanf("%lf", &num1);
printf("Enter unit [m ; g ; l ; m3]: ");
scanf("%s", unit);

printf("Enter unit to convert to : ");
scanf("%s", unitTwo);

if(unit == "m" && unitTwo == "cm"){
    printf("%f\n", num1 * 100);
}else if(unitTwo == "dm"){
    printf("%f\n", num1 * 10);
}else if(unitTwo == "mm"){
    printf("%f\n", num1 * 1000);
}else if(unitTwo == "dam"){
    printf("%f\n", num1 / 10);
}else if(unitTwo == "hm"){
    printf("%f\n", num1 / 100);
}else if(unitTwo == "km"){
    printf("%f\n", num1 / 1000);
}else{
    printf("Choose valid entry.\n");
}

My scanf worked with ("%s", strvar); when i created a madlips. It will give me Choose valid entry even i input 3, m and cm...

Thanks in advance

daedsidog
  • 1,732
  • 2
  • 17
  • 36
Kendrik
  • 26
  • 3

0 Answers0