Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <stdio.h>
int main()
{
    float a, b;
    printf("Type 0 to convert km to miles\nType 1 to convert kg to pounds\nType 2 to convert  cm to inches\n");
    scanf("%d", &a);
    if (a == 0)
    {

        printf("Enter km to convert in miles:\n");
        scanf("%f", &b); //1.609
        printf("%f km is equals to %f", b, b / 1.609);
    }
    else if (a == 1)
    {
        printf("Enter kgs to convert in pounds:\n");
        scanf("%f", &b);
        printf("%f kg is equals to %f Pounds", b, b * 2.205);
    }
    else if (a == 2)
    {
        printf("Enter cm to convert in inches:\n");
        scanf("%f", &b);
        printf("%f cm is equals to %f inches", b, b * 2.54);
    }
    return 0;
}


What I have tried:

When i run this code and enter 0 it works fine as it would be but when i try other values (ie., 1 and 2 ) code do not run . it get exit . please help me i am stucked.
Posted
Updated 3-Mar-21 20:25pm
v2

In addition to other solutions:

  1. Don't use float unless you have very good reason for. Use double instead.
  2. Your code should handle incorrect user choice.
  3. The cm to inch formula in your code is wrong.
Try
C
#include <stdio.h>
int main()
{
    double arg, result;
    int choice;
    printf("Type 0 to convert km to miles\nType 1 to convert kg to pounds\nType 2 to convert  cm to inches\n");
    scanf("%d", &choice);
    if (choice == 0)
    {
        printf("Enter km to convert in miles:\n");
        scanf("%lf", &arg); //1.609
        result = arg / 1.609;
        printf("%f km is equals to %f\n", arg, result);
    }
    else if (choice == 1)
    {
        printf("Enter kgs to convert in pounds:\n");
        scanf("%lf", &arg);
        result = arg * 2.205;
        printf("%f kg is equals to %f Pounds\n", arg, result);
    }
    else if (choice == 2)
    {
        printf("Enter cm to convert in inches:\n");
        scanf("%lf", &arg);
        result = arg / 2.54;
        printf("%f cm is equals to %f inches\n", arg, result);
    }
    else
    {
      printf("Invalid choice\n");
    }
    return 0;
}
 
Share this answer
 
v2
Comments
Patrice T 4-Mar-21 2:31am    
+5
CPallini 4-Mar-21 2:41am    
Thank you!
You have declared a as float.
Then you use scanf to store an integer value in that place.
An integer 0 has the same bit pattern as a float 0.0,
but the integer values for 1 and 2 are nothing like the floats
for 1.0 and 2.0

I'll leave you to work out the fix.
 
Share this answer
 
a variable type should be integer. Integer is a type of number which does not have fractional parts, unlike float. Input of zero works for both the integer and float types because 0 and 0.0 are all the bits of the variable set to zero, so they are equivalent.

Try this.

C++
int a = 0;
float b = 0.0f;
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900