2012-10-14 9 views
-1

실제 입력 int를 double로 변환하는 방법이 있습니까? 문자열 값을 가져 오지 않고 isDigit을 사용하여 double인지 int인지 확인합니다.int에서 double로 입력 변환

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을 입력 할 때 내가 원하는 것은 double을 입력 할 수 있다는 메시지를 전하고 싶습니다. 문자 입력을하지 않고도 그렇게 할 수 있습니까?

+0

이 입력은 어디에서 오는가? 너 뭐하려고? – epsalon

+0

사용자가 double input을 입력하면 int로 변환됩니다. 이걸 어떻게 막을 수 있습니까? – snnlankrdsm

+0

@MertMetin, 아마도 int를 기대하고 있기 때문에 그것은 숫자가 아니기 때문에 십진수에서 멈 춥니 다. – chris

답변

2

당신은 단순히 캐스트를 사용하면 double로 전환하고자하는 정지 int 값이있는 경우 :

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

캐스트가 필요하지 않습니다. – ouah

2

당신은 두 번 사용으로 입력을 읽으려면 :

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

"%lf"은 입력이 double임을 scanf에게 알려줍니다.

+2

사실, 당신은'% lf'을 원할 것입니다. '% ld'은 부동 소수점이 아닌'long'을 읽는 것입니다. – chris

0

scanf ("% d", & u) 을 사용하거나 int 또는 char 유형으로 입력 한 다음 double으로 유형 변환 할 수 있습니다.

+0

'% d'은 (는)'chars'을 읽지 않습니다.'ints'를 읽습니다. –

+0

예. getchar() 또는 scanf ("% c")를 사용하여 엄격하게 char을 가져올 수 있습니다. –

관련 문제