将输入从int转换为double

时间:2012-10-14 20:24:44

标签: c

有没有办法将实际输入int转换为double。我不想获取字符串值并使用isDigit来检查它是double还是int。

int main(void)
{

    // Initializing variables
    int a,b,c,res;

    // Getting input from user
    printf("Please enter the sides of triangle");

    printf("\nPlease enter side 1:");
    scanf("%d",&a);

    printf("Please enter side 2:");
    scanf("%d",&b);

    printf("Please enter side 3:");
    scanf("%d",&c);


    // Calling function in order to print type of the triangle.
    checkTriangle(a,b,c);



}

// A function which decides the type of the triangle and print it
void checkTriangle(int s1, int s2,int s3)
{


    // Check the values whether it is triangle or not.
  if ((s1 + s2 > s3 && s1 +  s3 > s2 && s2 + s3 > s1) && (s1 > 0 && s2 > 0 && s3 > 0))
     {
    // Deciding type of triangle according to given input.
      if (s1 == s2 && s2 == s3)
          printf("EQUILATERAL TRIANGLE");
      else if (s1 == s2 || s2 == s3 || s1 == s3)
          printf("ISOSCELES TRIANGLE\n");
      else
          printf("SCALENE TRIANGLE \n");
    }
     else
         printf("Triangle could not be formed.");


}

当用户键入双精度时,我想要做什么我想给出一个可以输入double的消息。没有得到字符输入有没有办法做到这一点?

3 个答案:

答案 0 :(得分:2)

如果您希望将int值变为double,只需使用演员:

int foo = 5;
double bar = (double) foo;

答案 1 :(得分:2)

如果要将输入作为double读取,请使用:

double inputValue;
scanf("%lf", &inputValue);

"%lf"告诉scanf输入为双倍。

答案 2 :(得分:0)

你可以使用scanf(“%d”,& u) 或者您可以将输入作为int或char类型,然后将其类型化为double。