0

I wrote some code in C to convert your distance to other distance units but it seems to mix up the numbers and miscalculate the result.

I use a Windows 10 laptop, but, I don't think it matters because I used an online compiler (GDB) to write the code. I really don't even know where is the problem.

#include <stdio.h>

int main ()
{
    float distance;
    char in_unit[20];

    printf("Enter your distance:\n");
    scanf("%f", &distance);

    printf("Enter the length unit of your distance.(C=centimeters, Ft=feet, Kg=kilometers, In=inches, M=miles, Mtrs=meters)\n");
    scanf("%s", in_unit);
    if (in_unit == "Cm");
    {
        printf("Your distance in feet is:%f", distance/30.48);
        printf("\nYour distance in kilometers is:%f", distance/100000);
        printf("\nYour distance in inches is:%f", distance/2.54);
        printf("\nYour distance in miles is:%f", distance/160934.4);
        printf("\nYour distance in meters is:%f", distance/100);
    }
    if (in_unit == "Ft")
    {
        printf("Your distance in centimeters is:%f", distance*30.48);
        printf("\nYour distance in kilometers is:%f", distance/3280.84);
        printf("\nYour distance in inches is:%f", distance*12);
        printf("\nYour distance in miles is:%f", distance/5280);
        printf("\nYour distance in meters is:%f", distance/3.281);
    }
    if (in_unit == "Kg")
    {
        printf("Your distance in centimeters is:%f", distance * 100000);
        printf("\nYour distance in feet is:%f", distance * 3280.84);
        printf("\nYour distance in inches is:%f", distance * 39370.079);
        printf("\nYour distance in miles is:%f", distance / 1.609);
        printf("\nYour distance in meters is:%f", distance * 1000);
    }
    if (in_unit == "In")
    {
        printf("Your distance in centimeters is:%f", distance*2.54);
        printf("\nYour distance in feet is:%f", distance/12);
        printf("\nYour distance in kilometers is:%f", distance/39370.079);
        printf("\nYour distance in miles is:%f", distance/63360);
        printf("\nYour distance in meters is:%f", distance/39.37);
    }
    if (in_unit == "M")
    {
       printf("Your distance in centimeters is:%f", distance*160934.4);
       printf("\nYour distance in feet is:%f", distance*5280);
       printf("\nYour distance in kilometers is:%f", distance*1.609);
       printf("\nYour distance in inches is:%f", distance*63360);
       printf("\nYour distance in meters is:%f", distance*1609.344);
    }
    if (in_unit == "Mtrs")
    {
       printf("Your distance in centimeters is:%f", distance*100);
       printf("\nYour distance in feet is:%f", distance*3.281);
       printf("\nYour distance in kilometers is:%f", distance/1000);
       printf("\nYour distance in inches is:%f", distance*39.37);
       printf("\nYour distance in miles is:%f", distance/1609.344);
    }
    return 0;
}

For example, this is one of the runs:

Enter your distance:                                                                                                         
1
Enter the length unit of your distance. (C=centimeters, Ft=feet, Kg=kilometers, In=inches, M=miles, Mtrs=meters)              
Kg                                                                                                                           
Your distance in feet is:0.032808                                                                                            
Your distance in kilometers is:0.000010                                                                                      
Your distance in inches is:0.393701
Your distance in miles is:0.000006                                                                                           
Your distance in meters is:0.010000
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
abdulla farhoud
  • 183
  • 1
  • 1
  • 5
  • 2
    `in_unit == "Cm"` - you can't compare strings like this in `C`. Use `strcmp`. I don't even quite understand how you have managed to get *any* results at all. – Eugene Sh. Jan 29 '19 at 17:47
  • Note that `kg` is the SI symbol for kilograms; use `km` for kilometres (or kilometers if you've got an American spell checker at work). – Jonathan Leffler Jan 29 '19 at 17:54
  • Stylistically, it is better to end `printf()` format strings with a newline than to begin them with a newline. All else apart, it improves the chance of the last line of output being shown — very often, the output doesn't appear until after a newline is produced. Once you've got the `strcmp()` problem solved, you'll want to report when the user types ºC instead of Mtrs — so you'll look at using `else if` instead of just `if`, so that you can have a final `else` that covers "units not recognized above". Then you should think about how you can improve the modularity of your code. – Jonathan Leffler Jan 29 '19 at 17:58

0 Answers0