2017-01-25 7 views
0

사용자로부터 기본 IP 주소를 입력하려고하는데 내 명령이 scanf에서 멈추고 아무 것도 실행되지 않습니다.어떤 이유로 든 명령이 멈추는 문제가 발생했습니다.

int ip1,ip2,ip3,ip4; 
    scanf("%d.%d.%d.%d",&ip1,&ip2,&ip3,&ip4); 
    printf("Here"); 

기본적으로 "여기"는 인쇄되지 않으며 scanf 명령은 결코 끝나지 않습니까?

#include <stdio.h> 
#include<math.h> 
int main(void) { 
char input; 
char rep = 'r'; 
char quit = 'q'; 
char first = '1'; 
char second = '2'; 
input = rep; 
while(input != quit) { 
    printf("What type of conversion do you want? \n"); 
    printf("Enter 1 for 32-bit number to dot-decimal conversion, 2 for the inverse of operation: "); 
     char val; 
    scanf(" %c", &val); 
    if(val == first) { 
    } else if(val == second) { 
     printf("\nEnter dot-decimal IP address:"); 

     int ip1,ip2,ip3,ip4; 
     scanf(" %d.%d.%d.%d", &ip1,&ip2,&ip3,&ip4); 
     printf("Here"); 
     unsigned int ip = 0,c,k,counter = 31; 
     for(c = 7; c >= 0; c--) { 
      k = ip1 >> c; 
      if(k & 1) { 
       int temp = 2,i; 
       for(i = 0; i < counter;i++) { 
        temp *= 2; 
       } 
       ip += temp; 
       counter--; 
      } 

     } 

     for(c = 7; c >= 0; c--) { 
      k = ip2 >> c; 
      if(k & 1) { 
       int temp = 2,i; 
       for(i = 0; i < counter;i++) { 
        temp *= 2; 
       } 
       ip += temp; 
       counter--; 
      } 
     } 


     for(c = 7; c >= 0; c--) { 
      k = ip3 >> c; 
      if(k & 1) { 
       int temp = 2,i; 
       for(i = 0; i < counter;i++) { 
        temp *= 2; 
       } 
       ip += temp; 
       counter--;    
      } 
     } 

     for(c = 7; c >= 0; c--) { 
      k = ip4 >> c; 
      if(k & 1) { 
       int temp = 2,i; 
       for(i = 0; i < counter;i++) { 
        temp *= 2; 
       } 
       ip += temp; 
       counter--;    
      } 
     } 


     printf("%u is the IP Address",ip); 

    } 
    printf("\n \n Enter r to repeat, q to quit:"); 
    scanf(" %c",&input); 
} 
return 0; 

}

이는 내가 일을 정확히 것입니다. 10 진수 표기법으로 IP 주소를 얻으려고 할 때 막히게됩니다.

+0

귀하의 의견을 알려주세요. – BLUEPIXY

+2

어떻게 입력을 전달합니까? – Inian

+0

192.162.2.3 (단지 하나의 경우) – dave

답변

1

업데이트 (전체 코드) 후 코드를 분석 한 결과 해당 입력이 scanf이 아닌 데이터가 획득 된 후 실행되는 for 루프가있는 것으로 나타났습니다. 그 루프에서

봐 : 감소가 0 새로운 양의 값 UINT_MAX에서 만들기 때문에

unsigned int ip = 0,c,k,counter = 31; 
    for(c = 7; c >= 0; c--) { 
     k = ip1 >> c; 
     if(k & 1) { 
      int temp = 2,i; 
      for(i = 0; i < counter;i++) { 
       temp *= 2; 
      } 
      ip += temp; 
      counter--; 
     } 
    } 

특히 c 유형 unsigned int의 것을 고려 for(c = 7; c >= 0; c--)에서 ... 내가 볼 (이 루프가 INFINITE 것을 볼 limits.h).