2012-06-14 4 views
0

다음 프로그램을 컴파일 할 때 부동 소수점 오류가 불법적으로 사용됩니다. 내가 실수를 저지르고 있음을 알려주십시오.c의 부동 소수점을 사용하는 계수

#include<stdio.h> 
#include<conio.h> 

void main() 
{ 
    float a; 
    clrscr(); 
    printf("\n Enter the num : "); 
    scanf("%f", &a); 

    if (a >= 0) 
    { 
     if ((a % 2) == 0) //ERROR HERE 
     { 
      printf("\n You entered a positive even num"); 
     } 
     else 
     { 
      printf("\n You entered a positive odd num"); 
     } 
    } 
    else 
    { 
     if ((a % 2) == 0) //ERROR HERE 
     { 
      printf("\n You entered a negative even num"); 
     } 
     else 
     { 
      printf("\n You entered a negative odd num"); 
     } 
    } 
    getch(); 
} 

답변

5

%은 정수 유형입니다. fmod()을 사용하십시오.

평소와 마찬가지로 performing equality comparisons (==) with floating-point types을 매우주의하십시오. 아마도 당신의 경우 정수형으로 작업하는 것이 더 낫습니다.

+3

... "이상한/추측에 대한 추론을한다면 어쨌든 정수로 작업 할 것입니다."'a'를'int'로 변경하십시오. – Vlad

+0

fmod() 함수에서 전달할 매개 변수는 무엇입니까? – rippy

+0

@rippy : 이제 일부 문서에 대한 링크를 추가했습니다. –

관련 문제