2017-02-24 13 views
0
#include <math.h> 
#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include <assert.h> 
#include <limits.h> 
#include <stdbool.h> 

int main() 
{ 
    int n,i; 
    char a[10][100]; 
    printf("\n Enter the no. of strings:"); 
    scanf("%d",&n); 

    printf("\n enter the %d numbers:",n); 

    for(i=0;i<n;i++) 
    { 
     printf("\n %d",i); 

     gets(a[i]); 
    } 
    for(i=0;i<=n;i++) 
     { 
      puts(a[i]); 
     } 
    return 0; 
} 

의 C 입력() 함수를 가져, 왜 0에 입력하지 않는다?는 그것이 <code>0</code> 건너 뛰고 인덱스 <code>1</code>에서 두 개의 스트링을 취하고 <code>2</code> 다음 <code>n = 3</code> 경우 배열

여기서 a은 문자열 배열입니다.

+1

'gets'는 올바르게 사용할 수없는 기능입니다. 따라서 최신 C 표준에서 제거되었습니다. 그것을 사용하는 것은 버그입니다. 대신'fgets'을 사용해보십시오. – user694733

+2

그 전에'scanf'는 입력 버퍼에'\ n'을 남겨두고 첫 번째 반복에서'gets'를 읽습니다. BTW, [** 결코'gets' **를 사용하지 마십시오. 그것은 위험합니다!] (http://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) –

답변

0

잘못된 행동에 대한 이유는 scanfn의 입력을 확인하는 것이 필요하다를 입력 읽지 않는다는 것입니다. 당신이 getsdummy 호출을 추가하면 수행합니다 원래 하나

#include <math.h> 
#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include <assert.h> 
#include <limits.h> 
#include <stdbool.h> 

int main() 
{ 
    int n,i; 
    char a[10][100]; 
    printf("\n Enter the no. of strings:"); 
    scanf("%d",&n); gets(a[0]); 

    printf("\n enter the %d numbers:",n); 

    for(i=0;i<n;++i) 
    { 
     printf("\n %d",i); 

     gets(a[i]); 
    } 
    for(i=0;i<n;++i) 
     { 
      puts(a[i]); 
     } 
    return 0; 
} 

하십시오 diff 내 버전. 출력 루프에서 또 다른 문제를 수정했습니다.

+0

감사합니다! 나를 – ash